【金三銀四】Mysql索引優化實戰

【MySql系列】文章導航

【金三銀四】深刻理解MySql索引底層數據結構解密 juejin.im/post/5e0d7b…mysql

【金三銀四】MySql執行計劃EXPLAIN詳解 juejin.im/post/5e12e4…sql

【金三銀四】MySql執行計劃EXPLAIN最佳實踐 juejin.im/post/5e12e4…bash

【金三銀四】MySql索引優化實戰 juejin.im/post/5e12e5…數據結構

**建立test表(測試表) **

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` varchar(10) DEFAULT NULL,
  `c2` varchar(10) DEFAULT NULL,
  `c3` varchar(10) DEFAULT NULL,
  `c4` varchar(10) DEFAULT NULL,
  `c5` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into test(c1,c2,c3,c4,c5) values('a1','a2','a3','a4','a5');
insert into test(c1,c2,c3,c4,c5) values('b1','b2','b3','b4','b5');
insert into test(c1,c2,c3,c4,c5) values('c1','c2','c3','c4','c5');
insert into test(c1,c2,c3,c4,c5) values('d1','d2','d3','d4','d5');
insert into test(c1,c2,c3,c4,c5) values('e1','e2','e3','e4','e5');

複製代碼

建立索引

create index idx_test_c1234 on test(c1,c2,c3,c4);
show index from test;

複製代碼

圖片

分析如下Case索引使用狀況

Case 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 c4='a4' and c2='a2' and c3='a3' ;

複製代碼

圖片

分析:post

①建立複合索引的順序爲c1,c2,c3,c4。測試

②上述explain執行的結果都同樣:type=ref,key_len=132,ref=const,const,const,const。優化

結論:在執行常量等值查詢時,改變索引列的順序並不會更改explain的執行結果,由於mysql底層優化器會進行優化,可是推薦按照索引順序列編寫sql語句。ui

Case 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';

複製代碼

圖片

分析:spa

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

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

Case 2.1:

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

複製代碼

圖片

分析:

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

結論:範圍右邊索引列失效,是有順序的:c1,c2,c3,c4,若是c3有範圍,則c4失效;若是c4有範圍,則沒有失效的索引列,從而會使用所有索引。

Case 2.2:

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

複製代碼

圖片

分析:

若是在c1處使用範圍,則type=ALL,key=Null,索引失效,全表掃描,這裏違背了最左前綴法則,帶頭大哥已死,由於c1主要用於範圍,而不是查詢。

解決方式使用覆蓋索引。

圖片
結論:在最左前綴法則中,若是最左前列(帶頭大哥)的索引失效,則後面的索引都失效。

Case 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索引列用在排序過程當中。

Case 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索引用於排序。

Case 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。

Case 4:

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

複製代碼

圖片

分析:

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

Case 4.1:

圖片

分析:

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

Case 4.2:

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

複製代碼

圖片

圖片

分析:

在查詢時增長了c5,可是explain的執行結果同樣,由於c5並未建立索引。

Case 4.3:

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

複製代碼

圖片

分析:

與Case 4.1對比,在Extra中並未出現Using filesort,由於c2爲常量,在排序中被優化,因此索引未顛倒,不會出現Using filesort。

Case 5:

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

複製代碼

圖片

分析:

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

Case 5.1:

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

複製代碼

圖片

分析:

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

Case 6:

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

複製代碼

圖片

分析:

①在c1,c2,c3,c4上建立了索引,直接在c1上使用範圍,致使了索引失效,全表掃描:type=ALL,ref=Null。由於此時c1主要用於排序,並非查詢。

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

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

圖片

Case 7:

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

複製代碼

圖片

分析:

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

Case 8:

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

複製代碼

圖片

分析:

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

總結:

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

②order by知足兩種狀況會使用Using index。

  • order by語句使用索引最左前列。
  • 使用where子句與order by子句條件列組合知足索引最左前列。

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

④若是order by的條件不在索引列上,就會產生Using filesort。

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

通俗理解口訣:

全值匹配我最愛,最左前綴要遵照;

帶頭大哥不能死,中間兄弟不能斷;

索引列上少計算,範圍以後全失效;

LIKE百分寫最右,覆蓋索引不寫星;

不等空值還有or,索引失效要少用。

相關文章
相關標籤/搜索