当前位置:网站首页>MySQL 索引失效的几种类型以及解决方式
MySQL 索引失效的几种类型以及解决方式
2022-06-29 10:23:00 【民工哥】
点击下方“Java编程鸭”关注并标星
更多精彩 第一时间直达
索引失效的情况有哪些?
索引列不独立
使用了左模糊
使用 or 查询部分字段没有使用索引
字符串条件没有使用 ''
不符合最左前缀原则的查询
索引字段没有添加 not null 约束
隐式转换导致索引失效
索引列不独立是指被索引的这列不能是表达式的一部分,不能是函数的参数,比如下面的这种情况
select id,name,age,salary from table_name where salary + 1000 = 6000;salary 列被用户表达式的计算了,这种情况下索引就会失效,解决方式就是提前计算好条件值,不要让索引列参与表达式计算。
索引字段作为函数的参数
select id,name,age,salary from table_name where substring(name,1,3)= 'luc';解决方式是什么呢,可以提前计算好条件,不要使用索引,或者可以使用其他的 sql 替换上面的,比如,上面的sql 可以使用 like 来代替
select id,name,age,salary from table_name where name like 'luc%';使用了左模糊
select id,name,age,salary from table_name where name like '%lucs%';平时尽可能避免用到左模糊,可以这样写
select id,name,age,salary from table_name where name like 'lucs%';如果实在避免不了左模糊查询的话,考虑一下搜索引擎 比如 ES
or 查询部分字段没有使用索引
select id,name,age,salary from table_name where name ='lucs' and age >25这种情况,可以为 name 和 age 都建立索引,否则会走全表扫描。
字符串条件没有使用 ''
select id,name,age,salary from table_name where phone=13088772233上面的这条 sql phone 字段类型是 字符串类型的,但是没有使用 '13088772233 ', SQL 就全表扫描了,所以字符串索引要使用 ‘’
select id,name,age,salary from table_name where phone='13088772233 '不符合最左前缀原则的查询
例如有这样一个组合索引 index(a,b,c)
select * from table_name where b='1'and c='2'
select * from table_name where c='2'
// 上面这两条 SQL 都是无法走索引执行的最左原则,就是要最左边的优先存在,我不在的话,你们自己就玩不动了,除非你自己单独创立一个索引,下面这几条 SQL 就可以走索引执行
select * from table_name where a = 'asaa' and b='1'and c='2'
select * from table_name where a = 'asda' and b='1231'
// 上面这两条是走索引的,但是下面这条你觉得索引应该怎么走,是全部走,还是部分走索引?
select * from table_name where a = 'asda' and c='dsfsdafsfsd'索引字段没有添加 not null 约束
select * from table_name where a is null;
// 这条sql就无法走索引执行了,is null 条件 不能使用索引,只能全表扫描了
// mysql 官方建议是把字段设置为 not null所以针对这个情况,在mysql 创建表字段的时候,可以将需要索引的字符串设置为 not null default '' 默认空字符串即可
隐式转换
关联表的两个字段类型不一致会发生隐式转换
select * from table_name t1 left join table_name2 t2 on t1.id=t2.tid;
// 上面这条语句里,如果 t1 表的id 类型和 t2 表的tid 类型不一致的时候,就无法
// 按索引执行了。
// 解决方式就是统一设置字段类型。END
看完本文有收获?请转发分享给更多人
关注「Java编程鸭」,提升Java技能
关注Java编程鸭微信公众号,后台回复:码农大礼包 可以获取最新整理的技术资料一份。涵盖Java 框架学习、架构师学习等!
文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)边栏推荐
- (JS)模仿一个instanceof方法
- 毕业季·进击的技术er - 职场打工人
- What are the pop, push, unshift, and shift of the (JS) array?
- 在编写shell脚本时如何正确姿势地管理临时文件
- (JS)数组排平(flat)
- (JS)数组中纯函数
- map合并相同的键,值合并为列表
- The use of variables in shell that you have to be familiar with
- Xiaomi mobile phone - Unlock BL + open root permission
- How to properly manage temporary files when writing shell scripts
猜你喜欢

ModbusTCP协议网络学习型单路红外模块(双层板)

《Datawhale推荐系统教程》来了!

多线程高并发服务器:3个问题

历史上的今天:马斯克出生;微软推出 Office 365;蔡氏电路的发明者出生

Bs-gx-017 online examination management system based on SSM

Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Version 2) 全球生态系统数据集

math_ Mathematical expression & deformation of equation equation & accumulation of combined operation skills / means

Modbus RTU 协议485学习型2路红外模块

容器平台性能如何测试,稳定性、扩展效率、组件性能
![[various * * question series] what are OLTP and OLAP?](/img/3f/48b4108d14d40212f678971c1b62bb.png)
[various * * question series] what are OLTP and OLAP?
随机推荐
EasyDSS部署在C盘,录像回看无法正常播放该如何解决?
容器平台性能如何测试,稳定性、扩展效率、组件性能
Nuc980 open source project 16- start from SPI flash (w25q128)
(JS)观察者模式
(JS)手写深拷贝
BS-GX-018 基于SSM实现在校学生考试系统
Here comes the tutorial of datawhale recommendation system!
Dropout layer
西门子S7-200SMART控制步进电机的具体方法及示例程序
Modbus RTU 协议485学习型2路红外模块
[various * * question series] what are OLTP and OLAP?
世界上第一个“半机械人”去世,改造自己只为“逆天改命”
Mysql查询表字段信息
Shell quotation marks and escape are rarely noticed, but they are often used in writing scripts
Bs-gx-018 student examination system based on SSM
(JS)数组方法:slice和splice
The encryption market has exploded one after another. Can Celsius avoid bankruptcy?
Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career
【每日3题(1)】判断国际象棋棋盘中一个格子的颜色
[untitled] I am plotting a big event