MySQL 索引失效的幾種類型以及解決方式


索引失效的情況有哪些?

  1. 索引列不獨立
  2. 使用了左模糊
  3. 使用 or 查詢部分字段沒有使用索引
  4. 字符串條件沒有使用 ''
  5. 不符合最左前綴原則的查詢
  6. 索引字段沒有添加 not null 約束
  7. 隱式轉換導致索引失效

 

索引列不獨立是指 被索引的這列不能是表達式的一部分,不能是函數的參數,比如下面的這種情況

select id,name,age,salary from table_name where salary + 1000 = 6000;

salary 列被用戶表達式的計算了,這種情況下索引就會失效,解決方式就是提前計算好條件值,不要讓索引列參與表達式計算,修改后 sql 如下

select id,name,age,salary from table_name where salary = 5000;

索引字段作為函數的參數

select id,name,age,salary from table_name where substring(name,1,3)= 'luc';

解決方式是什么呢,可以提前計算好條件,不要使用索引,或者可以使用其他的 sql 替換上面的,比如,上面的sql 可以使用 like 來代替

select id,name,age,salary from table_name where name like 'luc%';

使用了左模糊

select id,name,age,salary from table_name where name like '%lucs%';

平時盡可能避免用到左模糊,可以這樣寫

select id,name,age,salary from table_name where name like 'lucs%';

如果實在避免不了左模糊查詢的話,考慮一下搜索引擎 比如 ES

or 查詢部分字段沒有使用索引

select id,name,age,salary from table_name where name ='lucs' and age >25

這種情況,可以為 name 和 age 都建立索引,否則會走全表掃描。

字符串條件沒有使用 ''

select id,name,age,salary from table_name where phone=13088772233 

上面的這條 sql phone 字段類型是 字符串類型的,但是沒有使用 '13088772233 ', SQL 就全表掃描了,所以字符串索引要使用 ‘’

select id,name,age,salary from table_name where phone='13088772233 '

不符合最左前綴原則的查詢

例如有這樣一個組合索引 index(a,b,c)

select * from table_name where b='1'and c='2'
select * from table_name where c='2'

// 上面這兩條 SQL 都是無法走索引執行的

最左原則,就是要最左邊的優先存在,我不在的話,你們自己就玩不動了,除非你自己單獨創立一個索引,下面這幾條 SQL 就可以走索引執行

select * from table_name where a = 'asaa' and b='1'and c='2'
select * from table_name where a = 'asda' and b='1231' 
// 上面這兩條是走索引的,但是下面這條你覺得索引應該怎么走,是全部走,還是部分走索引?
select * from table_name where a = 'asda' and c='dsfsdafsfsd' 

索引字段沒有添加 not null 約束

select * from table_name where a is null;
// 這條sql就無法走索引執行了,is null 條件 不能使用索引,只能全表掃描了
// mysql 官方建議是把字段設置為 not null 

所以針對這個情況,在mysql 創建表字段的時候,可以將需要索引的字符串設置為 not null default '' 默認空字符串即可

隱式轉換

關聯表的兩個字段類型不一致會發生隱式轉換

select * from table_name t1 left join table_name2 t2 on t1.id=t2.tid;
// 上面這條語句里,如果 t1 表的id 類型和 t2 表的tid 類型不一致的時候,就無法
// 按索引執行了。
// 解決方式就是統一設置字段類型。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM