【建表语句】

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. ENGINE =MyISAM DEFAULT CHARACTER SET latin1 COLLATE latin1_general_cs AUTO_INCREMENT=0; 什么意思

    ENGINE =MyISAM //表类型为myisam插写比较快 DEFAULT CHARACTER  SET  latin1//默认字符为latin1 COLLATE  latin1_general ...

  2. 论文学习 :Learning a Deep Convolutional Network for Image Super-Resolution 2014

    (Learning a Deep Convolutional Network for Image Super-Resolution, ECCV2014) 摘要:我们提出了一种单图像超分辨率的深度学习方 ...

  3. 百度编辑器ueditor上传图片失败,显示上传错误,实际上图片已经传到服务器或者本地

    报错,上传失败,图片没有显示,且调试response没有信息,但是图片已经上传到了本地 这个问题是因为ueditor里面的Upload.class.php里面__construct()方法里面的ico ...

  4. 安装Mantis出现的一些问题解决

    1.去除 Warning: date_default_timezone_get() 打开config_inc.php, 增加: $g_default_timezone = 'RPC'; 2.去除  S ...

  5. Appium关键字

    *** Settings *** Library AppiumLibrary Library AutoItLibrary Library os *** Keywords *** xpath应该匹配次数 ...

  6. Flex String拼接

    平时Flex String拼接的时候直接str+str2 今天就想看看Flex自带的函数好不好用,然后使用 str.concat(str2); Alert.show(str); 结果没有变化,才发现一 ...

  7. 基于Python对象引用、可变性和垃圾回收详解

    基于Python对象引用.可变性和垃圾回收详解 下面小编就为大家带来一篇基于Python对象引用.可变性和垃圾回收详解.小编觉得挺不错的,现在就分享给大家,也给大家做个参考. 变量不是盒子 在示例所示 ...

  8. 【算法与数据结构】二叉堆和优先队列 Priority Queue

    优先队列的特点 普通队列遵守先进先出(FIFO)的规则,而优先队列虽然也叫队列,规则有所不同: 最大优先队列:优先级最高的元素先出队 最小优先队列:优先级最低的元素先出队 优先队列可以用下面几种数据结 ...

  9. cocos2dx基础篇(16) 基本绘图DrawPrimitives

    [3.x] (1)去掉前缀 "cc" (2)将 ccDraw***() 封装到了 DrawPrimitives 命名空间中. (3)重写绘图函数:         draw(Ren ...

  10. CentOS7.查看进程占用端口情况

    1.命令:"netstat -lntp" 2.没有改命令的话,需要安装 net-tools工具:"yum install net-tools" 3. 4. 5.