当前位置: 首页 > news >正文

一张图搞懂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连接

少用or explain 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: user Create 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

http://www.jsqmd.com/news/1069603/

相关文章:

  • 【Canal】Canal 是如何处理 DDL(数据定义语言,如 CREATE/ALTER/DROP)事件的?客户端能收到 DDL 变更吗?
  • 白嫖薅羊毛免费算力 启智社区(OpenI)50点卡(优惠卡有50卡时)的方法 支持各个国产算力卡 和nvidia的卡
  • 苹果自带的剪切板竟然出这么多功能了?
  • 2026市面上目前扫码点餐小程序点餐系统口碑好的有哪些?实测推荐来啦
  • 【Canal】 Canal 内部是如何管理多个数据库实例(instance)的?一个 Server 能同时监听多个 MySQL 吗?
  • J语言绘图初步
  • 如何用biliTickerBuy轻松搞定B站会员购抢票难题:3步实现自动化购票
  • 多工作流融合 x10 倍效率提升:多模型 Agent 编排 + Hooks Loop
  • Python爬虫实战:爬虫监控与告警系统——让爬虫7×24小时稳定运行
  • 恒玄bes2800bp用于智能眼镜/手表项目
  • Gitnuro终极指南:跨平台Git客户端快速上手教程
  • Android Framework深度剖析:startActivity的完整执行流程与源码解析
  • Jenkins前端打包构建老项目拯救指南
  • 阿里云短信服务skill实操|如何用 openclaw 一句话发短信?
  • 3分钟掌握SiYuan知识管理的5个核心技巧
  • 鸿蒙 ArkUI 布局与基础语法综合总结
  • 跟AI学一手之虚拟滚动
  • 基于Linux IIO/ADC 子系统的MQ-7 一氧化碳(CO)气体传感器调试
  • 终极console-powers样式系统完全指南:10个技巧打造彩色控制台输出
  • 学成在线--day02 CMS前端开发(含Vue基础知识得回顾)
  • 【Python 打印九九乘法表】
  • 测试体系与测试方案设计
  • ELF3 的人形机器人关节,为什么不只是“电机更大”?
  • 经典管理效应-近因效应
  • *比分网websocket逆向分析
  • 大模型应用开发教程
  • WezTerm终端模拟器:技术原理与配置优化实践
  • 【YOLO 训练专用】安防数据集
  • 不会 MCP?用 Spring AI 一步搞定 Server 实现
  • Windows 10 局域网跨电脑共享文件 - 图文版