MySQL高級知識(八)——索引面試題分析

MySQL高級知識(八)——索引面試題分析

此博客的內容主要來源於尚硅谷的視頻中,在此記錄,以備以後自己查看。

準備

  1. 創建test表(測試表)
drop table if exists test;
create table test(
id int primary key auto_increment,
c1 varchar(10),
c2 varchar(10),
c3 varchar(10),
c4 varchar(10),
c5 varchar(10)
) 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');

select * from test;

  1. 創建索引
    create index idx_test_c1234 on test(c1,c2,c3,c4);
    show index from test;

在這裏插入圖片描述
在這裏插入圖片描述

事例分析

  • 事例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';

在這裏插入圖片描述

分析:

  • 創建複合索引的順序爲c1,c2,c3,c4;
  • 上述的四組explain執行的結果都一樣:type=ref,key_len=132,ref=const,const,const,const。(and忽略左右關係,優化器自動優化)

結論:在執行常量等值查詢的時候,改變索引列的順序並不會更改explain的執行結果,因爲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增加了,說明使用上了索引,但是對比事例1中執行的結果,說明c4索引失效。

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


  • 事例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),所以吃
  • 右邊不會出現失效的索引列,因此4個索引全部用上了。

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


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

# 覆蓋索引
explain select c1,c2,c3,c4 from test where c1>'a1' and c2='a2' and c3='a3' and c4='a4';

在這裏插入圖片描述

分析:

  • 如果在c1出使用範圍,則type=ALL,key=Null,索引失效,全表掃描,這裏違背了最佳左前綴法則,帶頭大哥已死,因爲c1主要用於範圍,而不是查詢。解決方式:覆蓋索引
  • 上述的第二個就是覆蓋索引的效果,可以看出type=range,key_len=33,所有隻有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;

在這裏插入圖片描述
分析:

  • 和事例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='a5' order by c2,c3;

在這裏插入圖片描述

分析:

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

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

在這裏插入圖片描述

分析:

  • 與事例4.1對比,在Extra中並未出現Using filesort,因爲c2爲常量,在排序中被優化(等值常量可以被優化),所以索引未顛倒,不會出現Using filesort。

  • 事例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 filesort,極度惡劣。原因:c3和c2與索引創建順序相反。

總結

  1. 最佳左前綴法則。

    1. 在等值查詢時,更改索引列順序,並不會影響explain的執行結果,因爲mysql底層會進行優化。

    2. 在使用order by時,注意索引順序、常量,以及可能會導致Using filesort的情況。

  2. group by容易產生Using temporary。

  3. 通俗理解口訣:

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

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

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

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

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

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