mysql中 myisam,innodb默認使用的是 Btree索引,至於btree的數據結構是怎樣的都不重要,
只須要知道結果,既然是索引那這個數據結構最後是排好序;就像新華字典他的目錄就是按照a,b,c..這樣排好序的;
因此你在找東西的時候才快,好比你找 「中」 這個字的解釋,你確定就會定位到目錄的 z 開頭部分;
組合索引能夠這樣理解,好比(a,b,c),abc都是排好序的,在任意一段a的下面b都是排好序的,任何一段b下面c都是排好序的;mysql
組合索引的生效原則是 從前日後依次使用生效,若是中間某個索引沒有使用,那麼斷點前面的索引部分起做用,斷點後面的索引沒有起做用;
好比sql
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測試數據庫;數據結構
建立測試表:oop
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();