当前位置:网站首页>面试突击60:什么情况会导致 MySQL 索引失效?
面试突击60:什么情况会导致 MySQL 索引失效?
2022-06-27 10:53:00 【InfoQ】

- id — 选择标识符,id 越大优先级越高,越先被执行;
- select_type — 表示查询的类型;
- table — 输出结果集的表;
- partitions — 匹配的分区;
- type — 表示表的连接类型;
- possible_keys — 表示查询时,可能使用的索引;
- key — 表示实际使用的索引;
- key_len — 索引字段的长度;
- ref— 列与索引的比较;
- rows — 大概估算的行数;
- filtered — 按表条件过滤的行百分比;
- Extra — 执行情况的描述和说明。
- 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,'水帘洞');

索引失效情况1:非最左匹配

- A+B+C
- A+B
- A+C
- 其中:A 等于字段 sn,B 等于字段 name,C 等于字段 age。
索引失效情况2:错误模糊查询
- 模糊匹配后面任意字符:like '张%'
- 模糊匹配前面任意字符:like '%张'
- 模糊匹配前后任意字符:like '%张%'

索引失效情况3:列运算

索引失效情况4:使用函数

索引失效情况5:类型转换

索引失效情况6:使用 is not null

总结
- 联合索引不满足最左匹配原则。
- 模糊查询最前面的为不确定匹配字符。
- 索引列参与了运算。
- 索引列使用了函数。
- 索引列存在类型转换。
- 索引列使用 is not null 查询。
边栏推荐
- Test how students participate in codereview
- 堆-堆排序-TopK
- 3D mobile translate3d
- Working at home is more tiring than going to work at the company| Community essay solicitation
- oracle触发器 存储过程同时写入
- Naacl 2022 | TAMT: search the transportable Bert subnet through downstream task independent mask training
- Proe/Creo家电产品结构设计规范-电煎锅
- Privacy computing fat offline prediction
- [tcapulusdb knowledge base] Introduction to tmonitor background one click installation (II)
- Institute of Microbiology, Chinese Academy of Sciences recruited 20 young PI, with a resettlement fee of 2million yuan and a start-up fund of 10million yuan (long-term effective)
猜你喜欢
User authentication technology
Learning notes - data set generation
What basic functions are required for live e-commerce application development? What is the future development prospect?
oracle触发器 存储过程同时写入
【TcaplusDB知识库】Tmonitor单机安装指引介绍(二)
Glide caching mechanism
Leetcode 729. 我的日程安排表 I(牛逼,已解决)
iMeta:高颜值绘图网站imageGP+视频教程合集,已被引360次(220625更新)
Design and Simulation of direct torque control system for induction motor (motion control matlab/simulink)
How to deploy jupyterlab in methodot?
随机推荐
Basic violin plot in R with plot
iMeta:高颜值绘图网站imageGP+视频教程合集,已被引360次(220625更新)
go-zero微服务实战系列(七、请求量这么高该如何优化)
C any() and aii() methods
实验笔记之——CARMEN (.log .clf)文件转换为rosbag
C apprentissage des langues - jour 12.
3D mobile translate3d
.NET6接入Skywalking链路追踪完整流程
[cloud enjoys freshness] community weekly · vol.68- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + business realization
Experiment notes - Convert Carmen (.Log.Clf) file to rosbag
堆-堆排序-TopK
Array object in JS
3D移动 translate3d
What is the experience of telecommuting in a foreign company| Community essay solicitation
C语言学习-Day_06
Ubuntu manually installing MySQL
Working at home is more tiring than going to work at the company| Community essay solicitation
audiotrack与audioflinger
浅析基于边缘计算的移动AR实现(中)
ECMAScript 6(es6)