欢迎大家来到IT世界,在知识的湖畔探索吧!
索引对于MySQL而言,是非常重要的篇章。索引知识点也巨多,要想掌握透彻,需要逐个知识点一一击破,今天来先来聊聊哪些情况下会导致索引失效。
图片版
欢迎大家来到IT世界,在知识的湖畔探索吧!
全值匹配(索引最佳)
explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '';欢迎大家来到IT世界,在知识的湖畔探索吧!
索引最佳
欢迎大家来到IT世界,在知识的湖畔探索吧!和索引顺序无关,MySQL底层的优化器会进行优化,调整索引的顺序 explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '';
MySQL底层优化器
1、违反最左前缀法则
如果索引有多列,要遵守最左前缀法则即查询从索引的最左前列开始并且不跳过索引中的列 explain select * from user where age = 20 and phone = '' and pos = 'cxy';
违反最左前缀法则
2、在索引列上做任何操作
欢迎大家来到IT世界,在知识的湖畔探索吧!如计算、函数、(自动or手动)类型转换等操作,会导致索引失效从而全表扫描 explain select * from user where left(name,5) = 'zhangsan' and age = 20 and phone = '';
索引列操作
3、索引范围条件右边的列
索引范围条件右边的索引列会失效 explain select * from user where name = 'zhangsan' and age > 20 and pos = 'cxy';
索引范围条件
4、尽量使用覆盖索引
欢迎大家来到IT世界,在知识的湖畔探索吧!只访问索引查询(索引列和查询列一致),减少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’)
欢迎大家来到IT世界,在知识的湖畔探索吧!索引失效 explain select * from user where name like '%zhangsan';
索引失效
索引生效 explain select * from user where name like 'zhangsan%';
索引生效
7、字符串不加单引号索引失效
欢迎大家来到IT世界,在知识的湖畔探索吧!explain select * from user where name = 2000;
字符串不加单引号
8、or连接
少用or explain select * from user where name = '2000' or age = 20 or pos ='cxy';
or连接
9、order by
欢迎大家来到IT世界,在知识的湖畔探索吧!正常(索引参与了排序) explain select * from user where name = 'zhangsan' and age = 20 order by age,pos;备注:索引有两个作用:排序和查找
order by
导致额外的文件排序(会降低性能) 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
欢迎大家来到IT世界,在知识的湖畔探索吧!正常(索引参与了排序) explain select name,age from user where name = 'zhangsan' group by age;备注:分组之前必排序(排序同order by)
group 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;//含非索引字段
违反最左前缀法则
违反最左前缀法则
含有非索引字段
使用的示例数据
欢迎大家来到IT世界,在知识的湖畔探索吧!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
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/94828.html