MySQL

MySQL 04 - MySQL Explain

简介:本文主要讲述如何通过explain命令获取select语句的执行计划,通过explain我们可以知道以下信息:表的读取顺序,数据读取操作的类型,哪些索引可以使用,哪些索引实际使用了,表之间的引用,每张表有多少行被优化器查询等信息。

1. Explain简介

本文主要讲述如何通过explain命令获取select语句的执行计划,通过explain我们可以知道以下信息:表的读取顺序,数据读取操作的类型,哪些索引可以使用,哪些索引实际使用了,表之间的引用,每张表有多少行被优化器查询等信息。

注:在本文中使用的测试数据来自于MySQL官方提供的测试数据,Github地址为datacharmer/test_db,请自行下载导入。

下面是使用explain的例子:

select语句之前增加explain关键字,MySQL会在查询上设置一个标记,执行查询时,会返回执行计划的信息,而不是执行这条SQL(如果from中包含子查询,仍会执行该子查询,将结果放入临时表中)。

  • mysql> explain select * from departments;
  • +----+-------------+-------------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-------------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
  • | 1 | SIMPLE | departments | NULL | index | NULL | dept_name | 42 | NULL | 9 | 100.00 | Using index |
  • +----+-------------+-------------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
  • 1 row in set, 1 warning (0.00 sec)

在查询中的每个表会输出一行,如果有两个表通过join连接查询,那么会输出两行。表的意义相当广泛:可以是子查询、一个union结果等。

explain有两个变种:

  1. explain extended:会在explain的基础上额外提供一些查询优化的信息。紧随其后通过show warnings命令可以得到优化后的查询语句,从而看出优化器优化了什么。额外还有 filtered列,是一个半分比的值,rows * filtered / 100可以估算出将要和explain中前一个表进行连接的行数(前一个表指explain中的id值比当前表id值小的表)。
  • mysql> explain extended select * from departments where dept_no = 'd001';
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
  • | 1 | SIMPLE | departments | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
  • 1 row in set, 2 warnings (0.00 sec)
  • mysql> show warnings;
  • +---------+------+-------------------------------------------------------------------------------------------------------------+
  • | Level | Code | Message |
  • +---------+------+-------------------------------------------------------------------------------------------------------------+
  • | Warning | 1681 | 'EXTENDED' is deprecated and will be removed in a future release. |
  • | Note | 1003 | /* select#1 */ select 'd001' AS `dept_no`,'Marketing' AS `dept_name` from `employees`.`departments` where 1 |
  • +---------+------+-------------------------------------------------------------------------------------------------------------+
  • 2 rows in set (0.00 sec)
  1. explain partitions:相比explain多了个partitions字段,如果查询是基于分区表的话,会显示查询将访问的分区。

注:MySQL 5.7版本缺省会带有filtered和partitions列。

2. Explain中的列

接下来我们将展示Explain中每个列的信息。

2.1. id列

id列的编号是select的序列号,有几个select就有几个id,并且id的顺序是按select出现的顺序增长的。MySQL将select查询分为简单查询和复杂查询。复杂查询分为三类:简单子查询、派生表(from语句中的子查询)、union查询。

  1. 简单子查询
  • mysql> explain select (select 1 from employees limit 1) from departments;
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • | 1 | PRIMARY | departments | NULL | index | NULL | PRIMARY | 4 | NULL | 9 | 100.00 | Using index |
  • | 2 | SUBQUERY | employees | NULL | index | NULL | PRIMARY | 4 | NULL | 299069 | 100.00 | Using index |
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • 2 rows in set, 1 warning (0.00 sec)
  1. from子句中的子查询
  • mysql> explain select * from (select gender, count(1) from employees group by gender) as e;
  • +----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+---------------------------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+---------------------------------+
  • | 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 299069 | 100.00 | NULL |
  • | 2 | DERIVED | employees | NULL | ALL | NULL | NULL | NULL | NULL | 299069 | 100.00 | Using temporary; Using filesort |
  • +----+-------------+------------+------------+------+---------------+------+---------+------+--------+----------+---------------------------------+
  • 2 rows in set, 1 warning (0.00 sec)

这个查询执行时有个临时表别名为e,外部select查询引用了这个临时表,<derived2>中的数字对应于id列的值。

  1. union查询
  • mysql> explain select 1 union select 1;
  • +------+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +------+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
  • | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
  • | 2 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
  • | NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |
  • +------+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
  • 3 rows in set (0.02 sec)

union结果总是放在一个匿名临时表中,临时表不在SQL总出现,因此它的id是NULL。

2.2. select_type列

select_type表示对应行是简单还是复杂的查询,如果是复杂的查询,又是上述三种复杂查询中的哪一种。

  1. SIMPLE:简单查询,查询不包含子查询和union。
  • mysql> explain select * from employees where emp_no > 10010;
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • | 1 | SIMPLE | employees | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 149534 | 100.00 | Using where |
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • 1 row in set, 1 warning (0.01 sec)
  1. PRIMARY:复杂查询中最外层的select。
  2. SUBQUERY:包含在select中的子查询(不在from子句中)。
  3. DERIVED:包含在from子句中的子查询。MySQL会将结果存放在一个临时表中,也称为派生表(derived的英文含义)。

用这个例子来了解PRIMARY、SUBQUERY和DERIVED类型:

  • mysql> explain select (select dept_name from departments where dept_no = 'd008'), e.first_name, count(1) from (select * from employees group by gender) e;
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+-------+--------+----------+---------------------------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+-------+--------+----------+---------------------------------+
  • | 1 | PRIMARY | <derived3> | NULL | ALL | NULL | NULL | NULL | NULL | 299069 | 100.00 | NULL |
  • | 3 | DERIVED | employees | NULL | ALL | NULL | NULL | NULL | NULL | 299069 | 100.00 | Using temporary; Using filesort |
  • | 2 | SUBQUERY | departments | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
  • +----+-------------+-------------+------------+-------+---------------+---------+---------+-------+--------+----------+---------------------------------+
  • 3 rows in set, 1 warning (0.00 sec)
  1. UNION:在union中的第二个和随后的select。
  2. UNION RESULT:从union临时表检索结果的select。

用这个例子来了解UNION和UNION RESULT类型:

  • mysql> explain select 1 union select 1;
  • +------+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +------+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
  • | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
  • | 2 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
  • | NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |
  • +------+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
  • 3 rows in set (0.02 sec)

2.3. table列

这一列表示explain的一行正在访问哪个表。当from子句中有子查询时,table列是<derivenN>格式,表示当前查询依赖id为N的查询,于是先执行id为N的查询。当有union时,UNION RESULT的table列的值为<union1,2>,1和2表示参与union的select行id。

2.4. partitions列

分区中的记录将被查询相匹配。显示此列仅在使用分区关键字。该值为NULL对于非分区表。

2.5. type列

这一列表示关联类型或访问类型,即MySQL决定如何查找表中的行。依次从最优到最差分别为:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL。

  1. NULL:MySQL能够在优化阶段分解查询语句,在执行阶段用不着再访问表或索引。例如,在索引列中选取最小值,可以单独查找索引来完成,不需要在执行时访问表
  • mysql> explain select min(emp_no) from employees;
  • +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
  • | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
  • +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
  • 1 row in set, 1 warning (0.00 sec)
  1. const、system:MySQL能对查询的某部分进行优化并将其转化成一个常量(可以看show warnings的结果)。用于primary key或unique key的所有列与常数比较时,所以表最多有一个匹配行,读取1次,速度比较快。
  • mysql> explain extended select * from (select gender, count(1) from employees where emp_no = 10011) as e;
  • +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------+
  • | 1 | PRIMARY | <derived2> | NULL | system | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
  • | 2 | DERIVED | employees | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
  • +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------+
  • 2 rows in set, 2 warnings (0.00 sec)
  • mysql> show warnings;
  • +---------+------+-------------------------------------------------------------------+
  • | Level | Code | Message |
  • +---------+------+-------------------------------------------------------------------+
  • | Warning | 1681 | 'EXTENDED' is deprecated and will be removed in a future release. |
  • | Note | 1003 | /* select#1 */ select 'F' AS `gender`,'1' AS `count(1)` from dual |
  • +---------+------+-------------------------------------------------------------------+
  • 2 rows in set (0.00 sec)
  1. eq_ref:primary key或unique key索引的所有部分被连接使用,最多只会返回一条符合条件的记录。这可能是在const之外最好的联接类型了,简单的select查询不会出现这种type。
  • mysql> explain select * from dept_emp left join employees on employees.emp_no = dept_emp.emp_no;
  • +----+-------------+-----------+------------+--------+---------------+---------+---------+---------------------------+--------+----------+-------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+--------+---------------+---------+---------+---------------------------+--------+----------+-------+
  • | 1 | SIMPLE | dept_emp | NULL | ALL | NULL | NULL | NULL | NULL | 331570 | 100.00 | NULL |
  • | 1 | SIMPLE | employees | NULL | eq_ref | PRIMARY | PRIMARY | 4 | employees.dept_emp.emp_no | 1 | 100.00 | NULL |
  • +----+-------------+-----------+------------+--------+---------------+---------+---------+---------------------------+--------+----------+-------+
  • 2 rows in set, 1 warning (0.00 sec)
  1. ref:相比eq_ref,不使用唯一索引,而是使用普通索引或者唯一性索引的部分前缀,索引要和某个值相比较,可能会找到多个符合条件的行。
  • 简单select查询,name是普通索引(非唯一索引)。
  • mysql> create index idx_name on employees(first_name);
  • Query OK, 0 rows affected (0.80 sec)
  • Records: 0 Duplicates: 0 Warnings: 0
  • mysql> explain select * from employees where first_name = 'Mary';
  • +----+-------------+-----------+------------+------+---------------+----------+---------+-------+------+----------+-------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+------+---------------+----------+---------+-------+------+----------+-------+
  • | 1 | SIMPLE | employees | NULL | ref | idx_name | idx_name | 16 | const | 224 | 100.00 | NULL |
  • +----+-------------+-----------+------------+------+---------------+----------+---------+-------+------+----------+-------+
  • 1 row in set, 1 warning (0.00 sec)
  • 关联表查询,idx_emp_no__dept_no是dept_emp表的emp_no和dept_no两个字段的联合唯一索引,这里使用到了idx_emp_no__dept_no的左边前缀emp_no部分。
  • mysql> show index from dept_emp;
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | dept_emp | 1 | idx_emp_no__dept_no | 1 | emp_no | A | 299815 | NULL | NULL | YES | BTREE | | |
  • | dept_emp | 1 | idx_emp_no__dept_no | 2 | dept_no | A | 331008 | NULL | NULL | YES | BTREE | | |
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • 2 rows in set (0.00 sec)
  • mysql> explain select * from employees left join dept_emp on employees.emp_no = dept_emp.emp_no;
  • +----+-------------+-----------+------------+------+---------------------+---------------------+---------+----------------------------+--------+----------+-------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+------+---------------------+---------------------+---------+----------------------------+--------+----------+-------+
  • | 1 | SIMPLE | employees | NULL | ALL | NULL | NULL | NULL | NULL | 299069 | 100.00 | NULL |
  • | 1 | SIMPLE | dept_emp | NULL | ref | idx_emp_no__dept_no | idx_emp_no__dept_no | 5 | employees.employees.emp_no | 1 | 100.00 | NULL |
  • +----+-------------+-----------+------------+------+---------------------+---------------------+---------+----------------------------+--------+----------+-------+
  • 2 rows in set, 1 warning (0.00 sec)
  1. ref_or_null:类似ref,但是可以搜索值为NULL的行。
  • mysql> show index from dept_emp;
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | dept_emp | 1 | idx_emp_no__dept_no | 1 | emp_no | A | 299815 | NULL | NULL | YES | BTREE | | |
  • | dept_emp | 1 | idx_emp_no__dept_no | 2 | dept_no | A | 331008 | NULL | NULL | YES | BTREE | | |
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • 2 rows in set (0.00 sec)
  • mysql> explain select * from dept_emp where emp_no = 10010 or emp_no is null;
  • +----+-------------+----------+------------+-------------+---------------------+---------------------+---------+-------+------+----------+-----------------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+----------+------------+-------------+---------------------+---------------------+---------+-------+------+----------+-----------------------+
  • | 1 | SIMPLE | dept_emp | NULL | ref_or_null | idx_emp_no__dept_no | idx_emp_no__dept_no | 5 | const | 3 | 100.00 | Using index condition |
  • +----+-------------+----------+------------+-------------+---------------------+---------------------+---------+-------+------+----------+-----------------------+
  • 1 row in set, 1 warning (0.00 sec)
  1. index_merge:表示使用了索引合并的优化方法。 例如下表中emp_no是主键,idx_name是first_name的普通索引。or操作的时候没有用主键,而是使用了主键和first_name索引:
  • mysql> show index from employees;
  • +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  • +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | employees | 0 | PRIMARY | 1 | emp_no | A | 299069 | NULL | NULL | | BTREE | | |
  • | employees | 1 | idx_name | 1 | first_name | A | 1297 | NULL | NULL | | BTREE | | |
  • +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • 2 rows in set (0.00 sec)
  • mysql> explain select * from employees where emp_no = 10023 or first_name = 'Mary';
  • +----+-------------+-----------+------------+-------------+------------------+------------------+---------+------+------+----------+--------------------------------------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+-------------+------------------+------------------+---------+------+------+----------+--------------------------------------------+
  • | 1 | SIMPLE | employees | NULL | index_merge | PRIMARY,idx_name | PRIMARY,idx_name | 4,16 | NULL | 225 | 100.00 | Using union(PRIMARY,idx_name); Using where |
  • +----+-------------+-----------+------------+-------------+------------------+------------------+---------+------+------+----------+--------------------------------------------+
  • 1 row in set, 1 warning (0.00 sec)
  1. range:范围扫描通常出现在in()between><>=等操作中,使用一个索引来检索给定范围的行。
  • mysql> explain select * from emp where id > 2;
  • +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
  • | 1 | SIMPLE | emp | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 4 | 100.00 | Using where |
  • +----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
  • 1 row in set, 1 warning (0.00 sec)
  1. index:和ALL一样,不同就是MySQL只需扫描索引树,这通常比ALL快一些。
  • mysql> explain select count(1) from employees;
  • +----+-------------+-----------+------------+-------+---------------+----------+---------+------+--------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+-------+---------------+----------+---------+------+--------+----------+-------------+
  • | 1 | SIMPLE | employees | NULL | index | NULL | idx_name | 16 | NULL | 299069 | 100.00 | Using index |
  • +----+-------------+-----------+------------+-------+---------------+----------+---------+------+--------+----------+-------------+
  • 1 row in set, 1 warning (0.00 sec)
  1. ALL:即全表扫描,意味着mysql需要从头到尾去查找所需要的行。
  • mysql> explain select * from employees;
  • +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------+
  • | 1 | SIMPLE | employees | NULL | ALL | NULL | NULL | NULL | NULL | 299069 | 100.00 | NULL |
  • +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------+
  • 1 row in set, 1 warning (0.00 sec)

2.6. possible_keys列

这一列显示查询可能使用哪些索引来查找。可能出现possible_keys列有值,而key显示NULL的情况,这种情况是因为表中数据不多,MySQL认为索引对此查询帮助不大,选择了全表查询。 如果该列是NULL,则没有相关的索引。在这种情况下,可以通过检查where子句看是否可以创造一个适当的索引来提高查询性能,然后用explain查看效果。

2.7. key列

这一列显示mysql实际采用哪个索引来优化对该表的访问。如果没有使用索引,则该列是NULL。如果想强制MySQL使用或忽视possible_keys列中的索引,在查询中使用force indexignore index

2.8. key_len列

这一列显示了mysql在索引里使用的字节数,通过这个值可以算出具体使用了索引中的哪些列。

如,在上面的dept_emp表的联合索引idx_emp_no__dept_no由emp_no(char类型,可为NULL)和dept_no(int类型,可为NULL)组成,并且每个char是4字节,每个int是4字节。通过结果中的key_len为5可推断出查询使用了emp_no列来执行索引查找:

  • mysql> show index from dept_emp;
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | dept_emp | 1 | idx_emp_no__dept_no | 1 | emp_no | A | 299794 | NULL | NULL | YES | BTREE | | |
  • | dept_emp | 1 | idx_emp_no__dept_no | 2 | dept_no | A | 331008 | NULL | NULL | YES | BTREE | | |
  • +----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • 2 rows in set (0.00 sec)
  • mysql> explain select * from dept_emp where emp_no = 100120;
  • +----+-------------+----------+------------+------+---------------------+---------------------+---------+-------+------+----------+-------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+----------+------------+------+---------------------+---------------------+---------+-------+------+----------+-------+
  • | 1 | SIMPLE | dept_emp | NULL | ref | idx_emp_no__dept_no | idx_emp_no__dept_no | 5 | const | 1 | 100.00 | NULL |
  • +----+-------------+----------+------------+------+---------------------+---------------------+---------+-------+------+----------+-------+
  • 1 row in set, 1 warning (0.00 sec)

key_len计算规则如下:

  • char(n):n字节长度
  • varchar(n):2字节存储字符串长度,如果是UTF-8,则长度3n + 2
  • tinyint:1字节
  • smallint:2字节
  • int:4字节
  • bigint:8字节  
  • date:3字节
  • timestamp:4字节
  • datetime:8字节

如果字段允许为 NULL,需要1字节记录是否为NULL。

索引最大长度是768字节,当字符串过长时,MySQL会做一个类似左前缀索引的处理,将前半部分的字符提取出来做索引。

2.9. ref列

这一列显示了在key列记录的索引中,表查找值所用到的列或常量,常见的有const(常量)、func、NULL、字段名(以库.表.字段的方式显示,如employees.employees.emp_no)。

2.10. rows列

这一列是mysql估计要读取并检测的行数,注意这个不是结果集里的行数。

2.11. filtered列

如果你用EXPLAIN EXTENDED将会展示出这列filtered(MySQL 5.7缺省就会输出filtered),它指返回结果的行占需要读到的行(rows列的值)的百分比。

2.12. Extra列

这一列展示的是额外信息,常见的重要值如下:

  • distinct: 一旦找到了与行相联合匹配的行,就不再搜索了。
  • mysql> explain select distinct first_name from employees left join dept_emp on employees.emp_no = dept_emp.emp_no;
  • +----+-------------+-----------+------------+-------+---------------------+---------------------+---------+----------------------------+--------+----------+------------------------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+-------+---------------------+---------------------+---------+----------------------------+--------+----------+------------------------------+
  • | 1 | SIMPLE | employees | NULL | index | idx_name | idx_name | 16 | NULL | 299069 | 100.00 | Using index; Using temporary |
  • | 1 | SIMPLE | dept_emp | NULL | ref | idx_emp_no__dept_no | idx_emp_no__dept_no | 5 | employees.employees.emp_no | 1 | 100.00 | Using index; Distinct |
  • +----+-------------+-----------+------------+-------+---------------------+---------------------+---------+----------------------------+--------+----------+------------------------------+
  • 2 rows in set, 1 warning (0.00 sec)
  • Using index:这发生在对表的请求列都是同一索引的部分的时候,返回的列数据只使用了索引中的信息,而没有再去访问表中的行记录。是性能高的表现。
  • mysql> explain select emp_no from employees order by emp_no;
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • | 1 | SIMPLE | employees | NULL | index | NULL | PRIMARY | 4 | NULL | 299069 | 100.00 | Using index |
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • 1 row in set, 1 warning (0.00 sec)
  • Using where:MySQL将在存储引擎检索行后再进行过滤。就是先读取整行数据,再按where条件进行检查,符合就留下,不符合就丢弃。
  • mysql> explain select * from employees where emp_no > 11000;
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • | 1 | SIMPLE | employees | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 149534 | 100.00 | Using where |
  • +----+-------------+-----------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
  • 1 row in set, 1 warning (0.00 sec)
  • Using temporary:MySQL需要创建一张临时表来处理查询,出现这种情况一般是要进行优化的,首先是想到用索引来优化。

dept_emp表的emp_no字段没有索引,此时创建了张临时表来进行distinct操作:

  • mysql> explain select distinct emp_no from dept_emp;
  • +----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-----------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-----------------+
  • | 1 | SIMPLE | dept_emp | NULL | ALL | NULL | NULL | NULL | NULL | 331008 | 100.00 | Using temporary |
  • +----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+-----------------+
  • 1 row in set, 1 warning (0.00 sec)

为dept_emp表的emp_no字段建立了idx_emp_no索引,此时查询时Extra是Using index,没有用临时表:

  • mysql> show index from dept_emp;
  • +----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  • +----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | dept_emp | 1 | idx_emp_no | 1 | emp_no | A | 299727 | NULL | NULL | YES | BTREE | | |
  • +----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • 1 row in set (0.00 sec)
  • mysql> explain select distinct emp_no from dept_emp;
  • +----+-------------+----------+------------+-------+---------------+------------+---------+------+--------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+----------+------------+-------+---------------+------------+---------+------+--------+----------+-------------+
  • | 1 | SIMPLE | dept_emp | NULL | index | idx_emp_no | idx_emp_no | 5 | NULL | 331008 | 100.00 | Using index |
  • +----+-------------+----------+------------+-------+---------------+------------+---------+------+--------+----------+-------------+
  • 1 row in set, 1 warning (0.00 sec)
  • Using filesort:MySQL会对结果使用一个外部索引排序,而不是按索引次序从表里读取行。此时MySQL会根据连接类型浏览所有符合条件的记录,并保存排序关键字和行指针,然后排序关键字并按顺序检索行信息。这种情况下一般也是要考虑使用索引来优化的。

dept_emp表的emp_no字段没有索引,会浏览dept_emp整个表,保存排序关键字emp_no,然后排序name并检索行记录:

  • mysql> explain select emp_no from dept_emp order by emp_no;
  • +----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------+
  • | 1 | SIMPLE | dept_emp | NULL | ALL | NULL | NULL | NULL | NULL | 331008 | 100.00 | Using filesort |
  • +----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------+
  • 1 row in set, 1 warning (0.00 sec)

为dept_emp表的emp_no字段建立了idx_emp_no索引,此时查询时Extra是Using index:

  • mysql> show index from dept_emp;
  • +----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
  • +----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • | dept_emp | 1 | idx_emp_no | 1 | emp_no | A | 299626 | NULL | NULL | YES | BTREE | | |
  • +----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
  • 1 row in set (0.00 sec)
  • mysql> explain select emp_no from dept_emp order by emp_no;
  • +----+-------------+----------+------------+-------+---------------+------------+---------+------+--------+----------+-------------+
  • | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  • +----+-------------+----------+------------+-------+---------------+------------+---------+------+--------+----------+-------------+
  • | 1 | SIMPLE | dept_emp | NULL | index | NULL | idx_emp_no | 5 | NULL | 331008 | 100.00 | Using index |
  • +----+-------------+----------+------------+-------+---------------+------------+---------+------+--------+----------+-------------+
  • 1 row in set, 1 warning (0.00 sec)