@MySQL explain用法

使用explain(execution plan)关键字可以模拟优化器执行sql查询语句,从而知道mysql是如何处理你的sql语句的,分析你的查询语句或是表结构的性能瓶颈~
在日常工作中,我们会开启慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,这时我们常常用到explain这个命令来查看一个SQL语句的执行计划,查看该SQL语句有没有使用索引,有没有做全表扫描,并做相应的优化~

mysql> explain + sql语句;
The EXPLAIN statement provides information about how MySQL executes statements:

  When you precede a SELECT statement with the keyword EXPLAIN, MySQL displays information from the optimizer about the statement execution plan. 
That is, MySQL explains how it would process the statement, including information about how tables are joined and in which order. With the help of EXPLAIN, you can see where you should add indexes to tables so that the statement executes faster by using indexes to find rows.
You can also use EXPLAIN to check whether the optimizer joins the tables in an optimal order. To give a hint to the optimizer to use a join order
corresponding to the order in which the tables are named in a SELECT statement, begin the statement with SELECT STRAIGHT_JOIN rather than just
SELECT.
mysql> explain select * from tb_item;
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | tb_item | ALL  | NULL          | NULL | NULL    | NULL | 2770 | NULL  |
+----+-------------+---------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)

expain出来的信息有10列,分别是id、select_type、table、type、possible_keys、key、key_len、ref、rows、Extra,下面对这些字段出现的可能进行解释:

一、 id

id:select查询的序列号,代表查询中执行select子句或加载表的顺序,SQL从大到小的执行,有三种情况

1. id相同时,执行顺序由上至下,(看第三列table,mysql查询优化器读取表的顺序是t1,t3,r2)

EXPLAIN SELECT t2.*
FROM t1,t2,t3
WHERE t1.`id`=t2.`id`AND t1.`id`=t3.`id`
AND t1.`name`='张三'
+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref        | rows | Extra       |
+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------------+
|  1 | SIMPLE      | t1    | ALL    | PRIMARY       | NULL    | NULL    | NULL       |    1 | Using where |
|  1 | SIMPLE      | t3    | eq_ref | PRIMARY       | PRIMARY | 4       | test.t1.id |    1 | NULL        |
|  1 | SIMPLE      | t2    | eq_ref | PRIMARY       | PRIMARY | 4       | test.t1.id |    1 | Using index |
+----+-------------+-------+--------+---------------+---------+---------+------------+------+-------------+

2. 如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行

EXPLAIN SELECT t2.*
FROM t2
WHERE id=(SELECT id
          FROM t1
          WHERE id=(SELECT t3.`id`
                    FROM t3
                    WHERE t3.`name`='张三'));
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|  1 | PRIMARY     | t2    | const | PRIMARY       | PRIMARY | 4       | const |    1 | NULL        |
|  2 | SUBQUERY    | t1    | const | PRIMARY       | PRIMARY | 4       | const |    1 | Using index |
|  3 | SUBQUERY    | t3    | ALL   | NULL          | NULL    | NULL    | NULL  |    1 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
3 rows in set (0.00 sec)

读取表的顺序分别是t3-->t1-->t2,很好理解~

3.id如果相同,可以认为是一组,从上往下顺序执行;在所有组中,id值越大,优先级越高,越先执行

EXPLAIN SELECT t2.* FROM (
SELECT t3.`id`
FROM t3
WHERE t3.`name`='张三')s1,t2
WHERE s1.id=t2.id
+----+-------------+------------+------+---------------+-------------+---------+------------+------+-------------+
| id | select_type | table      | type | possible_keys | key         | key_len | ref        | rows | Extra       |
+----+-------------+------------+------+---------------+-------------+---------+------------+------+-------------+
|  1 | PRIMARY     | t2         | ALL  | PRIMARY       | NULL        | NULL    | NULL       |    1 | NULL        |
|  1 | PRIMARY     | <derived2> | ref  | <auto_key0>   | <auto_key0> | 4       | test.t2.id |    2 | Using index |
|  2 | DERIVED     | t3         | ALL  | NULL          | NULL        | NULL    | NULL       |    1 | Using where |
+----+-------------+------------+------+---------------+-------------+---------+------------+------+-------------+
derived2就表示表s1

二、select_type是查询中每个select子句的类型

主要用于区别普通查询,联合查询,子查询等复杂查询

(1) SIMPLE(简单SELECT,不使用UNION或子查询等)

(2) PRIMARY(查询中若包含任何复杂的子部分,最外层的select被标记为PRIMARY),看id的第二种情况

(3) UNION(UNION中的第二个或后面的SELECT语句)

(4) DEPENDENT UNION(UNION中的第二个或后面的SELECT语句,取决于外面的查询)

(5) UNION RESULT(UNION的结果)

explain select * from tbl_dept a left join tbl_emp b  on a.id=b.deptId  union select * from tbl_dept a right join tbl_emp b  on a.id=b.deptId;
+----+--------------+------------+------+---------------+------------+---------+-------------+------+----------------------------------------------------+
| id | select_type  | table      | type | possible_keys | key        | key_len | ref         | rows | Extra                                              |
+----+--------------+------------+------+---------------+------------+---------+-------------+------+----------------------------------------------------+
|  1 | PRIMARY      | a          | ALL  | NULL          | NULL       | NULL    | NULL        |    5 | NULL                                               |
|  1 | PRIMARY      | b          | ref  | fk_dept_id    | fk_dept_id | 5       | db0629.a.id |    1 | NULL                                               |
|  2 | UNION        | b          | ALL  | NULL          | NULL       | NULL    | NULL        |    8 | NULL                                               |
|  2 | UNION        | a          | ALL  | PRIMARY       | NULL       | NULL    | NULL        |    5 | Using where; Using join buffer (Block Nested Loop) |
|NULL| UNION RESULT | <union1,2> | ALL  | NULL          | NULL       | NULL    | NULL        | NULL | Using temporary                                    |
+----+--------------+------------+------+---------------+------------+---------+-------------+------+----------------------------------------------------+
5 rows in set (0.09 sec)

(6) SUBQUERY(子查询)

(7) DEPENDENT SUBQUERY(子查询中的第一个SELECT,取决于外面的查询)

(8) DERIVED(派生表的SELECT, FROM子句的子查询)

(9) UNCACHEABLE SUBQUERY(一个子查询的结果不能被缓存,必须重新评估外链接的第一行)

三、table

显示这一行的数据属于哪张表,有时不是真实的表名字,看到的是derivedx(x是个数字,表示是第几步执行的结果)

mysql> explain select * from (select * from ( select * from t1 where id=2602) a) b;
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
| id | select_type | table      | type   | possible_keys     | key     | key_len | ref  | rows | Extra |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+
|  1 | PRIMARY     | <derived2> | system | NULL              | NULL    | NULL    | NULL |    1 |       |
|  2 | DERIVED     | <derived3> | system | NULL              | NULL    | NULL    | NULL |    1 |       |
|  3 | DERIVED     | t1         | const  | PRIMARY,idx_t1_id | PRIMARY | 4       |      |    1 |       |
+----+-------------+------------+--------+-------------------+---------+---------+------+------+-------+

四、type

表示MySQL在表中找到所需行的方式,又称"访问类型"。

常用的类型有(8): ALL, index,  range, ref, eq_ref, const, system, NULL

最好到最差依次是system>const>eq_ref>ref>range>index>ALL

ALL:Full Table Scan, MySQL将全表扫描以找到匹配的行

index: Full Index Scan,index与ALL区别为index类型只遍历索引树,这通常比All快,因为索引文件通常比数据文件小。也就是说虽然all和index都是读全表,但是index是从索引中读取的,而all是从硬盘中读的~

range:只检索给定范围的行,使用一个索引来选择行,key列显示使用了哪个索引,一般就是在你的where语句中出现了between,<,>,in等的查询,不需要全表扫描~

ref: 非唯一性索引访问

eq_ref: 类似ref,使用唯一性索引查找(主键或唯一索引),对于每个索引键值,表中只有一条记录匹配,简单来说,就是多表连接中使用primary key或者 unique key作为关联条件

const、system: 表示通过索引一次就找到了,只匹配一行数据。如将主键置于where列表中,MySQL就能将该查询转换为一个常量,system是const类型的特例,当查询的表只有一行的情况下,使用system

EXPLAIN SELECT *
FROM tb_item
WHERE id=536563
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table   | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | tb_item | const | PRIMARY       | PRIMARY | 8       | const |    1 | NULL  |
+----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

用主键查

NULL: MySQL在优化过程中分解语句,不再需要访问表或索引,例如从一个索引列里选取最小值可以通过单独索引查找完成。

五、possible_keys

可能被用到的索引。

该列完全独立于EXPLAIN输出所示的表的次序。这意味着在possible_keys中的某些键实际上不能按生成的表次序使用。
如果该列是NULL,则没有相关的索引。在这种情况下,可以通过检查WHERE子句看是否它引用某些列或适合索引的列来提高你的查询性能。如果是这样,创造一个适当的索引并且再次用EXPLAIN检查查询

六、Key

查询过程中实际用到的索引

如果没有选择索引,键是NULL。要想强制MySQL使用或忽视possible_keys列中的索引,在查询中使用FORCE INDEX、USE INDEX或者IGNORE INDEX。

+----+-------------+---------+
|   possible_keys  |   key   | 
+------------------+---------+
| PRIMARY,idx_t1   | idx_t1  |
+----+-------------+---------+
+-------------+---------------------+
|possible_keys|        key          | 
+-------------+---------------------+
|    NULL     | idx_column1_column2 |
+-------------+---------------------+

七、key_len

key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的

不损失精确性的情况下,长度越短越好 

八、ref

表示上述表的连接匹配条件,即哪些列或常量被用于查找索引列上的值

九、rows

 表示MySQL估算的找到所需的记录所需要读取的行数

十、Extra

该列包含MySQL解决查询的详细信息,有以下几种情况:

Using where:列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的,这发生在对表的全部的请求列都是同一个索引的部分的时候,表示mysql服务器将在存储引擎检索行后再进行过滤

Using temporary:表示MySQL需要使用临时表来存储结果集,常见于排序和分组查询

Using filesort:MySQL中无法利用索引完成的排序操作称为“文件排序”

Using join buffer:改值强调了在获取连接条件时没有使用索引,并且需要连接缓冲区来存储中间结果。如果出现了这个值,那应该注意,根据查询的具体情况可能需要添加索引来改进能。

Impossible where:这个值强调了where语句会导致没有符合条件的行。

Select tables optimized away:这个值意味着仅通过使用索引,优化器可能仅从聚合函数结果中返回一行

原文地址:https://www.cnblogs.com/winner-0715/p/6534528.html