当前位置:网站首页>面试突击60:什么情况会导致 MySQL 索引失效?
面试突击60:什么情况会导致 MySQL 索引失效?
2022-06-27 11:31:00 【王磊】
为了验证 MySQL 中哪些情况下会导致索引失效,我们可以借助 explain 执行计划来分析索引失效的具体场景。
explain 使用如下,只需要在查询的 SQL 前面添加上 explain 关键字即可,如下图所示:
而以上查询结果的列中,我们最主要观察 key 这一列,key 这一列表示实际使用的索引,如果为 NULL 则表示未使用索引,反之则使用了索引。
以上所有结果列说明如下:
id — 选择标识符,id 越大优先级越高,越先被执行; select_type — 表示查询的类型; table — 输出结果集的表; partitions — 匹配的分区; type — 表示表的连接类型; possible_keys — 表示查询时,可能使用的索引; key — 表示实际使用的索引; key_len — 索引字段的长度; ref— 列与索引的比较; rows — 大概估算的行数; filtered — 按表条件过滤的行百分比; Extra — 执行情况的描述和说明。
其中最重要的就是 type 字段,type 值类型如下:
all — 扫描全表数据;
index — 遍历索引;
range — 索引范围查找;
index_subquery — 在子查询中使用 ref;
unique_subquery — 在子查询中使用 eq_ref;
ref_or_null — 对 null 进行索引的优化的 ref;
fulltext — 使用全文索引;
ref — 使用非唯一索引查找数据;
eq_ref — 在 join 查询中使用主键或唯一索引关联;
const — 将一个主键放置到 where 后面作为条件查询, MySQL 优化器就能把这次查询优化转化为一个常量,如何转化以及何时转化,这个取决于优化器,这个比 eq_ref 效率高一点。
创建测试表和数据
为了演示和测试那种情况下会导致索引失效,我们先创建一个测试表和相应的数据:
-- 创建表
drop table if exists student;
create table student(
id int primary key auto_increment comment '主键',
sn varchar(32) comment '学号',
name varchar(250) comment '姓名',
age int comment '年龄',
sex bit comment '性别',
address varchar(250) comment '家庭地址',
key idx_address (address),
key idx_sn_name_age (sn,name,age)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 添加测试数据
insert into student(id,sn,name,age,sex,address)
values(1,'cn001','张三',18,1,'高老庄'),
(2,'cn002','李四',20,0,'花果山'),
(3,'cn003','王五',50,1,'水帘洞');当前表中总共有 3 个索引,如下图所示:

PS:本文以下内容基于 MySQL 5.7 InnoDB 数据引擎下。
索引失效情况1:非最左匹配
最左匹配原则指的是,以最左边的为起点字段查询可以使用联合索引,否则将不能使用联合索引。 我们本文的联合索引的字段顺序是 sn + name + age,我们假设它们的顺序是 A + B + C,以下联合索引的使用情况如下:
从上述结果可以看出,如果是以最左边开始匹配的字段都可以使用上联合索引,比如:
A+B+C
A+B
A+C
其中:A 等于字段 sn,B 等于字段 name,C 等于字段 age。
而 B+C 却不能使用到联合索引,这就是最左匹配原则。
索引失效情况2:错误模糊查询
模糊查询 like 的常见用法有 3 种:
模糊匹配后面任意字符:like '张%' 模糊匹配前面任意字符:like '%张' 模糊匹配前后任意字符:like '%张%'
而这 3 种模糊查询中只有第 1 种查询方式可以使用到索引,具体执行结果如下: 
索引失效情况3:列运算
如果索引列使用了运算,那么索引也会失效,如下图所示: 
索引失效情况4:使用函数
查询列如果使用任意 MySQL 提供的函数就会导致索引失效,比如以下列使用了 ifnull 函数之后的执行计划如下: 
索引失效情况5:类型转换
如果索引列存在类型转换,那么也不会走索引,比如 address 为字符串类型,而查询的时候设置了 int 类型的值就会导致索引失效,如下图所示: 
索引失效情况6:使用 is not null
当在查询中使用了 is not null 也会导致索引失效,而 is null 则会正常触发索引的,如下图所示: 
总结
导致 MySQL 索引失效的常见场景有以下 6 种:
联合索引不满足最左匹配原则。 模糊查询最前面的为不确定匹配字符。 索引列参与了运算。 索引列使用了函数。 索引列存在类型转换。 索引列使用 is not null 查询。
是非审之于己,毁誉听之于人,得失安之于数。
公众号:Java面试真题解析
边栏推荐
- Summary of qstype class usage (III)
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用dot.col参数指定分组数据点的颜色
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、解读分析模型
- MapReduce原理剖析(深入源码)
- 2022ciscn central China Web
- Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
- 【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍
- [tcapulusdb knowledge base] Introduction to tcapulusdb system management
- [tcaplusdb knowledge base] Introduction to tcaplusdb tcaplusadmin tool
- 2022CISCN华中 Web
猜你喜欢
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction](/img/a4/c3255ce17516348f703f7f21511555.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction

QStyle类用法总结(二)

Drive to APasS!使用明道云管理F1赛事

MapReduce实战小案例(自定义排序、二次排序、分组、分区)

pull request

0 basic understanding of how e-commerce systems connect with payment channels
![Jerry's DAC output mode setting [chapter]](/img/2e/62bc74e216ed941bd2a0a191db63f0.png)
Jerry's DAC output mode setting [chapter]

Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks

Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
![[tcapulusdb knowledge base] Introduction to tmonitor system upgrade](/img/04/b1194ca3340b23a4fb2091d1b2a44d.png)
[tcapulusdb knowledge base] Introduction to tmonitor system upgrade
随机推荐
Unity Shader学习(二)第一个Shader
QStyle类用法总结(二)
旭日3SDB,安装原版ros
Unity Shader学习(一)认识unity shader基本结构
巅峰小店APP仿站开发玩法模式讲解源码分享
Xuri 3sdb, installing the original ROS
Jerry's constant feeding of dogs will cause frequent switch interruptions leading to timer [chapter]
Jianmu continuous integration platform v2.5.0 release
Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
I.MX6ULL启动方式
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用VGAM包的vglm函数对有序多分类logistic回归模型进行平行性假设作检验
【TcaplusDB知识库】TcaplusDB-tcaplusadmin工具介绍
Summary of qstype class usage (III)
Heap heap sort TOPK
0基础了解电商系统如何对接支付渠道
【TcaplusDB知识库】TcaplusDB系统用户组介绍
pull request
On ticheck
等等, 怎么使用 SetMemoryLimit?
Code for structural design of proe/creo household appliances - electric frying pan