EXPLAIN

EXPLAIN 语句仅用于显示查询的执行计划,而不执行查询。EXPLAIN ANALYZE 可执行查询,补充 EXPLAIN 语句。如果 EXPLAIN 的输出与预期结果不匹配,可考虑在查询的每个表上执行 ANALYZE TABLE

语句 DESCDESCRIBEEXPLAIN 的别名。EXPLAIN <tableName> 的替代用法记录在 SHOW [FULL] COLUMNS FROM 下。

语法图

ExplainSym:

ExplainSym

ExplainStmt:

ExplainStmt

ExplainableStmt:

ExplainableStmt

示例

  1. EXPLAIN SELECT 1;
  1. +-------------------+-------+------+---------------+
  2. | id | count | task | operator info |
  3. +-------------------+-------+------+---------------+
  4. | Projection_3 | 1.00 | root | 1 |
  5. | └─TableDual_4 | 1.00 | root | rows:1 |
  6. +-------------------+-------+------+---------------+
  7. 2 rows in set (0.00 sec)
  1. CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, c1 INT NOT NULL);
  1. Query OK, 0 rows affected (0.10 sec)
  1. INSERT INTO t1 (c1) VALUES (1), (2), (3);
  1. Query OK, 3 rows affected (0.02 sec)
  2. Records: 3 Duplicates: 0 Warnings: 0
  1. EXPLAIN SELECT * FROM t1 WHERE id = 1;
  1. +-------------+-------+------+--------------------+
  2. | id | count | task | operator info |
  3. +-------------+-------+------+--------------------+
  4. | Point_Get_1 | 1.00 | root | table:t1, handle:1 |
  5. +-------------+-------+------+--------------------+
  6. 1 row in set (0.00 sec)
  1. DESC SELECT * FROM t1 WHERE id = 1;
  1. +-------------+-------+------+--------------------+
  2. | id | count | task | operator info |
  3. +-------------+-------+------+--------------------+
  4. | Point_Get_1 | 1.00 | root | table:t1, handle:1 |
  5. +-------------+-------+------+--------------------+
  6. 1 row in set (0.00 sec)
  1. DESCRIBE SELECT * FROM t1 WHERE id = 1;
  1. +-------------+-------+------+--------------------+
  2. | id | count | task | operator info |
  3. +-------------+-------+------+--------------------+
  4. | Point_Get_1 | 1.00 | root | table:t1, handle:1 |
  5. +-------------+-------+------+--------------------+
  6. 1 row in set (0.00 sec)
  1. EXPLAIN INSERT INTO t1 (c1) VALUES (4);
  1. ERROR 1105 (HY000): Unsupported type *core.Insert
  1. EXPLAIN UPDATE t1 SET c1=5 WHERE c1=3;
  1. +---------------------+----------+------+-------------------------------------------------------------+
  2. | id | count | task | operator info |
  3. +---------------------+----------+------+-------------------------------------------------------------+
  4. | TableReader_6 | 10.00 | root | data:Selection_5 |
  5. | └─Selection_5 | 10.00 | cop | eq(test.t1.c1, 3) |
  6. | └─TableScan_4 | 10000.00 | cop | table:t1, range:[-inf,+inf], keep order:false, stats:pseudo |
  7. +---------------------+----------+------+-------------------------------------------------------------+
  8. 3 rows in set (0.00 sec)
  1. EXPLAIN DELETE FROM t1 WHERE c1=3;
  1. +---------------------+----------+------+-------------------------------------------------------------+
  2. | id | count | task | operator info |
  3. +---------------------+----------+------+-------------------------------------------------------------+
  4. | TableReader_6 | 10.00 | root | data:Selection_5 |
  5. | └─Selection_5 | 10.00 | cop | eq(test.t1.c1, 3) |
  6. | └─TableScan_4 | 10000.00 | cop | table:t1, range:[-inf,+inf], keep order:false, stats:pseudo |
  7. +---------------------+----------+------+-------------------------------------------------------------+
  8. 3 rows in set (0.00 sec)

如果未指定 FORMAT,或未指定 FORMAT ="row",那么 EXPLAIN 语句将以表格格式输出结果。更多信息,可参阅 Understand the Query Execution Plan

除 MySQL 标准结果格式外,TiDB 还支持 DotGraph。需按照下列所示指定 FORMAT ="dot"

  1. create table t(a bigint, b bigint);
  2. desc format = "dot" select A.a, B.b from t A join t B on A.a > B.b where A.a < 10;
  1. | dot contents |
  2. +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. |
  4. digraph HashRightJoin_7 {
  5. subgraph cluster7{
  6. node [style=filled, color=lightgrey]
  7. color=black
  8. label = "root"
  9. "HashRightJoin_7" -> "TableReader_10"
  10. "HashRightJoin_7" -> "TableReader_12"
  11. }
  12. subgraph cluster9{
  13. node [style=filled, color=lightgrey]
  14. color=black
  15. label = "cop"
  16. "Selection_9" -> "TableScan_8"
  17. }
  18. subgraph cluster11{
  19. node [style=filled, color=lightgrey]
  20. color=black
  21. label = "cop"
  22. "TableScan_11"
  23. }
  24. "TableReader_10" -> "Selection_9"
  25. "TableReader_12" -> "TableScan_11"
  26. }
  27. |
  28. +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. 1 row in set (0.00 sec)

如果你的计算机上安装了 dot 程序(在 graphviz 包中),可使用以下方法生成 PNG 文件:

  1. dot xx.dot -T png -O

The xx.dot is the result returned by the above statement.

如果你的计算机上未安装 dot 程序,可将结果复制到 本网站 以获取树形图:

Explain Dot

MySQL 兼容性

  • EXPLAIN 的格式和 TiDB 中潜在的执行计划都与 MySQL 有很大不同。
  • TiDB 不像 MySQL 那样支持 EXPLAIN FORMAT = JSON
  • TiDB 目前不支持插入语句的 EXPLAIN

另请参阅