The EXPLAIN
statement provides information about how MySQL executes statements:EXPLAIN
语句提供有关MySQL如何执行语句的信息:
EXPLAIN
works with SELECT
, DELETE
, INSERT
, REPLACE
, and UPDATE
statements.EXPLAIN
使用SELECT
、DELETE
、INSERT
、REPLACE
和UPDATE
语句。
When 当EXPLAIN
is used with an explainable statement, MySQL displays information from the optimizer about the statement execution plan. EXPLAIN
与可解释语句一起使用时,MySQL显示来自优化器的关于语句执行计划的信息。That is, MySQL explains how it would process the statement, including information about how tables are joined and in which order. 也就是说,MySQL解释了它将如何处理该语句,包括有关如何联接表以及以何种顺序联接表的信息。For information about using 有关使用EXPLAIN获取执行计划信息的信息,请参阅第8.8.2节,“解释输出格式”。EXPLAIN
to obtain execution plan information, see Section 8.8.2, “EXPLAIN Output Format”.
When 当EXPLAIN
is used with FOR CONNECTION
rather than an explainable statement, it displays the execution plan for the statement executing in the named connection. connection_id
EXPLAIN
与FOR CONNECTION
而不是可解释语句一起使用时,它将显示在命名连接中执行的语句的执行计划。connection_id
See Section 8.8.4, “Obtaining Execution Plan Information for a Named Connection”.请参阅第8.8.4节,“获取命名连接的执行计划信息”。
For 对于SELECT
statements, EXPLAIN
produces additional execution plan information that can be displayed using SHOW WARNINGS
. SELECT
语句,EXPLAIN
生成可以使用SHOW WARNINGS
显示的其他执行计划信息。See Section 8.8.3, “Extended EXPLAIN Output Format”.请参阅第8.8.3节,“扩展解释输出格式”。
EXPLAIN
is useful for examining queries involving partitioned tables. EXPLAIN
用于检查涉及分区表的查询。See Section 24.3.5, “Obtaining Information About Partitions”.请参阅第24.3.5节,“获取分区信息”。
The FORMAT
option can be used to select the output format. FORMAT
选项可用于选择输出格式。TRADITIONAL
presents the output in tabular format. TRADITIONAL
以表格格式显示输出。This is the default if no 如果不存在FORMAT
option is present. FORMAT
选项,这是默认设置。JSON
format displays the information in JSON format.JSON
格式以JSON格式显示信息。
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. EXPLAIN
的帮助下,您可以看到应该在哪里向表添加索引,以便通过使用索引查找行,语句执行得更快。You can also use 还可以使用EXPLAIN
to check whether the optimizer joins the tables in an optimal order. EXPLAIN
检查优化器是否以最佳顺序连接表。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
. SELECT
语句中表的命名顺序相对应的联接顺序,请在语句开头使用SELECT STRAIGHT_JOIN
,而不仅仅是SELECT
。(See Section 13.2.10, “SELECT Statement”.) (请参阅第13.2.10节,“SELECT语句”。)However, 但是,STRAIGHT_JOIN
may prevent indexes from being used because it disables semijoin transformations. STRAIGHT_JOIN
可能会阻止使用索引,因为它禁用半连接转换。See Section 8.2.2.1, “Optimizing IN and EXISTS Subquery Predicates with Semijoin Transformations”.请参阅第8.2.2.1节,“使用半联接转换优化IN和EXISTS子查询谓词”。
The optimizer trace may sometimes provide information complementary to that of 优化器跟踪有时可以提供与EXPLAIN
. EXPLAIN
补充的信息。However, the optimizer trace format and content are subject to change between versions. 但是,优化器跟踪格式和内容在版本之间可能会发生更改。For details, see MySQL Internals: Tracing the Optimizer.有关详细信息,请参阅MySQL内部:跟踪优化器。
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. ANALYZE TABLE
来更新表统计信息,例如键的基数,这可能会影响优化器所做的选择。See Section 13.7.3.1, “ANALYZE TABLE Statement”.请参阅第13.7.3.1节,“ANALYZE TABLE语句”。
EXPLAIN
can also be used to obtain information about the columns in a table. EXPLAIN
还可用于获取有关表中列的信息。EXPLAIN
is synonymous with tbl_name
DESCRIBE
and tbl_name
SHOW COLUMNS FROM
. tbl_name
EXPLAIN
与tbl_name
DESCRIBE
以及tbl_name
SHOW COLUMNS FROM
同义。tbl_name
For more information, see Section 13.8.1, “DESCRIBE Statement”, and Section 13.7.7.5, “SHOW COLUMNS Statement”.有关更多信息,请参阅第13.8.1节,“DESCRIBE语句”和第13.7.7.5节,“SHOW COLUMNS语句”。