mysql innodb index_mysql innodb 索引使用指南

索引相关概念聚簇索引(clustered index)

使用innodb引擎时,每张表都有一个聚簇索引,比如我们设置的主键就是聚簇索引

聚簇是指数据的存储方式,表示数据行和相邻的键值紧凑的储存在一起

特点:查询数据特别快,因为聚簇索引和行数据存储在磁盘的同一页,这样可以减少磁盘I/O操作次数(MySQL 索引 B+Tree)

注意:主键应该尽量简短

二级索引(secondary index)

除了聚簇索引外的其他索引叫做二级索引(辅助索引),比如我们给除主键外其他字段创建的索引

特点:二级索引里面存储了聚簇索引,最后要通过聚簇索引找到行数据。可见,聚簇索引的效率会影响其他索引

覆盖索引(covering index)

索引包含了查询语句需要的所有数据,这种索引称为覆盖索引

特点:索引的叶子节点中已经包含要查询的数据,不需要回表操作所以很快(减少了磁盘I/O操作次数)

组合索引(multiple-column index)

组合索引也称为复合索引(联合索引),是指把多个字段组合起来创建一个索引(最多16个字段)

特点:遵循最左前缀匹配原则

最左前缀匹配原则(leftmost prefix principle)

mysql会从左向右匹配直到遇到不能使用索引的条件(>、

设想用a,b,c字段创建一个组合索引(a,b,c)

由于a是索引的最左边前缀,所以where条件中必须匹配字段a,mysql优化器才会用到这个索引

在匹配字段a的前提下,才能匹配字段b

在匹配字段a的前提下,并且匹配字段b,然后才能匹配字段c

使用explain查看执行计划

explain命令用来查看select语句执行计划,确认该SQL语句有没有使用索引,是否做全表扫描,是否使用覆盖索引等idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra

type:代表数据访问类型(由左至右,由最差到最好)

| All | index | range | ref | eq_ref | const | system | null |

possible_keys:表示哪些索引可能有利于高效的查找

key:显示mysql决定采用哪个索引来优化查询

key_len:显示mysql在索引里使用的字节数

ref:显示了之前的表在key列记录的索引中查找值所用的列或常量

rows:为了找到所需的行大致需要读取的行数

extra:表示额外的信息(左边较差,右边较好)

| Using filesort| Using temporary | Using where | Using index condition | Using index |

Using index:使用了覆盖索引,速度很快,限于查询字段都位于同一个索引中的场景

Using index condition:表示使用了ICP优化(Index Condition Pushdown),能减少引擎层访问基表的次数和MySQL Server访问存储引擎的次数

Using where:表示在存储引擎检索行后mysql服务器再进行过滤

Using filesort:返回结果前需要做一次外部排序(内存或硬盘),速度慢应该尽量避免

Using temporary:在对查询结果排序时会使用一个临时表,速度慢

创建一张测试表

使用InnoDB引擎创建teacher表,id设为自增主键

mobile、name和birthday建立第一个组合索引idx_one(注意三个字段在索引中顺序)

email、age和name字段建立第二个组合索引idx_two(同样注意顺序)CREATE TABLE `teacher` (

`id` int(11) unsigned NOT NULL AUTO_INCREMENT,

`name` varchar(64) DEFAULT NULL,

`birthday` timestamp NULL DEFAULT NULL,

`email` varchar(32) DEFAULT NULL,

`age` int(11) DEFAULT NULL,

`mobile` varchar(16) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `idx_one` (`mobile`,`name`,`birthday`),

KEY `idx_two` (`email`,`age`,`name`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

数据量很少时数据库全表扫描和走索引成本没有太大差别,这时候即使创建了索引并且符合走索引的条件,mysql 却不一定使用索引,下面是十万条初始数据(文章内容不支持添加文件只能到其他地址下载)

表初始数据下载链接

符合最左前缀场景

场景一:mobile是索引的左前缀explain select * from teacher where mobile = '18600660088';

说明:ref列只出现了一个const,说明使用索引的第一列

3dc186dcdae85253083b409769a909d9.png

场景二:mobile和name加起来是索引的左前缀explain select * from teacher where mobile = '18600660088' and name = 'kevin';

说明:ref列出现了两个const,说明使用索引的前缀mobile和name

878bc08966dec343f03bd54a7c8ff287.png

场景三:mobile、name和birthday加起来是索引的左前缀explain select * from teacher where birthday = '2019-01-01' and name = 'kevin' and mobile = '18600660088';

说明:ref列出现了三个const,说明使用索引的第一列、第二列和第三列

注意:mysql优化器会自动调整mobile、name、birthday在查询条件中出现的顺序以匹配索引

7a7d63da00b62bf85f2ba69e458e1e46.png

场景四:只有mobile是前缀,中间跳过了索引中第二列(name),birthday不使用索引explain select age from teacher where mobile = '18600660088' and birthday = '2019-01-01';

说明:ref列只出现了一个const,说明使用索引的前缀mobile部分

491cf71c658ca2a9680ba020d2a7ce8d.png

场景五:mobile和name加起来是索引的前缀,并且%位于模糊查询后缀explain select * from teacher where mobile = '18600660088' and name like 'kevin%';

说明:key_len是246与场景二一致,只使用了索引的前缀部分

0c29dd8081275e8496687e9095ffd855.png

场景六:mobile和name加起来是索引的前缀,并且%位于模糊查询的前缀explain select * from teacher where mobile = '18600660088' and name like '%kevin';

说明:mobile字段用到了索引,name不使用索引

f7246b68bb33b63b121b56708f4f8d29.png

场景七:mobile是索引的最左前缀,并且使用了范围查询explain select * from teacher where mobile > '18600660088' and name = 'kevin' and birthday = '2019-01-01';

说明:key_len是51与场景六一致,只使用了索引前缀mobile,name和birthday不使用索引

结论:索引从左往右匹配,遇到范围查询后停止匹配

ba2d5f2b311d1f9f6a8c72b0cd764dc5.png

场景八:name位于组合索引的中间,并且%位于模糊查询后缀explain select * from teacher where mobile = '18600660088' and name like 'kevin%' and birthday = '2019-01-01';

说明:key_len显示251说明跟场景三一致,使用到了整个组合索引

结论:%位于模糊查询后缀不影响索引的使用,如果是组合索引可以继续往右匹配

acf0dc2c165489e2893ee9f42ac427ca.png

不使用索引的场景

场景一:缺少前缀mobile字段explain select * from teacher where name = 'kevin chen';

说明:type列显示ALL表示全表扫描,MySQL 从头到尾扫描整张表查找行

d6f88363f08215a2e8a32b229c998b47.png

场景二:缺少mobile和name组合的前缀字段explain select * from teacher where birthday = '2019-01-01';

说明:type列显示ALL表示全表扫描,MySQL从头到尾扫描整张表查找行

764940ac2a01bb3fe8db246513bf5fd0.png

场景三:like模糊匹配%位于前缀explain select * from teacher where mobile like '%18600660088';

说明:type列显示ALL表示全表扫描,MySQL从头到尾扫描整张表查找行

66c6a0ce9bdce266c01ad8b79c4db5f6.png

场景四:索引列进行了函数运算explain select * from teacher where trim(mobile) = '18600660088';

说明:正确的做法是在等号的右边做数据运算或函数运算

5788055548fd1fc5fb2b10fbbedb16e0.png

场景五:字段类型不匹配explain select * from teacher where mobile = 18600660088;

说明:mobile是varchar类型,18600660088是整数

51882d414ddc287af338ba3932087fa2.png

覆盖索引

场景一:需要的数据都在索引中,走覆盖索引explain select mobile,name,birthday from teacher where mobile = '18600660088';

说明:Extra列显示附加信息,Using index表示使用覆盖索引

43842e80ba22a593bfeddf918e67f3de.png

场景二:查询的age字段不在索引中,不能使用覆盖索引explain select age from teacher where mobile = '18600660088';

97518e257a44332a5e2e65e419d0f59b.png

使用索引排序

场景一:查询字段和排序字段是同一个索引explain select id,mobile,name,birthday from teacher order by mobile,name,birthday;

说明:extra显示Using index表示使用了覆盖索引,二级索引中隐含了聚簇索引(主键)

50cad1859c30d56a63bd3e975e1901ad.png

场景二:多个排序字段位于多个不同索引explain select * from teacher order by mobile, email;

说明:mobil和email属于不同索引,Using filesort说明使用外部排序,不能用索引排序

f7bd2de32dad0d774e749229ee594556.png

场景三:多个排序字段不符合最左前缀匹配原则explain select id,mobile,name,birthday from teacher order by mobile, birthday;

说明:查询用了索引,排序跳过了组合索引中间字段name,extra显示Using filesort

f5b05675edf03ea9f8417b25eb3eab68.png

场景四:查询含索引外字段,用索引扫描后再回表查询比直接扫表成本更高,所以没使用索引explain select * from teacher order by mobile,name,birthday;

说明:extra显示Using filesort表示使用了外部排序

09e30b3034804974b77fef2814c8bafc.png

场景五:查询条件字段和排序字段组合起来符合索引最左前缀explain select * from teacher where mobile='18600660088' order by name;

e4b90b5b2001f4e189971ecf3c8a1549.png

使用索引分组

场景一:分组字段(多字段组合)属于索引最左前缀explain select email, age, name from teacher group by email, age, name;

说明:email、age和name组合起来符合最左前缀,使用索引idx_two,extra显示Using index

7b6fe795a5769cfebd6a8e603aa2eabc.pngexplain select distinct email, age, name from teacher;

说明:这里distinct字段组合起来同样符合索引最左前缀,使用索引idx_two

dd7387d22d61332418038261d486096c.png

场景二:min()/max()函数作用于同一列,并且紧跟属于同一索引的分组字段explain select email, min(age), max(age) from teacher group by email;

说明:email是分组字段,age是函数作用字段,email和age组合起来符合idx_two最左前缀

993d6ae178f9861db7cfae97c6a79eb9.png

场景三:count(distinct)、avg(distinct)和sum(distinct)组合起来符合最左前缀explain select count(distinct email), sum(distinct age) from teacher;

说明:avg(distinct)和sum(distinct)中distinct只适用单个字段

6c62cac40e6b48c147d76cc0f875b118.png

场景四:count(distinct),distinct适用于多个字段explain select count(distinct email, age) from teacher;

说明:extra显示Using index for group-by说明使用松散索引扫描(Loose Index Scan)

bb26a90f875f288755a139c54f1cc75a.png

场景五:缺少组合索引中间部分,不能使用索引排序explain select email, name from teacher group by email, name;

说明:分组字段缺少idx_two索引age部分,extra显示Using filesort说明使用外部排序

377b0690d1a9047f897f577193cae462.png

场景六:多个分组字段不属于同一个索引explain select email, age, birthday from teacher group by email, age, birthday;

说明:birthday不属于idx_two索引,显示Using filesort

b37e66bcb13ed8b67268df5873fe1234.png

场景七:紧凑索引扫描(Tight Index Scan)explain select email, age, name from teacher where age = 18 group by email, name;

说明:分组字段缺少了完整索引中间部分,但由查询条件 age = 18 补充了这部分常量

5fe90e306bbc76b3632b234c3df79ee5.png

场景八:紧凑索引扫描(Tight Index Scan)explain select email, age, name from teacher where email = 'kevin@qq.com' group by age, name;

说明:分组字段不以索引最左前缀开始,但查询条件 email='kevin@qq.com' 提供了这部分常量

0afea478479bc1c2eb1e99a111b9df1c.png

处理从右到左匹配的需求

如果查询条件需要处理从右到左匹配,可以有两种处理方案表结构新增一列用来存储需要从右到左匹配列的倒序字符并构建索引(新增列和索引都需要占用磁盘空间)

Mysql 5.7 版本提供了虚拟列功能,使用 reverse 函数对需要从右到左匹配的列构建虚拟列并创建索引

teacher 表新增虚拟列 name_reverse,按照 name 列字符倒序计算得到,并创建索引alter table teacher add column name_reverse varchar(20) generated always as (reverse(name));

alter table teacher add index idx_name_reverse(name_reverse);

说明:Generated Column 是 MySQL 5.7 引入的新特性,所谓 Cenerated Column 是数据库中这一列由其他列计算而得,Cenerated Column 支持 Virtual Column 与 Stored Column 两种类型,前者只将 Generated Colum n保存在数据字典中(表的元数据)并不会将这一列数据持久化到磁盘上;后者会将 Generated Column 持久化到磁盘上,而不是每次读取的时候计算所得,后者需要更多的磁盘空间

参考资料

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值