{EXPLAIN | DESCRIBE | DESC}tbl_name
[col_name
|wild
] {EXPLAIN | DESCRIBE | DESC} [explain_type
] {explainable_stmt
| FOR CONNECTIONconnection_id
} {EXPLAIN | DESCRIBE | DESC} ANALYZE [FORMAT = TREE]select_statement
explain_type
: { FORMAT =format_name
}format_name
: { TRADITIONAL | JSON | TREE }explainable_stmt
: { SELECT statement | TABLE statement | DELETE statement | INSERT statement | REPLACE statement | UPDATE statement }
The DESCRIBE
and EXPLAIN
statements are synonyms. DESCRIBE
和EXPLAIN
语句是同义词。In practice, the 在实践中,DESCRIBE
keyword is more often used to obtain information about table structure, whereas EXPLAIN
is used to obtain a query execution plan (that is, an explanation of how MySQL would execute a query).DESCRIBE
关键字更常用于获取有关表结构的信息,而EXPLAIN
用于获取查询执行计划(即解释MySQL将如何执行查询)。
The following discussion uses the 下面的讨论根据这些用法使用了DESCRIBE
and EXPLAIN
keywords in accordance with those uses, but the MySQL parser treats them as completely synonymous.DESCRIBE
和EXPLAIN
关键字,但MySQL解析器将它们视为完全同义词。
DESCRIBE
provides information about the columns in a table:DESCRIBE
提供有关表中列的信息:
mysql> DESCRIBE City;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| Id | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| Country | char(3) | NO | UNI | | |
| District | char(20) | YES | MUL | | |
| Population | int(11) | NO | | 0 | |
+------------+----------+------+-----+---------+----------------+
DESCRIBE
is a shortcut for SHOW COLUMNS
. DESCRIBE
是SHOW COLUMNS
的快捷方式。These statements also display information for views. 这些语句还显示视图的信息。The description for SHOW COLUMNS
provides more information about the output columns. SHOW COLUMNS
的说明提供了有关输出列的更多信息。See Section 13.7.7.5, “SHOW COLUMNS Statement”.请参阅第13.7.7.5节,“SHOW COLUMNS语句”。
By default, 默认情况下,DESCRIBE
displays information about all columns in the table. DESCRIBE
显示有关表中所有列的信息。col_name
, if given, is the name of a column in the table. col_name
(如果给定)是表中一列的名称。In this case, the statement displays information only for the named column. 在本例中,该语句仅显示命名列的信息。如果给定wild
, if given, is a pattern string. wild
,则为模式字符串。It can contain the SQL 它可以包含SQL%
and _
wildcard characters. %
和_
通配符。In this case, the statement displays output only for the columns with names matching the string. 在本例中,语句仅显示名称与字符串匹配的列的输出。There is no need to enclose the string within quotation marks unless it contains spaces or other special characters.除非字符串包含空格或其他特殊字符,否则不需要将字符串括在引号内。
The 提供DESCRIBE
statement is provided for compatibility with Oracle.DESCRIBE
语句是为了与Oracle兼容。
The SHOW CREATE TABLE
, SHOW TABLE STATUS
, and SHOW INDEX
statements also provide information about tables. SHOW CREATE TABLE
、SHOW TABLE STATUS
和SHOW INDEX
语句还提供有关表的信息。See Section 13.7.7, “SHOW Statements”.请参阅第13.7.7节,“SHOW语句”。
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
语句。In MySQL 8.0.19 and later, it also works with 在MySQL 8.0.19及更高版本中,它还可以使用TABLE
statements.TABLE
语句。
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
to obtain execution plan information, see Section 8.8.2, “EXPLAIN Output Format”.EXPLAIN
获取执行计划信息的信息,请参阅第8.8.2节,“解释输出格式”。
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
See Section 8.8.4, “Obtaining Execution Plan Information for a Named Connection”.请参阅第8.8.4节,“获取命名连接的执行计划信息”。
For explainable statements, 对于可解释语句,EXPLAIN
produces additional execution plan information that can be displayed using SHOW WARNINGS
. 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. This is the default if no FORMAT
option is present. JSON
format displays the information in JSON format. In MySQL 8.0.16 and later, TREE
provides tree-like output with more precise descriptions of query handling than the TRADITIONAL
format; it is the only format which shows hash join usage (see Section 8.2.1.4, “Hash Join Optimization”) and is always used for EXPLAIN ANALYZE
.
EXPLAIN
requires the same privileges required to execute the explained statement. Additionally, EXPLAIN
also requires the SHOW VIEW
privilege for any explained view. EXPLAIN ... FOR CONNECTION
also requires the PROCESS
privilege if the specified connection belongs to a different user.
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. 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 13.2.10, “SELECT Statement”.)(请参阅第13.2.10节,“SELECT语句”。)
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节,“分析表报表”。
MySQL Workbench has a Visual Explain capability that provides a visual representation of MySQL工作台具有可视化解释功能,提供EXPLAIN
output. EXPLAIN
输出的可视化表示。See Tutorial: Using Explain to Improve Query Performance.请参见教程:使用解释提高查询性能。
MySQL 8.0.18 introduces MySQL 8.0.18引入了EXPLAIN ANALYZE
, which runs a statement and produces EXPLAIN
output along with timing and additional, iterator-based, information about how the optimizer's expectations matched the actual execution. EXPLAIN ANALYSE
,它运行一条语句并生成EXPLAIN
输出,以及关于优化器的期望如何与实际执行相匹配的计时和其他基于迭代器的信息。For each iterator, the following information is provided:对于每个迭代器,提供以下信息:
Estimated execution cost估计执行成本
(Some iterators are not accounted for by the cost model, and so are not included in the estimate.)(有些迭代器未被成本模型考虑在内,因此未包含在估算中。)
Estimated number of returned rows估计返回的行数
Time to return first row返回第一行的时间到了
Time to return all rows (actual cost), in milliseconds返回所有行的时间(实际成本),以毫秒为单位
(When there are multiple loops, this figure shows the average time per loop.)(当存在多个循环时,此图显示每个循环的平均时间。)
Number of rows returned by the iterator迭代器返回的行数
Number of loops循环数
The query execution information is displayed using the 查询执行信息使用TREE
output format, in which nodes represent iterators. TREE
输出格式显示,其中节点表示迭代器。EXPLAIN ANALYZE
always uses the TREE
output format. In MySQL 8.0.21 and later, this can optionally be specified explicitly using FORMAT=TREE
; formats other than TREE
remain unsupported.
EXPLAIN ANALYZE
can be used with SELECT
statements, as well as with multi-table UPDATE
and DELETE
statements. EXPLAIN ANALYZE
可用于SELECT
语句,以及多表UPDATE
和DELETE
语句。Beginning with MySQL 8.0.19, it can also be used with 从MySQL 8.0.19开始,它还可以用于TABLE
statements.TABLE
语句。
Beginning with MySQL 8.0.20, you can terminate this statement using 从MySQL 8.0.20开始,可以使用KILL QUERY
or CTRL-C.KILL QUERY
或CTRL-C终止此语句。
EXPLAIN ANALYZE
cannot be used with FOR CONNECTION
.EXPLAIN ANALYZE
不能与FOR CONNECTION
一起使用。
Example output:示例输出:
mysql> EXPLAIN ANALYZE SELECT * FROM t1 JOIN t2 ON (t1.c1 = t2.c2)\G
*************************** 1. row ***************************
EXPLAIN: -> Inner hash join (t2.c2 = t1.c1) (cost=4.70 rows=6)
(actual time=0.032..0.035 rows=6 loops=1)
-> Table scan on t2 (cost=0.06 rows=6)
(actual time=0.003..0.005 rows=6 loops=1)
-> Hash
-> Table scan on t1 (cost=0.85 rows=6)
(actual time=0.018..0.022 rows=6 loops=1)
mysql> EXPLAIN ANALYZE SELECT * FROM t3 WHERE i > 8\G
*************************** 1. row ***************************
EXPLAIN: -> Filter: (t3.i > 8) (cost=1.75 rows=5)
(actual time=0.019..0.021 rows=6 loops=1)
-> Table scan on t3 (cost=1.75 rows=15)
(actual time=0.017..0.019 rows=15 loops=1)
mysql> EXPLAIN ANALYZE SELECT * FROM t3 WHERE pk > 17\G
*************************** 1. row ***************************
EXPLAIN: -> Filter: (t3.pk > 17) (cost=1.26 rows=5)
(actual time=0.013..0.016 rows=5 loops=1)
-> Index range scan on t3 using PRIMARY (cost=1.26 rows=5)
(actual time=0.012..0.014 rows=5 loops=1)
The tables used in the example output were created by the statements shown here:示例输出中使用的表由以下语句创建:
CREATE TABLE t1 ( c1 INTEGER DEFAULT NULL, c2 INTEGER DEFAULT NULL ); CREATE TABLE t2 ( c1 INTEGER DEFAULT NULL, c2 INTEGER DEFAULT NULL ); CREATE TABLE t3 ( pk INTEGER NOT NULL PRIMARY KEY, i INTEGER DEFAULT NULL );
Values shown for 此语句输出中显示的actual time
in the output of this statement are expressed in milliseconds.actual time
值以毫秒为单位。