mysql索引失效的原因
发表于:2025-11-10 作者:千家信息网编辑
千家信息网最后更新 2025年11月10日,小编给大家分享一下mysql索引失效的原因,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!索引对于MySQL而言,是非常重要的篇章。索引知识点也巨多,要想掌握透彻,需要逐个知识点一一击破,
千家信息网最后更新 2025年11月10日mysql索引失效的原因
小编给大家分享一下mysql索引失效的原因,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!
索引对于MySQL而言,是非常重要的篇章。索引知识点也巨多,要想掌握透彻,需要逐个知识点一一击破,今天来先来聊聊哪些情况下会导致索引失效。
图片总结版

相关免费学习推荐:mysql视频教程
全值匹配(索引最佳)
explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '18730658760';
和索引顺序无关,MySQL底层的优化器会进行优化,调整索引的顺序explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '18730658760';
1、违反最左前缀法则
如果索引有多列,要遵守最左前缀法则即查询从索引的最左前列开始并且不跳过索引中的列explain select * from user where age = 20 and phone = '18730658760' and pos = 'cxy';
2、在索引列上做任何操作
如计算、函数、(自动or手动)类型转换等操作,会导致索引失效从而全表扫描explain select * from user where left(name,5) = 'zhangsan' and age = 20 and phone = '18730658760';

3、索引范围条件右边的列
索引范围条件右边的索引列会失效explain select * from user where name = 'zhangsan' and age > 20 and pos = 'cxy';
4、尽量使用覆盖索引
只访问索引查询(索引列和查询列一致),减少select*explain select name,age,pos,phone from user where age = 20;
5、使用不等于(!=、<>)
mysql在使用不等于(!=、<>)的时候无法使用索引会导致全表扫描(除覆盖索引外)explain select * from user where age != 20;explain select * from user where age <> 20;
6、like以通配符开头('%abc')
索引失效explain select * from user where name like '%zhangsan';
索引生效explain select * from user where name like 'zhangsan%';
7、字符串不加单引号索引失效
explain select * from user where name = 2000;
8、or连接
少用orexplain select * from user where name = '2000' or age = 20 or pos ='cxy';
9、order by
正常(索引参与了排序)explain select * from user where name = 'zhangsan' and age = 20 order by age,pos;备注:索引有两个作用:排序和查找
导致额外的文件排序(会降低性能)explain select name,age from user where name = 'zhangsan' order by pos;//违反最左前缀法则explain select name,age from user where name = 'zhangsan' order by pos,age;//违反最左前缀法则explain select * from user where name = 'zhangsan' and age = 20 order by created_time,age;//含非索引字段
10、group by
正常(索引参与了排序)explain select name,age from user where name = 'zhangsan' group by age;备注:分组之前必排序(排序同order by)
导致产生临时表(会降低性能)explain select name,pos from user where name = 'zhangsan' group by pos;//违反最左前缀法则explain select name,age from user where name = 'zhangsan' group by pos,age;//违反最左前缀法则explain select name,age from user where name = 'zhangsan' group by age,created_time;//含非索引字段
使用的示例数据
mysql> show create table user \G****************************************************** Table: userCreate Table: CREATE TABLE `user` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `age` int(10) DEFAULT '0', `pos` varchar(30) DEFAULT NULL, `phone` varchar(11) DEFAULT NULL, `created_time` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_name_age_pos_phone` (`name`,`age`,`pos`,`phone`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
看完了这篇文章,相信你对mysql索引失效的原因有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
索引
前缀
法则
排序
知识
查询
原因
右边
备注
字段
性能
条件
知识点
篇文章
范围
顺序
透彻
重要
一致
两个
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
做软件开发男生内向
业务系统数据库类型怎么查找
软件开发阶段的输出
arma3如何寻找服务器
服务器监控 微信提醒
软件开发纠错
网络安全攻守兼备
mdf是哪个数据库文件扩展名
太原网络技术学校
网络安全博览会在哪里举办
西派网络技术
网站换主机数据库迁移时变小
网络安全黑客比赛
dbe数据库下载
mc中的服务器密码是什么
计算机中数据库关系模式是什么
网站怎么建立数据库
数据库中怎么再添加一列
软件开发需求文档例文
服务器swap分区多大
常州软件开发平均薪资
plsql 导出数据库
用自己的网络安全吗
华为首席网络安全规划
四川设备点检app软件开发
校园网络安全保密制度
中学网络安全情景剧剧本
互联网科技题材基金有那些
国家网络安全学院规划方案
品牌 数据库