mysql 多列索引的生效規則

來源:互聯網
上載者:User

標籤:就會   資料   abc   http   sam   now()   image   快速   val   

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()begindeclare 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();

 

mysql 多列索引的生效規則

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.