天天看點

一張圖搞懂MySQL的索引失效

圖檔總結版

一張圖搞懂MySQL的索引失效

全值比對(索引最佳)

explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '18730658760';           
一張圖搞懂MySQL的索引失效
和索引順序無關,MySQL底層的優化器會進行優化,調整索引的順序
explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '18730658760';           
一張圖搞懂MySQL的索引失效

1、違反最左字首法則

如果索引有多列,要遵守最左字首法則
即查詢從索引的最左前列開始并且不跳過索引中的列
explain select * from user where age = 20 and phone = '18730658760' and pos = 'cxy';           
一張圖搞懂MySQL的索引失效

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

如計算、函數、(自動or手動)類型轉換等操作,會導緻索引失效進而全表掃描
explain select * from user where left(name,5) = 'zhangsan' and age = 20 and phone = '18730658760';           
一張圖搞懂MySQL的索引失效

3、索引範圍條件右邊的列

索引範圍條件右邊的索引列會失效
explain select * from user where name = 'zhangsan' and age > 20 and pos = 'cxy';           
一張圖搞懂MySQL的索引失效

4、盡量使用覆寫索引

隻通路索引查詢(索引列和查詢列一緻),減少select*
explain select name,age,pos,phone from user where age = 20;           
一張圖搞懂MySQL的索引失效

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

mysql在使用不等于(!=、<>)的時候無法使用索引會導緻全表掃描(除覆寫索引外)
explain select * from user where age != 20;
explain select * from user where age <> 20;           
一張圖搞懂MySQL的索引失效
一張圖搞懂MySQL的索引失效

6、like以通配符開頭('%abc')

索引失效
explain select * from user where name like '%zhangsan';           
一張圖搞懂MySQL的索引失效
索引生效
explain select * from user where name like 'zhangsan%';           
一張圖搞懂MySQL的索引失效

7、字元串不加單引号索引失效

explain select * from user where name = 2000;           
一張圖搞懂MySQL的索引失效

8、or連接配接

少用or
explain select * from user where name = '2000' or age = 20 or pos ='cxy';           
一張圖搞懂MySQL的索引失效

9、order by

正常(索引參與了排序)
explain select * from user where name = 'zhangsan' and age = 20 order by age,pos;
備注:索引有兩個作用:排序和查找           
一張圖搞懂MySQL的索引失效
導緻額外的檔案排序(會降低性能)
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;//含非索引字段           
一張圖搞懂MySQL的索引失效
一張圖搞懂MySQL的索引失效
一張圖搞懂MySQL的索引失效

10、group by

正常(索引參與了排序)
explain select name,age from user where name = 'zhangsan' group by age;
備注:分組之前必排序(排序同order by)           
一張圖搞懂MySQL的索引失效
導緻産生臨時表(會降低性能)
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的索引失效
一張圖搞懂MySQL的索引失效
一張圖搞懂MySQL的索引失效

使用的示例資料

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           

繼續閱讀