【建表语句】

create table test03(
id int primary key not null auto_increment,
c1 char(10),
c2 char(10),
c3 char(10),
c4 char(10),
c5 char(10)
); insert into test03(c1,c2,c3,c4,c5) values('a1','a2','a3','a4','a5');
insert into test03(c1,c2,c3,c4,c5) values('b1','b2','b3','b4','b5');
insert into test03(c1,c2,c3,c4,c5) values('c1','c2','c3','c4','c5');
insert into test03(c1,c2,c3,c4,c5) values('d1','d2','d3','d4','d5');
insert into test03(c1,c2,c3,c4,c5) values('e1','e2','e3','e4','e5'); select * from test03;

【建索引】

create index idx_test03_c1234 on test03(c1,c2,c3,c4);
show index from test03;

问题:我们创建了复合索引idx_test03_c1234 ,根据以下SQL分析下索引使用情况?

 explain select * from test03 where c1='a1';
explain select * from test03 where c1='a1' and c2='a2';
explain select * from test03 where c1='a1' and c2='a2' and c3='a3';
explain select * from test03 where c1='a1' and c2='a2' and c3='a3' and c4='a4';

1)
explain select * from test03 where c1='a1' and c2='a2' and c3='a3' and c4='a4';

2)
explain select * from test03 where c1='a1' and c2='a2' and c4='a4' and c3='a3';

explain select * from test03 where c4='a4' and c3='a3' and c2='a2' and c1='a1';

3) 
explain select * from test03 where c1='a1' and c2='a2' and c3>'a3' and c4='a4';

4)
explain select * from test03 where c1='a1' and c2='a2' and c4>'a4' and c3='a3';

说明:4个索引全部使用,虽然c3在最后,但是mysql可以自动调优。

5)
explain select * from test03 where c1='a1' and c2='a2' and c4='a4' order by c3;

c3作用在排序而不是查找

【索引的两大功能:查找和排序】
6)
explain select * from test03 where c1='a1' and c2='a2' order by c3;

7)
explain select * from test03 where c1='a1' and c2='a2' order by c4;
出现了filesort

8)
8.1
explain select * from test03 where c1='a1' and c5='a5' order by c2,c3;

只用c1一个字段索引,但是c2、c3用于排序,无filesort
8.2
explain select * from test03 where c1='a1' and c5='a5' order by c3,c2;

出现了filesort,我们建的索引是1234,它没有按照顺序来,3 2 颠倒了

9)
explain select * from test03 where c1='a1' and c2='a2' order by c2,c3;

10)
explain select * from test03 where c1='a1' and c2='a2' and c5='a5' order by c2,c3;

用c1、c2两个字段索引,但是c2、c3用于排序,无filesort

explain select * from test03 where c1='a1' and c2='a2' and c5='a5' order by c3,c2;

本例有常量c2的情况,和8.2对比(c2='c2'已经有具体值,为常量时,无需排序)

explain select * from test03 where c1='a1' and c5='a5' order by c3,c2;

filesort出现

11)
explain select * from test03 where c1='a1' and c4='a4' group by c2,c3;

12)
explain select * from test03 where c1='a1' and c4='a4' group by c3,c2;

Using where; Using temporary; Using filesort

【group by表面理解为分组,但是要注意的是,分组之前必排序】

【结论】

【一般性建议】

1、对于单键索引,尽量选择针对当前query过滤性更好的索引

2、在选择组合索引的时候,当前Query中过滤性最好的字段在索引字段顺序中,位置越靠前(左)越好。(避免索引过滤性好的索引失效)

3、在选择组合索引的时候,尽量选择可以能够包含当前query中的where字句中更多字段的索引

4、尽可能通过分析统计信息和调整query的写法来达到选择合适索引的目的

MySQL索引面试题分析(索引分析,典型题目案例)的更多相关文章

  1. MySQL高级知识(七)——索引面试题分析

    前言:该篇随笔通过一些案例,对索引相关的面试题进行分析. 0.准备 #1.创建test表(测试表). drop table if exists test; create table test( id ...

  2. 干货—MySQL常见的面试题+索引原理分析!

    目录 MySQL索引的本质 MySQL索引的底层原理 MySQL索引的实战经验 面试 问:数据库中最常见的慢查询优化方式是什么? 同学A:加索引. 问:为什么加索引能优化慢查询? 同学A:...不知道 ...

  3. MYSQL索引结构原理、性能分析与优化

    [转]MYSQL索引结构原理.性能分析与优化 第一部分:基础知识 索引 官方介绍索引是帮助MySQL高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里, 不用一页一页 ...

  4. Atitit Mysql查询优化器 存取类型 范围存取类型 索引存取类型 AND or的分析

    Atitit Mysql查询优化器 存取类型 范围存取类型 索引存取类型 AND or的分析     Atitit Mysql查询优化器 存取类型 范围存取类型 索引存取类型 AND or的分析1 存 ...

  5. 由浅入深探究mysql索引结构原理、性能分析与优化 转

    第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1. 简单介绍B-tree B+ tree树 2. MyisAM索引结构 3. Annode索引结构 4. MyisAM索引与Inno ...

  6. 由浅入深探究mysql索引结构原理、性能分析与优化

    摘要: 第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1.简单介绍B-tree B+ tree树 2.MyisAM索引结构 3.Annode索引结构 4.MyisAM索引与Inno ...

  7. Mysql索引分析:适合建索引?不适合建索引?【转】

    数据库建立索引常用的规则如下: 1.表的主键.外键必须有索引: 2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特 ...

  8. 【Explain】mysql之explain详解(分析索引的最佳使用)

    在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain 这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句 ...

  9. 【转】由浅入深探究mysql索引结构原理、性能分析与优化

    摘要: 第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1.简单介绍B-tree B+ tree树 2.MyisAM索引结构 3.Annode索引结构 4.MyisAM索引与Inno ...

随机推荐

  1. JMS学习二(简单的ActiveMQ实例)

    下载安装ActiveMQ服务,下载地址当然可以去官网下载 http://activemq.apache.org/download-archives.html ActiveMQ安装很简单,下载解压后到b ...

  2. Oracle使用正则表达式拆分字段里多行分布式值

    不规范的表设计往往会带来程序设计上的麻烦,也会降低SQL的性能. 例如下表显示的内容: 这样我们调取多值字段用来做匹配的话就比较麻烦,我们可以通过正则表达式REGEXP_SUBSTR先将 多值得列分成 ...

  3. spring session 加载的时候一些配置问题

    启动springboot时候的错误信息: An attempt was made to call the method org.springframework.boot.autoconfigure.s ...

  4. 使用virt-manager创建无盘工作站(基于nfs文件系统)

    首先需要做些准备工作: 1.安装xming及virt-manager, 安装过程网上很多,就不一一叙述了.安装完成后,使用putty登陆服务器,需要配置如下: connection -> SSH ...

  5. Quartz安装包中的15个example

    Welcome======= Welcome to the Quartz Examples directory. This directory contains 15 examples that sh ...

  6. Go开发[八]goroutine和channel

    进程和线程 进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位. 一个进程可以创 ...

  7. Vue知识整理10:条件渲染(v-if v-show)

    在Vue中使用v-if等条件实现条件的判断来实现对象的显示. 也可以采用 v-show条件来实现对象的显示.

  8. tuple用法

    1 tuple中的元素可以直接赋给相同个数的变量 tup1 = ('asfa',234) p, q = tup1 print(p) print(q) # asfa # 参考:https://www.r ...

  9. 2018.03.29 python-pandas 数据读取

    #数据读取# read_table,read_csv,read_excel #读取普通分隔数据:read_table #可以读取txt,csv import os import pandas as p ...

  10. Spring MVC的RequestContextHolder使用误区

    JShop简介:jshop是一套使用Java语言开发的B2C网店系统,致力于为个人和中小企业提供免费.好用的网店系统. 项目主页:http://git.oschina.net/dinguangx/js ...