MySQL explain 详解
-
explain语法 -
explain输出-
id -
select_type-
SIMPLE -
PRIMARY -
UNION -
DEPENDENT UNION -
UNION RESULT -
SUBQUERY -
DEPENDENT SUBQUERY -
DERIVED -
MATERIALIZED -
UNCACHEABLE SUBQUERY -
UNCACHEABLE UNION
-
-
table -
partitions -
type-
system -
const -
eq_ref -
ref -
fulltext -
ref_or_null -
index_merge -
unique_subquery -
index_subquery -
range -
index -
ALL
-
-
possible_keys -
key -
key_len -
ref -
rows -
filtered -
extra-
Child of 'table' pushed join@1 -
const row not found -
Deleting all rows -
Distinct -
FirstMatch -
Full scan on NULL key -
Impossible HAVING -
Impossible WHERE -
Impossible WHERE noticed after reading const tables -
LooseScan(m..n) -
No matching min/max row -
no matching row in const table -
No matching rows after partition pruning -
No tables used -
Not exists -
Plan isn’t ready yet -
Range checked for each record (index map: N) -
Scanned N databases -
Select tables optimized away -
Skip_open_table, Open_frm_only, Open_trigger_only, Open_full_table-
Skip_open_table -
Open_frm_only -
Open_trigger_only -
Open_full_table
-
-
Start temporary, End temporary -
unique row not found -
Using filesort -
Using index -
Using index condition -
Using index for group-by -
Using join buffer (Block Nested Loop), Using join buffer (Batched Key Access) -
Using MRR -
Using sort_union(…), Using union(…), Using intersect(…) -
Using temporary -
Using where -
Using where with pushed condition -
Zero limit
-
-
EXPLAIN 语法
DESCRIBE 和 EXPLAIN 是同义词。在实践中,DESCRIBE 多用于显示表结构,而 EXPLAIN 多用于显示 SQL 语句的执行计划。
{EXPLAIN | DESCRIBE | DESC}
tbl_name [col_name | wild]
{EXPLAIN | DESCRIBE | DESC}
[explain_type]
{explainable_stmt | FOR CONNECTION connection_id}
explain_type: {
EXTENDED
| PARTITIONS
| FORMAT = format_name
}
format_name: {
TRADITIONAL
| JSON
}
explainable_stmt: {
SELECT statement
| DELETE statement
| INSERT statement
| REPLACE statement
| UPDATE statement
}
EXPLAIN FORMAT = JSON SELECT DISTINCT (m.user_id)
FROM user_extdata m
WHERE m.city_id IN (1, 2, 3, 4) ;
EXPLAIN 输出
select_type
SIMPLE-
简单SELECT,不使用UNION或子查询等
PRIMARY-
查询中若包含任何复杂的子部分,最外层的select被标记为PRIMARY
UNION-
UNION中的第二个或后面的SELECT语句
DEPENDENT UNION-
UNION中的第二个或后面的SELECT语句,取决于外面的查询
UNION RESULT-
UNION的结果
SUBQUERY-
子查询中的第一个SELECT
DEPENDENT SUBQUERY-
子查询中的第一个SELECT,取决于外面的查询
DERIVED-
派生表的SELECT, FROM子句的子查询
MATERIALIZEDUNCACHEABLE SUBQUERY-
一个子查询的结果不能被缓存,必须重新评估外链接的第一行
UNCACHEABLE UNION-
===
table
type
system-
当MySQL对查询某部分进行优化,并转换为一个常量时,使用这些类型访问。如将主键置于where列表中,MySQL就能将该查询转换为一个常量,system是const类型的特例,当查询的表只有一行的情况下,使用system
consteq_ref-
类似ref,区别就在使用的索引是唯一索引,对于每个索引键值,表中只有一条记录匹配,简单来说,就是多表连接中使用primary key或者 unique key作为关联条件
ref-
表示上述表的连接匹配条件,即哪些列或常量被用于查找索引列上的值
fulltextref_or_null-
MySQL在优化过程中分解语句,执行时甚至不用访问表或索引,例如从一个索引列里选取最小值可以通过单独索引查找完成。
index_mergeunique_subqueryindex_subqueryrange-
只检索给定范围的行,使用一个索引来选择行
index-
Full Index Scan,index与ALL区别为index类型只遍历索引树
ALL-
Full Table Scan, MySQL将遍历全表以找到匹配的行

