当前位置:网站首页>别再问我MySQL为啥没走索引?就这几种原因,全都告诉你
别再问我MySQL为啥没走索引?就这几种原因,全都告诉你
2022-07-23 14:46:00 【肥肥技术宅】
工作中,经常遇到这样的问题,我明明在MySQL表上面加了索引,为什么执行SQL查询的时候却没有用到索引?
同一条SQL有时候查询用到了索引,有时候却没用到索引,这是咋回事?
原因可能是索引失效了,失效的原因有以下几种,看你有没有踩过类似的坑?
1. 数据准备:
有这么一张用户表,在name字段上建个索引:
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) DEFAULT NULL COMMENT '姓名',
`age` int DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB COMMENT='用户表';
复制代码2. Explain详解:
想要查看一条SQL是否用到索引?用到了哪种类型的索引?
可以使用explain关键字,查看SQL执行计划。例如:
explain select * from user where id=1;
复制代码
可以看到type=const,表示使用了主键索引。
explain的所有type类型如下:

3. 失效原因
1. 数据类型隐式转换
name字段是varchar类型,如果我们使用数据类型查询,就会产生数据类型转换,虽然不会报错,但是无法用到索引。
explain select * from user where name='一灯';
复制代码
explain select * from user where name=18;
复制代码
2. 模糊查询 like 以%开头
explain select * from user where name like '张%';
复制代码
explain select * from user where name like '%张';
复制代码
3. or前后没有同时使用索引
虽然name字段上加了索引,但是age字段没有索引,使用or的时候会全表扫描。
# or前后没有同时使用索引,导致全表扫描
explain select * from user where name='一灯' or age=18;
复制代码
4. 联合索引,没有使用第一列索引
如果我们在(name,age)上,建立联合索引,但是查询条件中只用到了age字段,也是无法用到索引的。
使用联合索引,必须遵循最左匹配原则,首先使用第一列字段,然后使用第二列字段。
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) DEFAULT NULL COMMENT '姓名',
`age` int DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`),
KEY `idx_name_age` (`name`,`age`)
) ENGINE=InnoDB COMMENT='用户表';
复制代码
5. 在索引字段进行计算操作
如果我们在索引列进行了计算操作,也是无法用到索引的。
# 在主键索引上进行计算操作,导致全表扫描
explain select * from user where id+1=2;
复制代码
6. 在索引字段字段上使用函数
如果我们在索引列使用函数,也是无法用到索引的。

7. 优化器选错索引
同一条SQL有时候查询用到了索引,有时候却没用到索引,这是咋回事?
这可能是优化器选择的结果,会根据表中数据量选择是否使用索引。

当表中大部分name都是一灯,这时候用name='一灯'做查询,还会不会用到索引呢?
索引优化器会认为,用索引还不如全表扫描来得快,干脆不用索引了。

当然我们认为优化器优化的不对,也可以使用force index强制使用索引。

知识点总结:


边栏推荐
- 12张图+6K字图解ZGC垃圾回收器及调优技巧
- Explication détaillée de l'injection aveugle d'erreur SQL
- 简单了解首个 EVM 等效的 zkEVM Polygon 为何全力押注
- 程序环境和预处理
- Ros2 self study notes: rqt visualization tool
- Food safety | eight things you must know when choosing probiotic products
- Investment and finance report this week: Web3 game bear market attracts gold
- 软件质量体系之思
- Pymoo学习 (3):使用多目标优化找到最优解的集合
- AXI interconnect IP核的说明及用法
猜你喜欢

转账业务追加日志(事务的传播行为).

In depth understanding of USB communication protocol

Function secondary development / plug-in development of JMeter (detailed version)

Shrimp noodles: what do you know about the JVM memory layout?

封玩家IP和机器码以及解开被封的教程

When does MySQL use table locks and row locks?

Food safety chocolate is also true or false? How much do you know about it

Pyinstaller+installforge multi file project software packaging

12张图+6K字图解ZGC垃圾回收器及调优技巧

Pyinstaller+InstallForge多文件项目软件打包
随机推荐
59. General knowledge of lightning safety
Pymoo learning (3): use multi-objective optimization to find the set of optimal solutions
Shrimp noodles: what do you know about the JVM memory layout?
程序环境和预处理
Pymoo learning (2): Bi objective optimization problems with constraints
PPPoE协议讲解以及拨号过程Wireshark抓包解析
[introduction series of redis] redis builds master-slave servers
IR drop, EM, noise and antenna
软件质量体系之思
使用 Preparedstatement 选择和显示记录的 JDBC 程序
Pymoo学习 (2):带约束的双目标优化问题
Compressed storage of arrays and special matrices
nVisual综合布线管理软件与网管软件的区别
Typescript 清空数组
程序员最想干的三件事 |漫画
Mysql: MySQL problem that is not a MySQL problem
quota命令详细拓展使用方法,RHEL 7中quota命令搭载方法!磁盘容量配额!
Opencv finding the intersection of two regions
Food safety | eight things you must know when choosing probiotic products
MySQL:不是MySQL问题的MySQL问题