mysql聯合索引的選擇性

orclwujian發表於2017-01-18
聯合索引就是同時對錶中多個列進行索引,聯合索引會依次根據聯合索引中的列排序儲存。
下面透過下面的實驗來探討下聯合索引的使用選擇性:
版本:percona 5.6.27
create table test(
a int,
b int,
c int,
name varchar(32),
PRIMARY key(a),
key index_a_b_c(a,b,c)) ENGINE=INNODB
insert into test values(1,1,1,3,'leo');
insert into test values(2,1,2,1,'mike' );
insert into test values(3,1,3,1,'exo' );
insert into test values(4,1,2,3,'jhon' );
insert into test values(5,1,1,3,'lucy' );
insert into test values(6,2,2,3,'leo' );
insert into test values(7,3,1,2,'dv' );
insert into test values(8,2,1,3,'men' );

一:where條件對聯合索引的選擇性
mysql> explain select * from test where a=2;
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+
|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | NULL  |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+

mysql> explain select * from test where a=2 and b=1;
+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+
| id | select_type | table | type | possible_keys | key         | key_len | ref         | rows | Extra |
+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+
|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 10      | const,const |    1 | NULL  |
+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+


mysql> explain select * from test where a=2 and b=2 and c=3;
+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+
| id | select_type | table | type | possible_keys | key         | key_len | ref               | rows | Extra |
+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+
|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 15      | const,const,const |    1 | NULL  |
+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+

這三個是正常的使用方法,都走了索引

mysql> explain select * from test where a=2 and c=3;
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra                 |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using index condition |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)

如果把b漏掉,同樣走了索引

mysql> explain select * from test where b=2 and c=3;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from test where c=3;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

如果把a漏掉,則不會走索引

結論:必須有聯合索引的第一個欄位作為wehre條件

二:聯合索引排序選擇性
聯合索引的排序會按照(a,b,c)的順序進行排序
測試資料在聯合索引的排序會是(1,1,3), (1,2,1), (1,2,3), (1,3,1), (2,1,3), (2,2,3), (3,1,2)順序儲存
mysql> explain select * from test where a=2 order by b;
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+
|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using where |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+

mysql> explain select * from test where a=2 order by c;
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra                       |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+
|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using where; Using filesort |
+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+

可以看出第二個Using filesort使用了臨時表排序了,效率低。從聯合索引的排序就可以知道當指定a的值的時候,這些值會按b的值排序,不是按c的值排序,故order by b不用再filesort排序,反之order by b需要重新排序。
所以select * from test where a=2 and b=2 order by c;不會 filesort排序
mysql> explain select * from test where a=2 and b=2 order by c;
+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key         | key_len | ref         | rows | Extra       |
+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+
|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 10      | const,const |    1 | Using where |
+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+


結論:當針對聯合索引中的某個欄位進行排序的時候,最優的方法是有聯合索引排序欄位之前的欄位過濾條件

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29989552/viewspace-2132647/,如需轉載,請註明出處,否則將追究法律責任。

相關文章