MySQL高级知识(八)——索引面试题分析

MySQL高级知识(八)——索引面试题分析

此博客的内容主要来源于尚硅谷的视频中,在此记录,以备以后自己查看。

准备

  1. 创建test表(测试表)
drop table if exists test;
create table test(
id int primary key auto_increment,
c1 varchar(10),
c2 varchar(10),
c3 varchar(10),
c4 varchar(10),
c5 varchar(10)
) 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');

select * from test;

  1. 创建索引
    create index idx_test_c1234 on test(c1,c2,c3,c4);
    show index from test;

在这里插入图片描述
在这里插入图片描述

事例分析

  • 事例1
explain select * from test where c1='a1' and c2='a2' and c3='a3' and c4='a4';
explain select * from test where c1='a1' and c3='a3' and c2='a2' and c4='a4';
explain select * from test where c1='a1' and c4='a4' and c3='a3' and c2='a2';
explain select * from test where c4='a4' and c3='a3' and c2='a2' and c1='a1';

在这里插入图片描述

分析:

  • 创建复合索引的顺序为c1,c2,c3,c4;
  • 上述的四组explain执行的结果都一样:type=ref,key_len=132,ref=const,const,const,const。(and忽略左右关系,优化器自动优化)

结论:在执行常量等值查询的时候,改变索引列的顺序并不会更改explain的执行结果,因为mysql底层的优化器会进行优化,但是推荐按照索引顺序列编写sql语句。


  • 事例2
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';

在这里插入图片描述

在这里插入图片描述

分析:

  • 当出现范围的时候,type=range,key_len=99,比不用范围key_len=66增加了,说明使用上了索引,但是对比事例1中执行的结果,说明c4索引失效。

结论:范围索引右边索引列失效,但是范围当前位置(c3)的索引是有效的,从key_len=99可以证明。


  • 事例2.1
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),所以吃
  • 右边不会出现失效的索引列,因此4个索引全部用上了。

结论:范围右边索引列失效,是由顺序的:c1,c2,c3,c4。如果c3有范围,则c4失效;如果c4有范围,则c4后面没有索引列,就没有失效的索引列,从而会使用全部索引。


  • 事例2.2
explain select * from test where c1>'a1' and c2='a2' and c3='a3' and c4='a4';

# 覆盖索引
explain select c1,c2,c3,c4 from test where c1>'a1' and c2='a2' and c3='a3' and c4='a4';

在这里插入图片描述

分析:

  • 如果在c1出使用范围,则type=ALL,key=Null,索引失效,全表扫描,这里违背了最佳左前缀法则,带头大哥已死,因为c1主要用于范围,而不是查询。解决方式:覆盖索引
  • 上述的第二个就是覆盖索引的效果,可以看出type=range,key_len=33,所有只有c1这个索引列生效,但是比使用范围效果好了。

结论:在最佳左前缀法则中,如果最左前列(带头大哥)的索引失效,则后面的索引都失效


  • 事例3
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索引列用在排序过程中。

  • 事例3.1
explain select * from test where c1='a1' and c2='a2' order by c3;

在这里插入图片描述

分析:

  • 从explain的执行结果来看:key_len=66,ref=const,const,从而查找只用到了c1和c2索引,c3索引用于排序。

  • 事例3.2
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。

  • 事例4
explain select * from test where c1='a1' and c5='a5' order by c2,c3;

在这里插入图片描述
分析:

  • 查找只用到了索引c1,c2和c3用于排序,无Using filesort。

  • 事例4.1
explain select * from test where c1='a1' and c5='a5' order by c3,c2;

在这里插入图片描述
分析:

  • 和事例4中的explain的执行结果一样,但是出现了Using filesort,因为索引的后才能关键顺序是c1,c2,c3,c4,但是排序的时候c2和c3颠倒了位置。

  • 事例4.2
explain select * from test where c1='a1' and c2='a2' order by c2,c3;
explain select * from test where c1='a1' and c2='a2' and c5='a5' order by c2,c3;

在这里插入图片描述

分析:

  • 在查询时增加了c5,但是explain的执行结果一样,因为c5并未创建索引。

  • 事例4.3
explain select * from test where c1='a1' and c2='a2' and c5='a5' order by c3,c2;

在这里插入图片描述

分析:

  • 与事例4.1对比,在Extra中并未出现Using filesort,因为c2为常量,在排序中被优化(等值常量可以被优化),所以索引未颠倒,不会出现Using filesort。

  • 事例5
explain select * from test where c1='a1' and c4='a4' order by c2,c3;

在这里插入图片描述

分析:

  • 只用到c1上的索引,因为c4中间间断了,根据最佳左前缀法则,所以key_len=33,ref=const,表示只用到一个索引。

  • 事例5.1
explain select * from test where c1='a1' and c4='a4' order by c3,c2;

在这里插入图片描述

分析:

  • 对比Case 5,在group by时交换了c2和c3的位置,结果出现Using filesort,极度恶劣。原因:c3和c2与索引创建顺序相反。

总结

  1. 最佳左前缀法则。

    1. 在等值查询时,更改索引列顺序,并不会影响explain的执行结果,因为mysql底层会进行优化。

    2. 在使用order by时,注意索引顺序、常量,以及可能会导致Using filesort的情况。

  2. group by容易产生Using temporary。

  3. 通俗理解口诀:

    全值匹配我最爱,最左前缀要遵守;

    带头大哥不能死,中间兄弟不能断;

    索引列上少计算,范围之后全失效;

    LIKE百分写最右,覆盖索引不写星;

    不等空值还有or,索引失效要少用。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值