mysql索引失效 常見mysql索引失效條件

2021-10-11 16:09:35 字數 1157 閱讀 2387

使用索引的一般語句:

1、 where條件中有or,除非or的所有欄位都有索引,只要有乙個沒有索引,就不走索引

explain select * from jf_user ju where ju.user_id='***' or ju.superior1='yyy',user_id是主鍵,superior1是普通索引,結果如下

explain select * from jf_user ju where ju.user_id='***' or ju.user_mobile='zzz';user_id是主鍵,user_mobile沒有索引

2、對於多列查詢,有一列使用索引,其他列沒有索引,用 and 關鍵字會使用索引查詢

explain select * from jf_user ju where ju.user_id='***' and ju.user_mobile='mmm';

3、like查詢以%開頭不會使用索引

explain select * from jf_user ju where ju.user_id like '***%';user_id主鍵

explain select * from jf_user ju where ju.user_id like '%317%';user_id是主鍵

4、where中存在函式,這種情況也不會使用索引查詢

mysql 主鍵失效 MySQL索引(索引失效)

索引 索引也是一張表,該錶儲存了主鍵與索引字段,並指向實體表的記錄。myisam儲存引擎,資料檔案 索引檔案 表結構檔案分開儲存 innodb儲存引擎,資料和索引儲存在乙個檔案中 b tree索引 hash索引 hash索引 只有memory儲存引擎支援 查詢一條記錄的速度非常快 b tree索引 ...

mysql 索引失效場景 Mysql 索引失效場景

例如 一張user表 有欄位屬性 name,age 其中name為索引 下面列舉幾個索引失效的情況 1.select from user where name xzz or age 16 例如這種情況 當語句中帶有or的時候 即使有索引也會失效。2.select from user where na...

mysql索引失效機制 MySQL 索引失效原理

一 聯合索引的b 樹 索引失效我們針對的是聯合索引,我們之前有講到過,在沒有遵守最佳左法則或者使用like或者使用百分號的情況下索引會失效。但是到底為什麼索引失效了並沒有解釋。索引失效和innodb引擎的b 樹儲存方式有關。我們知道單索引的b 樹是這樣的。聯合索引的b 樹也相差不多,因為聯合所有有多...