Mysql 索引優化例子詳解優化

創建表、創建索引

分析case索引使用情況

一個20左右的表,建立三個左右的索引常規溝通,但是根據自己情況

1:

explain select * from test where c1 = 'a1' and c2 = 'a2' and c3 = 'a3' and c4 = 'a4';
explain select * from test where c1 = 'a1' and c3 = 'a3' and c2 = 'a2' and c4 = 'a4';
explain select * from test where c1 = 'a1' and c4 = 'a4' and c3 = 'a3' and c2 = 'a2';
explain select * from test where c4 = 'a4' and c3 = 'a3' and c2 = 'a2' and c1 = 'a1';

分析一下:

1、創建複合索引的順序爲c1,c2,c3,c4

2、explain執行的結果:type=ref,key_len=132,ref=const,const,const,const

結論:在執行常量等值查詢時,改版索引列的順序並不會更改explan的執行結果,因爲Mysql底層優化器會進行優化,但是推薦索引順序列編寫sql語句。

2

explain select * from test where c1 = 'a1'  and c2 = 'a2';

explain select * from test where c1 = 'a1' and c2 = 'a2' and c3 > 'a3' and c4 = 'a4';

分析:

當出現範圍的時候,type=range,key_len=99,比不用範圍key_len=66增加了,說明使用上了索引,但對比Case1中執行結果,說明c4上索引失效。

結論:

範圍右邊索引列失效,但是範圍當前位置(c3)的索引是有效的,從key_len=99可證明。

2.1

explain select * from test where c1 = 'a1' and c2 = 'a2' and c4 > '43' and c3 = 'a3' ;

分析:

與上面explain執行結果對比,key_len=132說明索引用到了4個,因爲對此sql語句mysql底層優化器會進行優化:範圍右邊索引列失效(c4右邊已經沒有索引列了),注意索引的順序(c1,c2,c3,c4),所以c4右邊不會出現失效的索引列,因此4個索引全部用上。

結論:

範圍右邊索引列失效,是有順序的:c1,c2,c3,c4,如果c3有範圍,則c4失效;如果c4有範圍,則沒有失效的索引列,從而會使用全部索引。

2.2

explain select * from test where c1 > 'a1' and c2 = 'a2' and c3 = 'a3' and c4 = 'a4';

分析:

如果在c1處使用範圍,則type=ALL,key=Null,索引失效,全表掃描,這裏違背了最佳左前綴法則,帶頭大哥已死,因爲c1主要用於範圍,而不是查詢。
解決方式使用覆蓋索引。

結論:

在最佳左前綴法則中,如果最左前列(帶頭大哥)的索引失效,則後面的索引都失效。

3

explain select * from test where c1 > 'a1' and c2 = 'a2' and c4 = 'a4' order by c3;

分析:

利用最佳左前綴法則:中間兄弟不能斷,因此用到了c1和c2索引(查找),從key_len=66,ref=const,const,c3索引列用在排序過程中。

3.1

explain select * from test where c1 > 'a1' and c2 = 'a2' order by c3;

分析:

從explain的執行結果來看:key_len=66,ref=const,const,從而查找只用到c1和c2索引,c3索引用於排序。

3.2

explain select * from test where c1 > 'a1' and c2 = 'a2' order by c4;

分析:

從explain的執行結果來看:key_len=66,ref=const,const,查詢使用了c1和c2索引,由於用了c4進行排序,跳過了c3,出現了Using filesort。

4

explain select * from test where c1 > 'a1' and c5 = 'a5' order by c2,c3;

分析:

查找只用到索引c1,c2和c3用於排序,無Using filesort。

4.1

explain select * from test where c1 > 'a1' and c5 = 'a5' order by c3,c2;

分析:

和Case 4中explain的執行結果一樣,但是出現了Using filesort,因爲索引的創建順序爲c1,c2,c3,c4,但是排序的時候c2和c3顛倒位置了。

4.2

explain select * from test where c1 > 'a1' and c2 = 'a2' order by c2,c3;

explain select * from test where c1 = 'a1' and c2 = 'a2' and c5 = 'c5' order by c2,c3;

分析:

在查詢時增加了c5,但是explain的執行結果一樣,因爲c5並未創建索引。

4.3

explain select * from test where c1 = 'a1' and c2 = 'a2' and c5 = 'c5' order by c3,c2;

5

explain select * from test where c1 = 'a1' and c4 = 'a4' order by c2,c3;

分析:

只用到c1上的索引,因爲c4中間間斷了,根據最佳左前綴法則,所以key_len=33,ref=const,表示只用到一個索引。

5.1

explain select * from test where c1 = 'a1' and c4 = 'a4' order by c3,c2;

分析:

對比Case 5,在group by時交換了c2和c3的位置,結果出現Using temporary和Using filesort,極度惡劣。原因:c3和c2與索引創建順序相反。

6

explain select * from test where c1 > 'a1'  order by c1;

分析:

1、在c1,c2,c3,c4上創建了索引,直接在c1上使用範圍,導致了索引失效,全表掃描:type=ALL,ref=Null。因爲此時c1主要用於排序,並不是查詢。

2、使用c1進行排序,出現了Using filesort。

3、解決方法:使用覆蓋索引。

explain select c1 from test where c1 > 'a1'  order by c1;

7

explain select c1 from test order by c1 asc,c2 desc;

分析:

雖然排序的字段列與索引順序一樣,且order by默認升序,這裏c2 desc變成了降序,導致與索引的排序方式不同,從而產生Using filesort。

8

EXPLAIN extended select c1 from test where c1 in ('a1','b1') ORDER BY c2,c3;

分析:

對於排序來說,多個相等條件也是範圍查詢

總結:

1、MySQL支持兩種方式的排序filesort和index,Using index是指MySQL掃描索引本身完成排序。index效率高,filesort效率低。

2、order by滿足兩種情況會使用Using index。

1.order by語句使用索引最左前列。
2.使用where子句與order by子句條件列組合滿足索引最左前列。

3、儘量在索引列上完成排序,遵循索引建立(索引創建的順序)時的最佳左前綴法則。

4、如果order by的條件不在索引列上,就會產生Using filesort。

5、group by與order by很類似,其實質是先排序後分組,遵照索引創建順序的最佳左前綴法則。注意where高於having,能寫在where中的限定條件就不要去having限定了。

口訣

全值匹配我最愛,最左前綴要遵守;
帶頭大哥不能死,中間兄弟不能斷;
索引列上少計算,範圍之後全失效;
LIKE百分寫最右,覆蓋索引不寫(*);
不等空值還有or,索引失效要少用。

in和exsits優化

原則:小表驅動大表,即小的數據集驅動大的數據集

in:當B表的數據集必須小於A表的數據集時,in優於exists

select * from A where id in (select id from B)
#等價於:  

for select id from B 

for select * from A where A.id = B.id

exists:當A表的數據集小於B表的數據集時,exists優於in

將主查詢A的數據,放到子查詢B中做條件驗證,根據驗證結果(true或false)來決定主查詢的數據是否保留

select * from A where exists (select 1 from B where B.id = A.id) #等價於   

for select * from A    

for select * from B where B.id = A.id#A表與B表的ID字段應建立索引

1、EXISTS (subquery)只返回TRUE或FALSE,因此子查詢中的SELECT * 也可以是SELECT 1或select X,官方說法是實際執行時會忽略SELECT清單,因此沒有區別

2、EXISTS子查詢的實際執行過程可能經過了優化而不是我們理解上的逐條對比

3、EXISTS子查詢往往也可以用JOIN來代替,何種最優需要具體問題具體分析

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章