【金三银四】深刻理解MySql索引底层数据结构解密 juejin.im/post/5e0d7b…mysql
【金三银四】MySql执行计划EXPLAIN详解 juejin.im/post/5e12e4…sql
【金三银四】MySql执行计划EXPLAIN最佳实践 juejin.im/post/5e12e4…bash
【金三银四】MySql索引优化实战 juejin.im/post/5e12e5…数据结构
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` varchar(10) DEFAULT NULL,
`c2` varchar(10) DEFAULT NULL,
`c3` varchar(10) DEFAULT NULL,
`c4` varchar(10) DEFAULT NULL,
`c5` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into test(c1,c2,c3,c4,c5) values('a1','a2','a3','a4','a5');
insert into test(c1,c2,c3,c4,c5) values('b1','b2','b3','b4','b5');
insert into test(c1,c2,c3,c4,c5) values('c1','c2','c3','c4','c5');
insert into test(c1,c2,c3,c4,c5) values('d1','d2','d3','d4','d5');
insert into test(c1,c2,c3,c4,c5) values('e1','e2','e3','e4','e5');
复制代码
create index idx_test_c1234 on test(c1,c2,c3,c4);
show index from test;
复制代码
explain select * from test where c1='a1' and c2='a2' and c3='a3' and c4='a4';
explain select * from test where c1='a1' and c4='a4' and c2='a2' and c3='a3' ;
复制代码
分析:post
①建立复合索引的顺序为c1,c2,c3,c4。测试
②上述explain执行的结果都同样:type=ref,key_len=132,ref=const,const,const,const。优化
结论:在执行常量等值查询时,改变索引列的顺序并不会更改explain的执行结果,由于mysql底层优化器会进行优化,可是推荐按照索引顺序列编写sql语句。ui
explain select * from test where c1='a1' and c2='a2';
复制代码
explain select * from test where c1='a1' and c2='a2' and c3>'a3' and c4='a4';
复制代码
分析:spa
当出现范围的时候,type=range,key_len=99,比不用范围key_len=66增长了,说明使用上了索引,但对比Case1中执行结果,说明c4上索引失效。3d
结论:范围右边索引列失效,可是范围当前位置(c3)的索引是有效的,从key_len=99可证实。
explain select * from test where c1='a1' and c2='a2' and c4>'a4' and c3='a3' ;
复制代码
分析:
与上面explain执行结果对比,key_len=132说明索引用到了4个,由于对此sql语句mysql底层优化器会进行优化:范围右边索引列失效(c4右边已经没有索引列了),注意索引的顺序(c1,c2,c3,c4),因此c4右边不会出现失效的索引列,所以4个索引所有用上。
结论:范围右边索引列失效,是有顺序的:c1,c2,c3,c4,若是c3有范围,则c4失效;若是c4有范围,则没有失效的索引列,从而会使用所有索引。
explain select * from test where c1>'a1' and c2='a2' and c3='a3' and c4='a4';
复制代码
分析:
若是在c1处使用范围,则type=ALL,key=Null,索引失效,全表扫描,这里违背了最左前缀法则,带头大哥已死,由于c1主要用于范围,而不是查询。
解决方式使用覆盖索引。
explain select * from test where c1='a1' and c2='a2' and c4='a4' order by c3;
复制代码
分析:
利用最左前缀法则:中间兄弟不能断,所以用到了c1和c2索引(查找),从key_len=66,ref=const,const,c3索引列用在排序过程当中。
explain select * from test where c1='a1' and c2='a2' order by c3;
复制代码
分析:
从explain的执行结果来看:key_len=66,ref=const,const,从而查找只用到c1和c2索引,c3索引用于排序。
explain select * from test where c1='a1' and c2='a2' order by c4;
复制代码
分析:
从explain的执行结果来看:key_len=66,ref=const,const,查询使用了c1和c2索引,因为用了c4进行排序,跳过了c3,出现了Using filesort。
explain select * from test where c1='a1' and c5='a5' order by c2,c3;
复制代码
分析:
查找只用到索引c1,c2和c3用于排序,无Using filesort。
分析:
和Case 4中explain的执行结果同样,可是出现了Using filesort,由于索引的建立顺序为c1,c2,c3,c4,可是排序的时候c2和c3颠倒位置了。
explain select * from test where c1='a1' and c2='a2' order by c2,c3;
复制代码
分析:
在查询时增长了c5,可是explain的执行结果同样,由于c5并未建立索引。
explain select * from test where c1='a1' and c2='a2' and c5='a5' order by c3,c2;
复制代码
分析:
与Case 4.1对比,在Extra中并未出现Using filesort,由于c2为常量,在排序中被优化,因此索引未颠倒,不会出现Using filesort。
explain select * from test where c1='a1' and c4='a4' group by c2,c3;
复制代码
分析:
只用到c1上的索引,由于c4中间间断了,根据最左前缀法则,因此key_len=33,ref=const,表示只用到一个索引。
explain select * from test where c1='a1' and c4='a4' group by c3,c2;
复制代码
分析:
对比Case 5,在group by时交换了c2和c3的位置,结果出现Using temporary和Using filesort,极度恶劣。缘由:c3和c2与索引建立顺序相反。
explain select * from test where c1>'a1' order by c1;
复制代码
分析:
①在c1,c2,c3,c4上建立了索引,直接在c1上使用范围,致使了索引失效,全表扫描:type=ALL,ref=Null。由于此时c1主要用于排序,并非查询。
②使用c1进行排序,出现了Using filesort。
③解决方法:使用覆盖索引。
explain select c1 from test order by c1 asc,c2 desc;
复制代码
分析:
虽然排序的字段列与索引顺序同样,且order by默认升序,这里c2 desc变成了降序,致使与索引的排序方式不一样,从而产生Using filesort。
EXPLAIN extended select c1 from test where c1 in ('a1','b1') ORDER BY c2,c3;
复制代码
分析:
对于排序来讲,多个相等条件也是范围查询
①MySQL支持两种方式的排序filesort和index,Using index是指MySQL扫描索引自己完成排序。index效率高,filesort效率低。
②order by知足两种状况会使用Using index。
③尽可能在索引列上完成排序,遵循索引创建(索引建立的顺序)时的最左前缀法则。
④若是order by的条件不在索引列上,就会产生Using filesort。
⑤group by与order by很相似,其实质是先排序后分组,遵守索引建立顺序的最左前缀法则。注意where高于having,能写在where中的限定条件就不要去having限定了。
通俗理解口诀:
全值匹配我最爱,最左前缀要遵照;
带头大哥不能死,中间兄弟不能断;
索引列上少计算,范围以后全失效;
LIKE百分写最右,覆盖索引不写星;
不等空值还有or,索引失效要少用。