EXPLAIN Statement

Show the execution plan of a statement.

Syntax:

  1. EXPLAIN [AST | SYNTAX | PLAN | PIPELINE] [setting = value, ...] SELECT ... [FORMAT ...]

Example:

  1. EXPLAIN SELECT sum(number) FROM numbers(10) UNION ALL SELECT sum(number) FROM numbers(10) ORDER BY sum(number) ASC FORMAT TSV;
  1. Union
  2. Expression (Projection)
  3. Expression (Before ORDER BY and SELECT)
  4. Aggregating
  5. Expression (Before GROUP BY)
  6. SettingQuotaAndLimits (Set limits and quota after reading from storage)
  7. ReadFromStorage (SystemNumbers)
  8. Expression (Projection)
  9. MergingSorted (Merge sorted streams for ORDER BY)
  10. MergeSorting (Merge sorted blocks for ORDER BY)
  11. PartialSorting (Sort each block for ORDER BY)
  12. Expression (Before ORDER BY and SELECT)
  13. Aggregating
  14. Expression (Before GROUP BY)
  15. SettingQuotaAndLimits (Set limits and quota after reading from storage)
  16. ReadFromStorage (SystemNumbers)

EXPLAIN Types

  • AST — Abstract syntax tree.
  • SYNTAX — Query text after AST-level optimizations.
  • PLAN — Query execution plan.
  • PIPELINE — Query execution pipeline.

EXPLAIN AST

Dump query AST.

Example:

  1. EXPLAIN AST SELECT 1;
  1. SelectWithUnionQuery (children 1)
  2. ExpressionList (children 1)
  3. SelectQuery (children 1)
  4. ExpressionList (children 1)
  5. Literal UInt64_1

EXPLAIN SYNTAX

Return query after syntax optimizations.

Example:

  1. EXPLAIN SYNTAX SELECT * FROM system.numbers AS a, system.numbers AS b, system.numbers AS c;
  1. SELECT
  2. `--a.number` AS `a.number`,
  3. `--b.number` AS `b.number`,
  4. number AS `c.number`
  5. FROM
  6. (
  7. SELECT
  8. number AS `--a.number`,
  9. b.number AS `--b.number`
  10. FROM system.numbers AS a
  11. CROSS JOIN system.numbers AS b
  12. ) AS `--.s`
  13. CROSS JOIN system.numbers AS c

EXPLAIN PLAN

Dump query plan steps.

Settings:

  • header — Print output header for step. Default: 0.
  • description — Print step description. Default: 1.
  • actions — Print detailed information about step actions. Default: 0.

Example:

  1. EXPLAIN SELECT sum(number) FROM numbers(10) GROUP BY number % 4;
  1. Union
  2. Expression (Projection)
  3. Expression (Before ORDER BY and SELECT)
  4. Aggregating
  5. Expression (Before GROUP BY)
  6. SettingQuotaAndLimits (Set limits and quota after reading from storage)
  7. ReadFromStorage (SystemNumbers)

Note

Step and query cost estimation is not supported.

EXPLAIN PIPELINE

Settings:

  • header — Print header for each output port. Default: 0.
  • graph — Use DOT graph description language. Default: 0.
  • compact — Print graph in compact mode if graph is enabled. Default: 1.

Example:

  1. EXPLAIN PIPELINE SELECT sum(number) FROM numbers_mt(100000) GROUP BY number % 4;
  1. (Union)
  2. (Expression)
  3. ExpressionTransform
  4. (Expression)
  5. ExpressionTransform
  6. (Aggregating)
  7. Resize 2 1
  8. AggregatingTransform × 2
  9. (Expression)
  10. ExpressionTransform × 2
  11. (SettingQuotaAndLimits)
  12. (ReadFromStorage)
  13. NumbersMt × 2 0 1

Оriginal article