1. 程式人生 > >mysql多列索引的生效規則

mysql多列索引的生效規則

mysql中 myisam,innodb預設使用的是 Btree索引,至於btree的資料結構是怎樣的都不重要,
只需要知道結果,既然是索引那這個資料結構最後是排好序;就像新華字典他的目錄就是按照a,b,c..這樣排好序的;
所以你在找東西的時候才快,比如你找 “中” 這個字的解釋,你肯定就會定位到目錄的 z 開頭部分;

組合索引可以這樣理解,比如(a,b,c),abc都是排好序的,在任意一段a的下面b都是排好序的,任何一段b下面c都是排好序的;


組合索引的生效原則是  從前往後依次使用生效,如果中間某個索引沒有使用,那麼斷點前面的索引部分起作用,斷點後面的索引沒有起作用;
比如

where a=3 and
b=45 and c=5 .... 這種三個索引順序使用中間沒有斷點,全部發揮作用; where a=3 and c=5... 這種情況下b就是斷點,a發揮了效果,c沒有效果 where b=3 and c=4... 這種情況下a就是斷點,在a後面的索引都沒有發揮作用,這種寫法聯合索引沒有發揮任何效果; where b=45 and a=3 and c=5 .... 這個跟第一個一樣,全部發揮作用,abc只要用上了就行,跟寫的順序無關


(a,b,c) 三個列上加了聯合索引(是聯合索引 不是在每個列上單獨加索引)

還需注意,  (a,b,c)多列索引和 (a,c,b)是不一樣的,看上面的圖也看得出來關係順序是不一樣的;
分析幾個實際例子來加強理解;
分析句子中使用的索引情況

複製程式碼
(0)    select * from mytable where a=3 and b=5 and c=4;
abc三個索引都在where條件裡面用到了,而且都發揮了作用
(1)    select * from mytable where  c=4 and b=6 and a=3;
這條語句列出來只想說明 mysql沒有那麼笨,where裡面的條件順序在查詢之前會被mysql自動優化,效果跟上一句一樣
(2)    select * from mytable where a=3 and c=7;
a用到索引,b沒有用,所以c是沒有用到索引效果的
(3)    select * from mytable where a=3
and b>7 and c=3; a用到了,b也用到了,c沒有用到,這個地方b是範圍值,也算斷點,只不過自身用到了索引 (4) select * from mytable where b=3 and c=4; 因為a索引沒有使用,所以這裡 bc都沒有用上索引效果 (5) select * from mytable where a>4 and b=7 and c=9; a用到了 b沒有使用,c沒有使用 (6) select * from mytable where a=3 order by b; a用到了索引,b在結果排序中也用到了索引的效果,前面說了,a下面任意一段的b是排好序的 (7) select * from mytable where a=3 order by c; a用到了索引,但是這個地方c沒有發揮排序效果,因為中間斷點了,使用 explain 可以看到 filesort (8) select * from mytable where b=3 order by a; b沒有用到索引,排序中a也沒有發揮索引效果
複製程式碼


補充一個:

快速生成1000W測試資料庫;

建立測試表:

複製程式碼
create table user (  
    id int(10) not null auto_increment,   
    uname  varchar(20) ,  
    regtime  char(30)  ,  
    age  int(11)   ,
    primary key (id)
)  
engine=myisam default charset=utf8 collate=utf8_general_ci  ,
auto_increment=1 ;
複製程式碼

編寫儲存過程:

複製程式碼
delimiter $$
SET AUTOCOMMIT = 0$$
 
create  procedure test()
begin
declare v_cnt decimal (10)  default 0 ;
dd:loop
          insert  into user values
    (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50),
        (null,rand()*10,now(),rand()*50);
      commit;
        set v_cnt = v_cnt+10 ;
           if  v_cnt = 10000000 then leave dd;
          end if;
         end loop dd ;
end;$$
 
delimiter ;
複製程式碼

呼叫儲存過程:

call test();