EXPLAIN 语句提供有关 MySQL 如何执行语句的信息:The EXPLAIN statement provides
information about how MySQL executes statements:
EXPLAIN可用于SELECT、DELETE、INSERT、REPLACE和UPDATE语句。EXPLAINworks withSELECT,DELETE,INSERT,REPLACE, andUPDATEstatements.当
EXPLAIN与可解释语句一起使用时,MySQL 会显示优化器关于语句执行计划的信息。也就是说,MySQL 会解释它将如何处理该语句,包括有关表如何连接以及以何种顺序连接的信息。有关使用EXPLAIN获取执行计划信息的信息,请参见 第 10.8.2 节,“EXPLAIN 输出格式”。WhenEXPLAINis used with an explainable statement, 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. For information about usingEXPLAINto obtain execution plan information, see Section 10.8.2, “EXPLAIN Output Format”.当
EXPLAIN与FOR CONNECTION connection_id一起使用,而不是与可解释语句一起使用时,它会显示在指定连接中正在执行的语句的执行计划。请参见 第 10.8.4 节,“获取命名连接的执行计划信息”。WhenEXPLAINis used withFOR CONNECTIONrather than an explainable statement, it displays the execution plan for the statement executing in the named connection. See Section 10.8.4, “Obtaining Execution Plan Information for a Named Connection”.connection_id对于
SELECT语句,EXPLAIN会生成额外的执行计划信息,可使用SHOW WARNINGS显示。参见第 10.8.3 节,“扩展 EXPLAIN 输出格式”。ForSELECTstatements,EXPLAINproduces additional execution plan information that can be displayed usingSHOW WARNINGS. See Section 10.8.3, “Extended EXPLAIN Output Format”.EXPLAIN对检查涉及分区表的查询非常有用。参见第 26.3.5 节,“获取分区信息”。EXPLAINis useful for examining queries involving partitioned tables. See Section 26.3.5, “Obtaining Information About Partitions”.FORMAT选项可用于选择输出格式。TRADITIONAL以表格格式显示输出。如果没有指定FORMAT选项,则这是默认格式。JSON格式以 JSON 格式显示信息。TheFORMAToption can be used to select the output format.TRADITIONALpresents the output in tabular format. This is the default if noFORMAToption is present.JSONformat displays the information in JSON format.
借助 EXPLAIN,您可以查看应在表中添加索引的位置,从而通过使用索引查找行使语句执行得更快。您还可以使用 EXPLAIN 检查优化器是否以最佳顺序连接表。要向优化器提供提示,使其使用与 SELECT 语句中表名顺序对应的连接顺序,请以 SELECT STRAIGHT_JOIN 开始语句,而不仅仅是 SELECT。(参见 第 15.2.13 节,“SELECT 语句”。)然而,STRAIGHT_JOIN 可能会阻止索引的使用,因为它禁用了半连接转换。请参阅 使用半连接转换优化 IN 和 EXISTS 子查询谓词。
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. (See
Section 15.2.13, “SELECT Statement”.) However,
STRAIGHT_JOIN may prevent indexes from being
used because it disables semijoin transformations. See
Optimizing IN and EXISTS Subquery Predicates with Semijoin Transformations.
优化器跟踪有时可能提供与 EXPLAIN 补充的信息。然而,优化器跟踪的格式和内容可能会随着版本的不同而发生变化。有关详细信息,请参见 第 10.15 节,“跟踪优化器”。The optimizer trace may sometimes provide information
complementary to that of EXPLAIN.
However, the optimizer trace format and content are subject to
change between versions. For details, see
Section 10.15, “Tracing the Optimizer”.
如果您遇到索引未被使用的问题,而您认为它们应该被使用,请运行 ANALYZE
TABLE 来更新表统计信息,例如键的基数,这些信息可能会影响优化器的选择。请参见 第 15.7.3.1 节,“ANALYZE TABLE 语句”。If you have a problem with indexes not being used when you
believe that they should be, run ANALYZE
TABLE to update table statistics, such as cardinality
of keys, that can affect the choices the optimizer makes. See
Section 15.7.3.1, “ANALYZE TABLE Statement”.
EXPLAIN 也可以用来获取表中列的信息。EXPLAIN
tbl_name 等同于 DESCRIBE
tbl_name 和 SHOW COLUMNS FROM
tbl_name。更多信息,请参见 第 15.8.1 节,“DESCRIBE 语句” 和 第 15.7.7.6 节,“SHOW COLUMNS 语句”。EXPLAIN can also be used to
obtain information about the columns in a table.
EXPLAIN
is synonymous
with tbl_nameDESCRIBE
and
tbl_nameSHOW COLUMNS FROM
. For more
information, see Section 15.8.1, “DESCRIBE Statement”, and
Section 15.7.7.6, “SHOW COLUMNS Statement”.tbl_name