描述

该语句用于解释 SQL 语句的执行计划,可以是 SELECTDELETEINSERTREPLACEUPDATE 语句。

格式

  1. 获取表或列的信息:
  2. {EXPLAIN | DESCRIBE | DESC} tbl_name [col_name | wild]
  3. 获取SQL计划信息:
  4. {EXPLAIN}
  5. [BASIC | OUTLINE | EXTENDED | EXTENDED_NOADDR | PARTITIONS | FORMAT = {TRADITIONAL| JSON}]
  6. {SELECT statement | DELETE statement | INSERT statement | UPDATE statement | MERGE statement}

参数解释

参数

描述

tbl_name

指定表名。

col_name

指定表的列名。

BASIC

指定输出计划的基础信息,如算子 ID、算子名称、所引用的表名。

OUTLINE

指定输出的计划信息包含 Outline 信息。

EXTENDED

EXPLAIN产生附加信息,包括:每个算子的输入列和输出列,访问表的分区信息,当前使用的filter信息,如果当前算子使用了索引,显示所使用的索引列及抽取的 Query Range。

EXTENDED_NOADDR

以简约的方式展示附加信息。

PARTITIONS

显示分区相关信息。

FORMAT = {TRADITIONAL| JSON}

指定EXPALIN的输出格式:

  • TRADITIONAL:表格输出格式.

  • JSONKEY:VALUE输出格式,JSON显示为JSON字符串,包括EXTENDEDPARTITIONS信息。

示例

  • 省略 explain_type
  1. obclient>CREATE TABLE t1(c1 INT,c2 INT);
  2. Query OK, 0 rows affected (0.04 sec)
  3. obclient>CREATE TABLE t2(c1 INT,c2 INT);
  4. Query OK, 0 rows affected (0.07 sec)
  5. obclient>EXPLAIN SELECT * FROM t1,t2 WHERE t1.c2=t2.c2 AND t2.c1 > 4\G
  6. *************************** 1. row ***************************
  7. Query Plan: =======================================
  8. |ID|OPERATOR |NAME|EST. ROWS|COST |
  9. ---------------------------------------
  10. |0 |HASH JOIN | |9801000 |5933109|
  11. |1 | TABLE SCAN|t2 |10000 |6219 |
  12. |2 | TABLE SCAN|t1 |100000 |68478 |
  13. =======================================
  14. Outputs & filters:
  15. -------------------------------------
  16. 0 - output([t1.c1], [t1.c2], [t2.c1], [t2.c2]), filter(nil),
  17. equal_conds([t1.c2 = t2.c2]), other_conds(nil)
  18. 1 - output([t2.c2], [t2.c1]), filter(nil),
  19. access([t2.c2], [t2.c1]), partitions(p0)
  20. 2 - output([t1.c2], [t1.c1]), filter(nil),
  21. access([t1.c2], [t1.c1]), partitions(p0)
  • EXTENDED
  1. obclient>EXPLAIN EXTENDED_NOADDR SELECT * FROM t1,t2
  2. WHERE t1.c2=t2.c2 AND t2.c1 > 4\G
  3. *************************** 1. row ***************************
  4. Query Plan: =======================================
  5. |ID|OPERATOR |NAME|EST. ROWS|COST |
  6. ---------------------------------------
  7. |0 |HASH JOIN | |9801000 |5933109|
  8. |1 | TABLE SCAN|t2 |10000 |6219 |
  9. |2 | TABLE SCAN|t1 |100000 |68478 |
  10. =======================================
  11. Outputs & filters:
  12. -------------------------------------
  13. 0 - output([t1.c1], [t1.c2], [t2.c1], [t2.c2]), filter(nil),
  14. equal_conds([t1.c2 = t2.c2]), other_conds(nil)
  15. 1 - output([t2.c2], [t2.c1]), filter(nil),
  16. access([t2.c2], [t2.c1]), partitions(p0),
  17. is_index_back=false,
  18. range_key([t2.c1]), range(4 ; MAX),
  19. range_cond([t2.c1 > 4])
  20. 2 - output([t1.c2], [t1.c1]), filter(nil),
  21. access([t1.c2], [t1.c1]), partitions(p0),
  22. is_index_back=false,
  23. range_key([t1.__pk_increment], [t1.__pk_cluster_id], [t1.__pk_partition_id]), range(MIN,MIN,MIN ; MAX,MAX,MAX)always true
  • TRADITIONAL格式
  1. obclient>EXPLAIN FORMAT=TRADITIONAL SELECT * FROM t1,t2
  2. WHERE t1.c2=t2.c2 AND t2.c1 > 4\G
  3. *************************** 1. row ***************************
  4. Query Plan: =======================================
  5. |ID|OPERATOR |NAME|EST. ROWS|COST |
  6. ---------------------------------------
  7. |0 |HASH JOIN | |9801000 |5933109|
  8. |1 | TABLE SCAN|t2 |10000 |6219 |
  9. |2 | TABLE SCAN|t1 |100000 |68478 |
  10. =======================================
  11. Outputs & filters:
  12. -------------------------------------
  13. 0 - output([t1.c1], [t1.c2], [t2.c1], [t2.c2]), filter(nil),
  14. equal_conds([t1.c2 = t2.c2]), other_conds(nil)
  15. 1 - output([t2.c2], [t2.c1]), filter(nil),
  16. access([t2.c2], [t2.c1]), partitions(p0)
  17. 2 - output([t1.c2], [t1.c1]), filter(nil),
  18. access([t1.c2], [t1.c1]), partitions(p0)
  • JSON 格式
  1. obclient>EXPLAIN FORMAT=JSON SELECT * FROM t1,t2
  2. WHERE t1.c2=t2.c2 AND t2.c1 > 4\G
  3. *************************** 1. row ***************************
  4. Query Plan: {
  5. "ID":2,
  6. "OPERATOR":"JOIN",
  7. "NAME":"JOIN",
  8. "EST.ROWS":9800999,
  9. "COST":6840524,
  10. "output": [
  11. "T1.C1",
  12. "T1.C2",
  13. "T2.C1",
  14. "T2.C2"
  15. ],
  16. "CHILD_1": {
  17. "ID":0,
  18. "OPERATOR":"TABLE SCAN",
  19. "NAME":"TABLE SCAN",
  20. "EST.ROWS":10000,
  21. "COST":80810,
  22. "output": [
  23. "T2.C2",
  24. "T2.C1"
  25. ]
  26. },
  27. "CHILD_2": {
  28. "ID":1,
  29. "OPERATOR":"TABLE SCAN",
  30. "NAME":"TABLE SCAN",
  31. "EST.ROWS":100000,
  32. "COST":64065,
  33. "output": [
  34. "T1.C2",
  35. "T1.C1"
  36. ]
  37. }
  38. }
  39. 1 row in set (0.01 sec)

EXPLAIN 的每个输出行提供一个表的相关信息,并且每个行包括下面的列:

列名

说明

ID

计划执行序列号。

OPERATOR

执行算子。

NAME

算子所引用的表。

EST.ROWS

估计执行到当前算子输出的行数。

COST

执行到当前算子的 CPU 时间。