mysql group 索引失效_介绍mysql索引失效的情况

本文介绍了MySQL中导致索引失效的各种情况,包括违反最左前缀法则、在索引列上进行操作、使用不等于操作符、LIKE通配符开头、字符串不加单引号、OR连接、ORDER BY和GROUP BY的不当使用等。理解这些情况有助于优化查询并提升数据库性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysql视频教程栏目索引失效的情况。

276f5213f9d2d335c58c0109dab7aca7.png索引对于MySQL而言,是非常重要的篇章。索引知识点也巨多,要想掌握透彻,需要逐个知识点一一击破,今天来先来聊聊哪些情况下会导致索引失效。

图片总结版

ffec5443f698ac08548f572c64b3aac8.png相关免费学习推荐:mysql视频教程

全值匹配(索引最佳)explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '18730658760';

da28410a84b1450a15efb7a326474c41.png和索引顺序无关,MySQL底层的优化器会进行优化,调整索引的顺序

explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '18730658760';

c481c70f0466bdc73fd77925830bb6ff.png

1、违反最左前缀法则如果索引有多列,要遵守最左前缀法则

即查询从索引的最左前列开始并且不跳过索引中的列

explain select * from user where age = 20 and phone = '18730658760' and pos = 'cxy';

4fed162d8028f9cda128a0790383c1b7.png

2、在索引列上做任何操作如计算、函数、(自动or手动)类型转换等操作,会导致索引失效从而全表扫描

explain select * from user where left(name,5) = 'zhangsan' and age = 20 and phone = '18730658760';

448f84330d206ea3f7860a5d131b3db1.png

3、索引范围条件右边的列索引范围条件右边的索引列会失效

explain select * from user where name = 'zhangsan' and age > 20 and pos = 'cxy';

59b73ad7cecfdb0d45b70af2271b8f0b.png

4、尽量使用覆盖索引只访问索引查询(索引列和查询列一致),减少select*

explain select name,age,pos,phone from user where age = 20;

24d3774e6b5e8e11054b94a57462a44e.png

5、使用不等于(!=、<>)mysql在使用不等于(!=、<>)的时候无法使用索引会导致全表扫描(除覆盖索引外)

explain select * from user where age != 20;

explain select * from user where age <> 20;

e742e861d840a0f8fc91eece36710a2a.png

0b3f65836a3f54000a8fb94c2da4fbbb.png

6、like以通配符开头('%abc')索引失效

explain select * from user where name like '%zhangsan';

cda750084dc729ff6953134334fc966f.png索引生效

explain select * from user where name like 'zhangsan%';

5be1033a58f5c8b98ec04326f1f8f098.png

7、字符串不加单引号索引失效explain select * from user where name = 2000;

7edd68f8d1be58ac4d8967d25caf3b59.png

8、or连接少用or

explain select * from user where name = '2000' or age = 20 or pos ='cxy';

2e3f617f697a6ed28d681d3701db2952.png

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;//含非索引字段

508b24a1ece2aae425d1fd8b667f6d2e.png

a2078a9f29bdd4d9ad0f04559bd9c990.png

ca2f4dd6d2dd197375cafa494c211015.png

10、group by正常(索引参与了排序)

explain select name,age from user where name = 'zhangsan' group by age;

备注:分组之前必排序(排序同order by)

22b9be982eac7aababa2e2b2d30f52c4.png导致产生临时表(会降低性能)

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;//含非索引字段

57baa3f467b1b1ffe0d3a964826d2ae3.png

59132431c7eae8fac20bb4dc7806bdfd.png

4c3e709170d7e1931ec3016f134d0422.png

使用的示例数据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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值