数据库

Mysql联合索引最左匹配原则

2019-01-17  本文已影响142人  王小杰at2019

[toc]

为什么使用联合索引

以联合索引(a,b,c)为例

1. 示例表结构

CREATE TABLE `test_user` (
  `name`     varchar(20) DEFAULT NULL,
  `province` int(11)     DEFAULT NULL,
  `sex`      varchar(20) DEFAULT NULL,
  `birthday` int(11)     DEFAULT NULL,
  `phone`    double      DEFAULT NULL
)
  ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

CREATE INDEX test_user_name_phone_province_index
  ON test_user (name, phone, province);

2. 执行计划说明

以下全部详细解析explain各个属性含义:

+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------+------+----------+-------+
| id | select_type | table     | partitions | type | possible_keys                       | key                                 | key_len | ref   | rows | filtered | Extra |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------+------+----------+-------+

各属性含义:

3. 执行计划分析

mysql> explain select *
    ->         from test_user
    ->         where name = '张三';
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------+------+----------+-------+
| id | select_type | table     | partitions | type | possible_keys                       | key                                 | key_len | ref   | rows | filtered | Extra |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | test_user | NULL       | ref  | test_user_name_phone_province_index | test_user_name_phone_province_index | 63      | const |   11 |   100.00 | NULL  |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)

使用了一个const

mysql> explain select *
    ->         from test_user
    ->         where phone = '13546294373';
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | test_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4753980 |    10.00 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

全表扫描

mysql> explain select *
    ->         from test_user
    ->         where province = '532130';
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | test_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4753980 |    10.00 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

全表扫描

mysql> explain select *
    ->         from test_user
    ->         where phone = '13546294373';
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | test_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4753980 |    10.00 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

全表扫描

mysql> explain select *
    ->         from test_user
    ->         where province = '532130';
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | test_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4753980 |    10.00 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

全表扫描

mysql> explain select *
    ->         from test_user
    ->         where name = '张三' and province = '532130';
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------+------+----------+-----------------------+
| id | select_type | table     | partitions | type | possible_keys                       | key                                 | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | test_user | NULL       | ref  | test_user_name_phone_province_index | test_user_name_phone_province_index | 63      | const |   11 |    10.00 | Using index condition |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

使用了一个const

mysql> explain select *
    ->         from test_user
    ->         where name = '张三' and phone = '13546294373';
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------------+------+----------+-------+
| id | select_type | table     | partitions | type | possible_keys                       | key                                 | key_len | ref         | rows | filtered | Extra |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------------+------+----------+-------+
|  1 | SIMPLE      | test_user | NULL       | ref  | test_user_name_phone_province_index | test_user_name_phone_province_index | 72      | const,const |    1 |   100.00 | NULL  |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

使用了两个const

mysql> explain select *
    ->         from test_user
    ->         where  phone = '13546294373' and province = '532130';
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | test_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4753980 |     1.00 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

全表扫描

mysql> explain select *
    ->         from test_user
    ->         where     province = '532130' and phone = '13546294373';
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | test_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 4753980 |     1.00 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

全表扫描

mysql> explain select *
    ->         from test_user
    ->         where name = '张三' and phone = '13546294373'  and province = '532130';
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------------------+------+----------+-------+
| id | select_type | table     | partitions | type | possible_keys                       | key                                 | key_len | ref               | rows | filtered | Extra |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------------------+------+----------+-------+
|  1 | SIMPLE      | test_user | NULL       | ref  | test_user_name_phone_province_index | test_user_name_phone_province_index | 77      | const,const,const |    1 |   100.00 | NULL  |
+----+-------------+-----------+------------+------+-------------------------------------+-------------------------------------+---------+-------------------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

使用了到了 三个const

4. 现象总结

对于联合索引

CREATE INDEX test_user_name_phone_province_index
  ON test_user (name, phone, province);

在查询中我们能用到的索引的是

对于其他顺序,或者其他字段我们不能使用该联合索引,这个就是mysql联合索引原则

上一篇 下一篇

猜你喜欢

热点阅读