Skip to content
旧时的足迹
Go back

浅谈MySQL联合索引

编辑此页

MySQL 提供 MyISAM 、 InnoDB 、 memory(heap) 等多种存储引擎。每种存储引擎对于索引的支持以及实现都不尽相同, 本文主要讨论 InnoDB 引擎相关的联合索引应用。

为何使用索引

索引用于快速找出在某个列中有一特定值的行。在查询时如果没有应用索引,MySQL 将不得不扫描表以找出符合条件的数据项,我们知道,IO操作是非常耗时的。

建立索引的几个原则

浅谈联合索引

首先因为 InnoDB 的数据文件本身要按主键聚集,所以InnoDB要求表必须有主键(MyISAM可以没有),如果没有显式指定,则MySQL系统会自动选择一个可以唯一标识数据记录的列作为主键,如果不存在这种列,则MySQL自动为InnoDB表生成一个隐含字段作为主键。主键对于 InnoDB 的索引结构是十分重要的。

InnoDB 引擎的索引是使用 B+树 实现索引结构的,当我们建立一个联合索引(a, b, c)时,B+树将按照从左至右来建立搜索树,然后检索时B+树将先比较 a 然后再其基础上比较 b 和 c 。不难看出如果我们的搜索条件中只有 a 和 c ,将不能使用完整的(a, b, c)索引,如果我们的搜索条件中没有 a 那么这条查询将不会用上索引,这其实就是最左前缀特性。

** 接下来我们来看下联合索引应用时的几种情况: **

desc users;

+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| rname | varchar(50) | NO   |     | NULL    |                |
| rdesc | varchar(50) | NO   |     | NULL    |                |
| age   | int(11)     | NO   | MUL | NULL    |                |
| card  | int(5)      | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

show index from users;

+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| users |          0 | PRIMARY       |            1 | id          | A         |          15 |     NULL | NULL   |      | BTREE      |         |               |
| users |          1 | age_name_desc |            1 | age         | A         |          15 |     NULL | NULL   |      | BTREE      |         |               |
| users |          1 | age_name_desc |            2 | rname       | A         |          15 |       10 | NULL   |      | BTREE      |         |               |
| users |          1 | age_name_desc |            3 | rdesc       | A         |          15 |       15 | NULL   |      | BTREE      |         |               |
+-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
explain select * from users where age = 11 and rname = "asd" and rdesc = "asd";

+----+-------------+-------+------+---------------+---------------+---------+-------------------+------+-------------+
| id | select_type | table | type | possible_keys | key           | key_len | ref               | rows | Extra       |
+----+-------------+-------+------+---------------+---------------+---------+-------------------+------+-------------+
|  1 | SIMPLE      | users | ref  | age_name_desc | age_name_desc | 83      | const,const,const |    1 | Using where |
+----+-------------+-------+------+---------------+---------------+---------+-------------------+------+-------------+

以上 explain 的结果可以看出,当对索引中所有列进行精确匹配的时候,可以用到完整索引。

explain select * from users where  rname = "asd" and age = 11 and rdesc = "asd";

+----+-------------+-------+------+---------------+---------------+---------+-------------------+------+-------------+
| id | select_type | table | type | possible_keys | key           | key_len | ref               | rows | Extra       |
+----+-------------+-------+------+---------------+---------------+---------+-------------------+------+-------------+
|  1 | SIMPLE      | users | ref  | age_name_desc | age_name_desc | 83      | const,const,const |    1 | Using where |
+----+-------------+-------+------+---------------+---------------+---------+-------------------+------+-------------+

按照B+树的结构联合索引本是对顺序十分敏感的,但是从以上结果可看出调整顺序并没有影响到索引的选用,这是因为MySQL的查询优化器会自动调整where子句的条件顺序以使用适合的索引。

explain select * from users where age = 11;

+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key           | key_len | ref   | rows | Extra |
+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------+
|  1 | SIMPLE      | users | ref  | age_name_desc | age_name_desc | 4       | const |    1 | NULL  |
+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------+

从上述结果可看出当精确匹配最左前缀的列时,是可以用到索引的,但是 key_len = 4,只用到了第一列前缀索引。

explain select * from users where age = 11 and rdesc = "asd";

+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key           | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+
|  1 | SIMPLE      | users | ref  | age_name_desc | age_name_desc | 4       | const |    1 | Using where |
+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+

上述查询缺失了rname列,可以看出当缺失中间列时将不能使用完整的联合索引。查询只用到了最左部分索引,而rdesc由于没有rname无法和左前缀衔接。

explain select * from users where rname = "asd";

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | users | ALL  | NULL          | NULL | NULL    | NULL |   15 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

上述查询由于查询条件不是最左前缀,将不能使用联合索引age_name_desc,建立联合索引时顺序是很重要的,必须在建索引前考虑清楚。

explain select * from users where age = 11 and rname like 'sad%';

+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
| id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra                              |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
|  1 | SIMPLE      | users | range | age_name_desc | age_name_desc | 36      | NULL |    1 | Using index condition; Using where |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+

explain select * from users where age = 11 and rname like 'sad%' and rdesc = 'asd';

+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
| id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra                              |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
|  1 | SIMPLE      | users | range | age_name_desc | age_name_desc | 83      | NULL |    1 | Using index condition; Using where |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+

可以看出like查询当通配符’%‘不出现在开头时,是可以应用到索引的。

explain select * from users where age = 11 and rname like '%sad%';

+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key           | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+
|  1 | SIMPLE      | users | ref  | age_name_desc | age_name_desc | 4       | const |    1 | Using where |
+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+

通配符’%‘出现在开头则不行

接下来我们看看范围查询

explain select * from users where age = 61 and rname < 'fasd';

+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
| id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra                              |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
|  1 | SIMPLE      | users | range | age_name_desc | age_name_desc | 36      | NULL |    1 | Using index condition; Using where |
+----+-------------+-------+-------+---------------+---------------+---------+------+------+------------------------------------+
explain select * from users where (age + 1) = 33;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | users | ALL  | NULL          | NULL | NULL    | NULL |   15 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

当查询条件中出现函数或表达式时,将不能应用索引。写查询语句时要尽量避免表达式和函数的出现。

指导性建议

建议在选择性高的列上建立索引,所谓索引的选择性,是指不重复的索引值与表记录数的比值:

Index Selectivity = Cardinality / Count

显然选择性的取值范围为(0, 1],选择性越高的索引价值越大。 比例越大我们扫描的记录数越少,唯一键的区分度是1,而一些状态、性别字段可能在大数据面前区分度就趋近于0

select count(distinct(rname))/count(*) as selectivity from users;

+-------------+
| selectivity |
+-------------+
|      0.9333 |
+-------------+

有一种与索引选择性有关的索引优化策略叫做前缀索引,就是用列的前缀代替整个列作为索引key,当前缀长度合适时,可以做到既使得前缀索引的选择性接近全列索引,同时因为索引key变短而减少了索引文件的大小和维护开销。

select count(distinct(left(rname, 3)))/count(*) as selectivity from users;

+-------------+
| selectivity |
+-------------+
|      0.7333 |
+-------------+

select count(distinct(left(rname, 5)))/count(*) as selectivity from users;

+-------------+
| selectivity |
+-------------+
|      0.9333 |
+-------------+

可以看到,当把前缀取到5时selectivity值就和完整的selectivity值一样了,这样可以大幅度减小索引所占用的空间,而且相应的查询速度也会有一定提升。


* * *

编辑此页
Share this post on:

Previous Post
MySQL索引与B-Tree
Next Post
An Introduction To Oauth2 - P2