聯(lián)合索引的b+結(jié)構(gòu)

聯(lián)合索引
create table test(
a int ,
b int,
c int,
d int,
key index_abc(a,b,c)
)engine=InnoDB default charset=utf8;
只要where有a的查詢就會(huì)用到上面的聯(lián)合索引,無(wú)關(guān)順序
比如:
explain select * from test where a<10 ;
explain select * from test where b<10 and a <10;
explain select * from test where b<10 and a <10 and c<10;
explain select * from test where a<10 and c <10;(a走索引了,c沒(méi)走)
explain select * from test where a<10 and b <10;
explain select * from test where a<10 and b <10 and c<10;
下面不會(huì)用到聯(lián)合索引(沒(méi)有用到a)
explain select * from test where b<10 and c <10;
為什么?
當(dāng)b+樹(shù)的數(shù)據(jù)項(xiàng)是復(fù)合的數(shù)據(jù)結(jié)構(gòu),比如(name,age,sex)的時(shí)候,b+數(shù)是按照從左到右的順序來(lái)建立搜索樹(shù)的,比如當(dāng)(張三,20,F)這樣的數(shù)據(jù)來(lái)檢索的時(shí)候,b+樹(shù)會(huì)優(yōu)先比較name來(lái)確定下一步的所搜方向,如果name相同再依次比較age和sex,最后得到檢索的數(shù)據(jù);
但當(dāng)(20,F)這樣的沒(méi)有name的數(shù)據(jù)來(lái)的時(shí)候,b+樹(shù)就不知道下一步該查哪個(gè)節(jié)點(diǎn),因?yàn)榻⑺阉鳂?shù)的時(shí)候name就是第一個(gè)比較因子,必須要先根據(jù)name來(lái)搜索才能知道下一步去哪里查詢。
比如當(dāng)(張三,F)這樣的數(shù)據(jù)來(lái)檢索時(shí),b+樹(shù)可以用name來(lái)指定搜索方向,但下一個(gè)字段age的缺失,所以只能把名字等于張三的數(shù)據(jù)都找到,然后再匹配性別是F的數(shù)據(jù)了, 這個(gè)是非常重要的性質(zhì),即索引的最左匹配特性。
單個(gè)索引
create table test(
a int ,
b int,
key index_a(a),
key index_b(b),
)engine=InnoDB default charset=utf8;
explain select * from test where a<10 and b <10;
explain select * from test where b <10 and a<10;
實(shí)際上只會(huì)用到index_a索引