本文转自[58沈剑 架构师之路]
概述
explain结果中的type字段代表什么意思?
MySQL的官网解释非常简洁,只用了3个单词:连接类型(the join type)。它描述了找到所需数据使用的扫描方式。
最为常见的扫描方式有:
system:系统表,少量数据,往往不需要进行磁盘IO;const:常量连接;eq_ref:主键索引(primary key)或者非空唯一索引(unique not null)等值扫描;ref:非主键非唯一索引等值扫描;range:范围扫描;index:索引树扫描;ALL:全表扫描(full table scan);
画外音:这些是最常见的,大家去
explain自己工作中的SQL语句,95%都是上面这些类型。
上面各类扫描方式由快到慢:
system > const > eq_ref > ref > range > index > ALL
下面一一举例说明。
一、system
mysql> explain select * from mysql.time_zone;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | time_zone | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.17 sec)上例中,从系统库mysql的系统表time_zone里查询数据,扫码类型为system,这些数据已经加载到内存里,不需要进行磁盘IO。
这类扫描是速度最快的。
数据准备:
create table user (
id int primary key,
name varchar(20)
)engine=innodb;
insert into user values(1,'shenjian');
insert into user values(2,'zhangsan');
insert into user values(3,'lisi');mysql> explain select * from (select * from user where id=1) tmp;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | user | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)再举一个例子,内层嵌套(const)返回了一个临时表,外层嵌套从临时表查询,其扫描类型也是system,也不需要走磁盘IO,速度超快。
二、const
使用上面准备的数据
mysql> explain select * from user where id=1;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | user | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)const扫描的条件为:
命中主键(primary key)或者唯一(unique)索引;
被连接的部分是一个常量(const)值;
如上例,id是PK,连接部分是常量1。
画外音:别搞什么类型转换的幺蛾子。
这类扫描效率极高,返回数据量少,速度非常快。
三、eq_ref
数据准备:
create table user_ex (
id int primary key,
age int
)engine=innodb;
insert into user_ex values(1,18);
insert into user_ex values(2,20);
insert into user_ex values(3,30);
insert into user_ex values(4,40);
insert into user_ex values(5,50);mysql> explain select * from user,user_ex where user.id=user_ex.id;
+----+-------------+---------+------------+--------+---------------+---------+---------+---------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+--------+---------------+---------+---------+---------------+------+----------+-------+
| 1 | SIMPLE | user | NULL | ALL | PRIMARY | NULL | NULL | NULL | 3 | 100.00 | NULL |
| 1 | SIMPLE | user_ex | NULL | eq_ref | PRIMARY | PRIMARY | 4 | test2.user.id | 1 | 100.00 | NULL |
+----+-------------+---------+------------+--------+---------------+---------+---------+---------------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)eq_ref扫描的条件为,对于前表的每一行(row),后表只有一行被扫描。
再细化一点:
join查询;
命中主键(primary key)或者非空唯一(unique not null)索引;
等值连接;
如上例,id是主键,该join查询为eq_ref扫描。
这类扫描的速度也异常之快。
四、ref
数据准备:
create table user_ex2 (
id int,
age int,
index(id)
)engine=innodb;
insert into user_ex2 values(1,18);
insert into user_ex2 values(2,20);
insert into user_ex2 values(3,30);
insert into user_ex2 values(4,40);
insert into user_ex2 values(5,50);把上例eq_ref案例中的主键索引,改为普通非唯一(non unique)索引。
mysql> explain select * from user,user_ex2 where user.id=user_ex2.id;
+----+-------------+----------+------------+------+---------------+------+---------+---------------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+---------------+------+----------+-------+
| 1 | SIMPLE | user | NULL | ALL | PRIMARY | NULL | NULL | NULL | 3 | 100.00 | NULL |
| 1 | SIMPLE | user_ex2 | NULL | ref | id | id | 5 | test2.user.id | 1 | 100.00 | NULL |
+----+-------------+----------+------------+------+---------------+------+---------+---------------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)就由eq_ref降级为了ref,此时对于前表的每一行(row),后表可能有多于一行的数据被扫描。
mysql> explain select * from user_ex2 where id=1;
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
| 1 | SIMPLE | user_ex2 | NULL | ref | id | id | 5 | const | 1 | 100.00 | NULL |
+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)当id改为普通非唯一索引后,常量的连接查询,也由const降级为了ref,因为也可能有多于一行的数据被扫描。
ref扫描,可能出现在join里,也可能出现在单表普通索引里,每一次匹配可能有多行数据返回,虽然它比eq_ref要慢,但它仍然是一个很快的join类型。
五、range
mysql> explain select * from user where id between 1 and 4;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 3 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)mysql> explain select * from user where id in(1,2,3);
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 3 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)mysql> explain select * from user where id>3;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 1 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)range扫描就比较好理解了,它是索引上的范围查询,它会在索引上扫描特定范围内的值。
画外音:必须是索引,否则不能批量”跳过”。
六、index
mysql> explain select count(*) from user;
+—-+————-+——-+————+——-+—————+———+———+——+——+———-+————-+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+—-+————-+——-+————+——-+—————+———+———+——+——+———-+————-+
| 1 | SIMPLE | user | NULL | index | NULL | PRIMARY | 4 | NULL | 3 | 100.00 | Using index |
+—-+————-+——-+————+——-+—————+———+———+——+——+———-+————-+
1 row in set, 1 warning (0.01 sec)
index类型,需要扫描索引上的全部数据。
如上例,id是主键,该count查询需要通过扫描索引上的全部数据来计数。
画外音:此表为
InnoDB引擎。
它仅比全表扫描快一点。
七、ALL
数据准备:
create table user_ex3 (
id int,
age int
)engine=innodb;
insert into user_ex3 values(1,18);
insert into user_ex3 values(2,20);
insert into user_ex3 values(3,30);
insert into user_ex3 values(4,40);
insert into user_ex3 values(5,50);mysql> explain select * from user,user_ex3 where user.id=user_ex3.id;
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
| 1 | SIMPLE | user | NULL | ALL | PRIMARY | NULL | NULL | NULL | 3 | 100.00 | NULL |
| 1 | SIMPLE | user_ex3 | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 20.00 | Using where; Using join buffer (hash join) |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
2 rows in set, 1 warning (0.00 sec)如果id上不建索引,对于前表的每一行(row),后表都要被全表扫描。
今天这篇文章中,这个相同的join语句出现了三次:
扫描类型为
eq_ref,此时id为主键;扫描类型为
ref,此时id为非唯一普通索引;扫描类型为
ALL,全表扫描,此时id上无索引;
由此可见,建立正确的索引,对数据库性能的提升是多么重要。