mysql group 索引失效_一张图搞懂MySQL的索引失效

mysql group 索引失效_一张图搞懂MySQL的索引失效_第1张图片

全值匹配(索引最佳)

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

f4ff1d71fbfac466f01252185020d73e.png

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

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

07776a2dc3390b2e93d065ab0240fbcb.png

1、违反最左前缀法则

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

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

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

5bf66698a092ff3a83df0e098120062e.png

2、在索引列上做任何操作

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

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

b9f6df62120a70ef4d7551d7dcbec846.png

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

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

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

337a49221e151d7f03c0ba6180a49727.png

4、尽量使用覆盖索引

只访问索引查询(索引列和查询列一致),减少select*

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

5a77134b11a48b466949a94625a30ec2.png

5、使用不等于(!=、<>)

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

explain select * from user where age != 20;

explain select * from user where age <> 20;

c3e59d7b22ebd9000d212dc12ab4e191.png

1bcb462e638ddffed2a9618593c3858f.png

6、like以通配符开头('%abc')

索引失效

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

871197b55ec01f2e5815aef94c40fc3f.png

索引生效

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

9a873ba91416c0c60075e652638142e1.png

7、字符串不加单引号索引失效

explain select * from user where name = 2000;

4fe9178903b0dbe9fef43561247d9387.png

8、or连接

少用or

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

ef343db3b6b48a9e768db0632b412f8e.png

9、order by

正常(索引参与了排序)

explain select * from user where name = 'zhangsan' and age = 20 order by age,pos;

备注:索引有两个作用:排序和查找

879796c8e57eec5c1e0922d1505551e3.png

导致额外的文件排序(会降低性能)

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

8a09c0d8e0d16cf680277db47add183f.png

07117bb49dc31886e6601335dfd49bf9.png

5dde102e954460373b95414ceb9a0cce.png

10、group by

正常(索引参与了排序)

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

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

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

9f65d6ae5ebbb35ca0e8d0e619c0e538.png

73eaaf984a8c4cafe905fc91f3c1305f.png

34dbaf1ea520ea85410d82d2d64fe7f8.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

你可能感兴趣的:(mysql,group,索引失效)