mysql 里創(chuàng)建‘聯(lián)合索引’的意義
問題?
因為什么需求避乏,要創(chuàng)建‘聯(lián)合索引’?最實際好處在于什么甘桑?如果是為了更快查詢到數(shù)據(jù)拍皮,有單列索引不是Ok?為什么有‘聯(lián)合索引’的存在跑杭?
一
簡單的說有兩個主要原因:
"一個頂三個"铆帽。建了一個(a,b,c)的復(fù)合索引,那么實際等于建了(a),(a,b),(a,b,c)三個索引艘蹋,因為每多一個索引锄贼,都會增加寫操作的開銷和磁盤空間的開銷。對于大量數(shù)據(jù)的表女阀,這可是不小的開銷宅荤!
覆蓋索引。同樣的有復(fù)合索引(a,b,c)浸策,如果有如下的sql: select a,b,c from table where a=1 and b = 1冯键。那么MySQL可以直接通過遍歷索引取得數(shù)據(jù),而無需回表庸汗,這減少了很多的隨機io操作惫确。減少io操作,特別的隨機io其實是dba主要的優(yōu)化策略。所以改化,在真正的實際應(yīng)用中掩蛤,覆蓋索引是主要的提升性能的優(yōu)化手段之一
索引列越多,通過索引篩選出的數(shù)據(jù)越少陈肛。有1000W條數(shù)據(jù)的表揍鸟,有如下sql:select * from table where a = 1 and b =2 and c = 3,假設(shè)假設(shè)每個條件可以篩選出10%的數(shù)據(jù),如果只有單值索引句旱,那么通過該索引能篩選出1000W*10%=100w 條數(shù)據(jù)阳藻,然后再回表從100w條數(shù)據(jù)中找到符合b=2 and c= 3的數(shù)據(jù),然后再排序谈撒,再分頁腥泥;如果是復(fù)合索引,通過索引篩選出1000w *10% *10% *10%=1w啃匿,然后再排序、分頁冒萄,哪個更高效,一眼便知
二
如下的有a,b,c 三個key的table
create table test(
a int,
b int,
c int,
);
如果我們
需要執(zhí)行很多的類似于 select * from test where a=10, b>50, c>20
這類的組合查詢 那么灯帮,我們可能需要創(chuàng)建 包含[a逻住,b瞎访,c] 的聯(lián)合索引,而單獨的[a][b] [c]上的索引是不夠的播演。(可以把一個索引想象成 sorted list).創(chuàng)建了 (a写烤,b洲炊,c)的索引相當于 按照a,b,c 排序(排序規(guī)則是
if(X.a>Y.a)
return '>';
else if(X.a<Y.a)
return '<';
else if(X.b>Y.b)
return '>';
else if (X.b<Y.b)
return '<';
else if (X.c>Y.c)
return '>'
else if (X.c<Y.c)
return '<'
esle
return '=='
)
和分別 按a 排序 分別按b排序 分別按照c排序是不一樣的暂衡。
其中 a b c 的順序也很重要,有時可以是a c b狂巢,或者b c a等等。
如果創(chuàng)建 (a,b,c)的聯(lián)合索引代态,查詢效率如下:
優(yōu): select * from test where a=10 and b>50
差: select * from test where a>50
優(yōu): select * from test order by a
差: select * from test order by b
差: select * from test order by c
優(yōu): select * from test where a=10 order by a
優(yōu): select * from test where a=10 order by b
差: select * from test where a=10 order by c
優(yōu): select * from test where a>10 order by a
差: select * from test where a>10 order by b
差: select * from test where a>10 order by c
優(yōu): select * from test where a=10 and b=10 order by a
優(yōu): select * from test where a=10 and b=10 order by b
優(yōu): select * from test where a=10 and b=10 order by c
優(yōu): select * from test where a=10 and b=10 order by a
優(yōu): select * from test where a=10 and b>10 order by b
差: select * from test where a=10 and b>10 order by c
下面用圖示的方式來表示
三 注意
在mysql中 若列是varchar 類型蹦疑,請不要使用 int類型去訪問
如下
zz_deals表中 product_id 是varchar類型
mysql> explain select * from zz_deals where qq_shop_id = 64230 and product_id = '38605906667' ;
+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+
| 1 | SIMPLE | zz_deals | ref | by_product_id_and_qq_shop_id | by_product_id_and_qq_shop_id | 156 | const,const | 1 | Using where |
+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from zz_deals where qq_shop_id = 64230 and product_id = 38605906667 ;
+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+
| 1 | SIMPLE | zz_deals | ALL | by_product_id_and_qq_shop_id | NULL | NULL | NULL | 17 | Using where |
+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
宣傳語
歷經(jīng)兩個半月的準備歉摧,三次大改版叁温,十七次小改版膝但。le1024終于要和大家見面了跟束。
le1024每天推薦1~3段丑孩,有趣温学、有愛仗岖、有故事的視頻箩帚。
為您工作、學習紧帕、生活之余增加一點快樂的感覺桅打。
參考:
segmentfault