MySQL优化之Explain命令解读

  explain为MySQL提供语句的执行计划信息。可以应用在select、delete、insert、update和place语句上。explain的执行计划,只是作为语句执行过程的一个参考,实际执行的过程不一定和计划完全一致,但是执行计划中透露出的讯息却可以帮助选择更好的索引和写出更优化的查询语句。

EXPLAIN输出项(可参考mysql5.7文档)

备注:当使用FORMAT=JSON, 返回的数据为json结构时,JSON Name为null的不显示。(参考文档:https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain-output-columns)

ColumnJSON NameMeaning
id   select_id   The SELECT identifier  
select_type   None   The SELECT type  
table   table_name   The table for the output row  
partitions   partitions   The matching partitions  
type   access_type   The join type  
possible_keys   possible_keys   The possible indexes to choose  
key   key   The index actually chosen  
key_len   key_length   The length of the chosen key  
ref   ref   The columns compared to the index  
rows   rows   Estimate of rows to be examined  
filtered   filtered   Percentage of rows filtered by table condition  
Extra   None   Additional information  

注意:在5.7以前的版本中,想要显示partitions需要使用explain partitions命令;想要显示filtered需要使用explain extended命令。在5.7版本后,默认explain直接显示partitions和filtered中的信息。
下面说明一下各列含义及可能值:

1、id的含义

  The SELECT identifier. This is the sequential number of the SELECT within the query. The value can be NULL if the row refers to the union result of other rows. In this case, the table column shows a value like <unionM,N> to indicate that the row refers to the union of the rows with id values of M and N.
翻译:id为SELECT的标识符。它是在SELECT查询中的顺序编号。如果这一行表示其他行的union结果,这个值可以为空。在这种情况下,table列会显示为形如<union M,N>,表示它是id为M和N的查询行的联合结果。

  注意:id列数字越大越先执行,如果说数字一样大,那么就从上往下依次执行。

  2、select_type可能出现的情况(来源于Mysql5.7文档) select_type ValueJSON NameMeaning
SIMPLE   None   Simple SELECT (not using UNION or subqueries)  
PRIMARY   None   Outermost SELECT  
UNION   None   Second or later SELECT statement in a UNION  
DEPENDENT UNION   dependent (true)   Second or later SELECT statement in a UNION, dependent on outer query  
UNION RESULT   union_result   Result of a UNION.  
SUBQUERY   None   First SELECT in subquery  
DEPENDENT SUBQUERY   dependent (true)   First SELECT in subquery, dependent on outer query  
DERIVED   None   Derived table SELECT (subquery in FROM clause)  
MATERIALIZED   materialized_from_subquery   Materialized subquery  
UNCACHEABLE SUBQUERY   cacheable (false)   A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query  
UNCACHEABLE UNION   cacheable (false)   The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)  

各项内容含义说明:
A:simple:表示不需要union操作或者不包含子查询的简单select查询。有连接查询时,外层的查询为simple,且只有一个。
B:primary:一个需要union操作或者含有子查询的select,位于最外层的单位查询的select_type即为primary。且只有一个。
C:union:union连接的select查询,除了第一个表外,第二个及以后的表select_type都是union。
D:dependent union:与union一样,出现在union 或union all语句中,但是这个查询要受到外部查询的影响
E:union result:包含union的结果集,在union和union all语句中,因为它不需要参与查询,所以id字段为null
F:subquery:除了from字句中包含的子查询外,其他地方出现的子查询都可能是subquery
G:dependent subquery:与dependent union类似,表示这个subquery的查询要受到外部表查询的影响
H:derived:from字句中出现的子查询。
I:materialized:被物化的子查询
J:UNCACHEABLE SUBQUERY:对于外层的主表,子查询不可被物化,每次都需要计算(耗时操作)
K:UNCACHEABLE UNION:UNION操作中,内层的不可被物化的子查询(类似于UNCACHEABLE SUBQUERY)

3、table

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/4a7ece58618fb88ca9d78e5fb32f438f.html