一句口诀教你辨别索引失效七大场景

本文分享自华为云社区《虚竹哥教你一句口诀辨别索引失效七大场景》,作者:小虚竹 。
一、口诀
虚竹哥教你一句功法口诀:模 型 数 或 运 最 快
二、初始化数据
创建存储引擎为 InnoDB 的学生表
插入 100 万条数据,这里使用了存储过程,进行批量提交数据,先关闭自动提交,插入一定条数再进行提交。

三、口诀详解
模
like 模糊全匹配(like ‘%内容 %’),会导致全表扫描;like 模糊左匹配(like ‘%内容’),会导致全表扫描。
实战验证
查看 student 表的索引

对 student_name 字段添加索引
再查看 student 表的索引

测试:like 右匹配是可以命中索引的

解析出来的 type 级别是 range
当查询条件使用索引检索某个范围的数据,典型的场景为使用=、<>、>、>=、<、<=、IS [NOT] NULL、<=>、BETWEEN AND 或者 IN 操作符时,类型为 range
like 左匹配:

解析出来的 type 级别是 index
查询条件中的字段包含索引中的字段(含有非索引字段,就会是 ALL 了),此时只需要扫描索引树。也是全表扫描的。
like 完全匹配:

解析出来的 type 级别是 index
查询条件中的字段包含索引中的字段(含有非索引字段,就会是 ALL 了),此时只需要扫描索引树。也是全表扫描的。
测试结论
模 字决索引失效成立。
型
代表数据类型。例如对字符串 name 字段加的索引,where 条件写 name=1,索引会失效。
实战验证
查看 student 表的索引

对 student_name 字段添加索引
再查看 student 表的索引

测试:

解析出来的 type 级别是 index
查询条件中的字段包含索引中的字段(含有非索引字段,就会是 ALL 了),此时只需要扫描索引树。也是全表扫描的。
测试结论
型 字决索引失效成立。
数
是函数的意思。对索引的字段使用内部函数,索引也会失效。这种情况下应该建立基于函数的索引。
SELECT * FROM
user
WHERE DATE(create_time) = ‘2020-09-03’;这里使用 DATE 函数
实战验证
查看 student 表的索引

对 create_time 字段添加索引
再查看 student 表的索引

测试:

解析出来的 type 级别是 index
查询条件中的字段包含索引中的字段(含有非索引字段,就会是 ALL 了),此时只需要扫描索引树。也是全表扫描的。
直接查 create_time 字段是可以的:

解析出来的 type 级别是 ref
当查询语句中的连接条件或者查询条件使用的索引不是主键和非空唯一索引,或者只是一个索引的一部分,则 type 的取值为 ref
测试结论
数 字决索引失效成立。
或
在 where 子句中使用 or 来连接条件,如果一个字段有索引,一个字段没有索引,将导致引擎放弃使用索引而进行全表扫描;
实战验证
查看 student 表的索引

对 order_num 字段添加索引
再查看 student 表的索引

测试:

测试结论
或 字决索引失效成立。
运
对索引的列进行运算,索引失效,例如:WHERE age+1=8;
实战验证
查看 student 表的索引

对 age 字段添加索引
再查看 student 表的索引

测试:

解析出来的 type 级别是 index
查询条件中的字段包含索引中的字段(含有非索引字段,就会是 ALL 了),此时只需要扫描索引树。也是全表扫描的。
测试结论
运 字决索引失效成立。
最
组合索引,查询时的条件列不是联合索引中的第一个列,索引失效(索引的最左原则)。
实战验证
查看 student 表的索引

对 student_name,age,gender 字段添加组合索引
再查看 student 表的索引

测试:查询条件中包含索引的第一列,索引生效:

解析出来的 type 级别是 ref
当查询语句中的连接条件或者查询条件使用的索引不是主键和非空唯一索引,或者只是一个索引的一部分,则 type 的取值为 ref
索引生效,查询条件中包含索引的第一列,其他排列组合,大家可自行体验下。查询条件中不包含索引的第一列,索引不生效:

测试结论
最 字决索引失效成立。
快
查询数量是超过表的一部分,mysql30%,oracle 20%(这个数据可能不准确,不是官方说明,仅供参考),导致索引失效;
实战验证

对 create_time 字段添加索引
再查看 student 表的索引

测试:查出来的数据量少,可命中索引:

解析出来的 type 级别是 range
当查询条件使用索引检索某个范围的数据,典型的场景为使用=、<>、>、>=、<、<=、IS [NOT] NULL、<=>、BETWEEN AND 或者 IN 操作符时,类型为 range。
查询出来的数据量多,会直接走全表:

测试结论
快 字决索引失效成立。
版权声明: 本文为 InfoQ 作者【华为云开发者联盟】的原创文章。
原文链接:【http://xie.infoq.cn/article/8c8c6f184c6b87676c99a372d】。文章转载请联系作者。
评论