EXPLAIN 语句会生成额外(“扩展”)信息,这些信息不属于 EXPLAIN 的输出内容,但可以通过在 EXPLAIN 之后执行 SHOW WARNINGS 语句来查看。扩展信息适用于 SELECT、DELETE、INSERT、REPLACE 和 UPDATE 语句。The EXPLAIN statement produces
extra (“extended”) information that is not part of
EXPLAIN output but can be viewed
by issuing a SHOW WARNINGS
statement following EXPLAIN.
Extended information is available for
SELECT,
DELETE,
INSERT,
REPLACE, and
UPDATE statements.
SHOW WARNINGS 输出中的 Message 值显示了优化器如何限定 SELECT 语句中的表名和列名,SELECT 在应用重写和优化规则后的样子,以及有关优化过程的其他可能备注。The Message value in
SHOW WARNINGS output displays how
the optimizer qualifies table and column names in the
SELECT statement, what the
SELECT looks like after the
application of rewriting and optimization rules, and possibly
other notes about the optimization process.
在 EXPLAIN 之后使用 SHOW WARNINGS 语句显示的扩展信息仅针对 SELECT 语句生成。对于其他可解释的语句(DELETE、INSERT、REPLACE 和 UPDATE),SHOW WARNINGS 会显示空结果。The extended information displayable with a
SHOW WARNINGS statement following
EXPLAIN is produced only for
SELECT statements.
SHOW WARNINGS displays an empty
result for other explainable statements
(DELETE,
INSERT,
REPLACE, and
UPDATE).
这是一个扩展的 EXPLAIN 输出示例:Here is an example of extended
EXPLAIN output:
mysql>EXPLAINSELECT t1.a, t1.a IN (SELECT t2.a FROM t2) FROM t1\G*************************** 1. row *************************** id: 1 select_type: PRIMARY table: t1 type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 4 filtered: 100.00 Extra: Using index *************************** 2. row *************************** id: 2 select_type: SUBQUERY table: t2 type: index possible_keys: a key: a key_len: 5 ref: NULL rows: 3 filtered: 100.00 Extra: Using index 2 rows in set, 1 warning (0.00 sec) mysql>SHOW WARNINGS\G*************************** 1. row *************************** Level: Note Code: 1003 Message: /* select#1 */ select `test`.`t1`.`a` AS `a`, <in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 having 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on <auto_key> where ((`test`.`t1`.`a` = `materialized-subquery`.`a`))))) AS `t1.a IN (SELECT t2.a FROM t2)` from `test`.`t1` 1 row in set (0.00 sec)
因为 SHOW
WARNINGS 显示的语句可能包含用于提供查询重写或优化器操作信息的特殊标记,所以该语句不一定是有效的 SQL,并且不打算执行。输出还可能包含带有 Message 值的行,这些值提供了额外的、关于优化器所采取操作的非 SQL 解释性说明。Because the statement displayed by SHOW
WARNINGS may contain special markers to provide
information about query rewriting or optimizer actions, the
statement is not necessarily valid SQL and is not intended to be
executed. The output may also include rows with
Message values that provide additional
non-SQL explanatory notes about actions taken by the optimizer.
下面的列表描述了可能出现在 SHOW
WARNINGS 显示的扩展输出中的特殊标记:The following list describes special markers that can appear in
the extended output displayed by SHOW
WARNINGS:
<auto_key>临时表的自动生成键。An automatically generated key for a temporary table.
<cache>(expr)<cache>(expr)该表达式(例如一个标量子查询)只执行一次,结果值会被保存在内存中以供后续使用。对于由多个值组成的结果,可能会创建一个临时表,并显示为
<temporary table>。The expression (such as a scalar subquery) is executed once and the resulting value is saved in memory for later use. For results consisting of multiple values, a temporary table may be created and<temporary table>is shown instead.<exists>(query fragment)<exists>(query fragment)子查询谓词被转换为
EXISTS谓词,并且子查询被改造以便可以与EXISTS谓词一起使用。The subquery predicate is converted to anEXISTSpredicate and the subquery is transformed so that it can be used together with theEXISTSpredicate.<in_optimizer>(query fragment)<in_optimizer>(query fragment)这是一个对用户没有意义的内部优化器对象。This is an internal optimizer object with no user significance.
<index_lookup>(query fragment)<index_lookup>(query fragment)查询片段使用索引查找来处理,以找到符合条件的行。The query fragment is processed using an index lookup to find qualifying rows.
<if>(condition, expr1, expr2)<if>(condition,expr1,expr2)如果条件为真,则计算
expr1,否则计算expr2。If the condition is true, evaluate toexpr1, otherwiseexpr2.<is_not_null_test>(expr)<is_not_null_test>(expr)测试表达式是否不等于
NULL。A test to verify that the expression does not evaluate toNULL.<materialize>(query fragment)<materialize>(query fragment)使用了子查询物化。Subquery materialization is used.
`materialized-subquery`.col_name`materialized-subquery`.col_name引用内部临时表中用于保存子查询评估结果的列
col_name。A reference to the columncol_namein an internal temporary table materialized to hold the result from evaluating a subquery.<primary_index_lookup>(query fragment)<primary_index_lookup>(query fragment)查询片段使用主键查找来查找符合条件的行。The query fragment is processed using a primary key lookup to find qualifying rows.
<ref_null_helper>(expr)<ref_null_helper>(expr)这是一个对用户没有意义的内部优化器对象。This is an internal optimizer object with no user significance.
/* select#N */ select_stmt/* select#N*/select_stmtSELECT与非扩展EXPLAIN输出中id值为N的行相关联。TheSELECTis associated with the row in non-extendedEXPLAINoutput that has anidvalue ofN.outer_tables semi join (inner_tables)outer_tablessemi join (inner_tables)一个半连接操作。
inner_tables显示未被抽取出的表。参见 使用半连接转换优化 IN 和 EXISTS 子查询谓词。A semijoin operation.inner_tablesshows the tables that were not pulled out. See Optimizing IN and EXISTS Subquery Predicates with Semijoin Transformations.<temporary table><temporary table>这表示一个内部临时表,用于缓存中间结果。This represents an internal temporary table created to cache an intermediate result.
当某些表是const或system类型时,涉及这些表列的表达式会被优化器提前计算,并不包含在显示的语句中。然而,使用FORMAT=JSON时,某些const表访问会显示为使用常量值的ref访问。When some tables are of const
or system type, expressions
involving columns from these tables are evaluated early by the
optimizer and are not part of the displayed statement. However,
with FORMAT=JSON, some
const table accesses are
displayed as a ref access
that uses a const value.