EXPLAIN

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

    TiDB 支持 EXPLAIN [options] FOR CONNECTION connection_id,但与 MySQL 的 EXPLAIN FOR 有一些区别,请参见 。

    EXPLAIN 输出格式

    目前 TiDB 的 EXPLAIN 会输出 5 列,分别是:idestRowstaskaccess objectoperator info。执行计划中每个算子都由这 5 列属性来描述,EXPLAIN 结果中每一行描述一个算子。每个属性的具体含义如下:

    1. EXPLAIN SELECT 1;
    1. +-------------------+---------+------+---------------+---------------+
    2. | id | estRows | task | access object | operator info |
    3. +-------------------+---------+------+---------------+---------------+
    4. | Projection_3 | 1.00 | root | | 1->Column#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. +-------------+---------+------+---------------+---------------+
    2. | id | estRows | task | access object | operator info |
    3. +-------------+---------+------+---------------+---------------+
    4. | Point_Get_1 | 1.00 | root | table:t1 | handle:1 |
    5. 1 row in set (0.00 sec)
    1. DESC SELECT * FROM t1 WHERE id = 1;
    1. +-------------+---------+------+---------------+---------------+
    2. | id | estRows | task | access object | 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. | id | estRows | task | access object | operator info |
    2. +-------------+---------+------+---------------+---------------+
    3. | Point_Get_1 | 1.00 | root | table:t1 | handle:1 |
    4. +-------------+---------+------+---------------+---------------+
    5. 1 row in set (0.00 sec)
    1. EXPLAIN INSERT INTO t1 (c1) VALUES (4);
    1. EXPLAIN UPDATE t1 SET c1=5 WHERE c1=3;
    1. +---------------------------+---------+-----------+---------------+--------------------------------+
    2. | id | estRows | task | access object | operator info |
    3. +---------------------------+---------+-----------+---------------+--------------------------------+
    4. | Update_4 | N/A | root | | N/A |
    5. | └─TableReader_8 | 0.00 | root | | data:Selection_7 |
    6. | └─Selection_7 | 0.00 | cop[tikv] | | eq(test.t1.c1, 3) |
    7. | └─TableFullScan_6 | 3.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
    8. +---------------------------+---------+-----------+---------------+--------------------------------+
    9. 4 rows in set (0.00 sec)
    1. EXPLAIN DELETE FROM t1 WHERE c1=3;
    1. +---------------------------+---------+-----------+---------------+--------------------------------+
    2. | id | estRows | task | access object | operator info |
    3. +---------------------------+---------+-----------+---------------+--------------------------------+
    4. | Delete_4 | N/A | root | | N/A |
    5. | └─TableReader_8 | 0.00 | root | | data:Selection_7 |
    6. | └─Selection_7 | 0.00 | cop[tikv] | | eq(test.t1.c1, 3) |
    7. | └─TableFullScan_6 | 3.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
    8. +---------------------------+---------+-----------+---------------+--------------------------------+
    9. 4 rows in set (0.01 sec)

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

    除 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 Projection_8 {
    5. node [style=filled, color=lightgrey]
    6. color=black
    7. label = "root"
    8. "Projection_8" -> "HashJoin_9"
    9. "HashJoin_9" -> "TableReader_13"
    10. "HashJoin_9" -> "Selection_14"
    11. "Selection_14" -> "TableReader_17"
    12. }
    13. subgraph cluster12{
    14. node [style=filled, color=lightgrey]
    15. color=black
    16. label = "cop"
    17. "Selection_12" -> "TableFullScan_11"
    18. }
    19. subgraph cluster16{
    20. node [style=filled, color=lightgrey]
    21. color=black
    22. label = "cop"
    23. "Selection_16" -> "TableFullScan_15"
    24. }
    25. "TableReader_13" -> "Selection_12"
    26. "TableReader_17" -> "Selection_16"
    27. }
    28. |
    29. +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    30. 1 row in set (0.00 sec)

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

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

    MySQL 兼容性

    • EXPLAIN 的格式和 TiDB 中潜在的执行计划都与 MySQL 有很大不同。
    • TiDB 不支持 FORMAT=JSONFORMAT=TREE 选项。
    • MySQL 返回的是正在执行的查询计划,而 TiDB 返回的是最后执行的查询计划。
    • MySQL 的文档中指出,MySQL 要求登录用户与被查询的连接相同,或者拥有 PROCESS 权限,而 TiDB 则要求登录用户与被查询的连接相同,或者拥有 权限。

    另请参阅