1. 执行计划简介

  执行计划是进行查询调优的重要参考。ClickHouse 中,可以使用explain语句很方便的查看 SQL 语句的执行计划。
  在 ClickHouse 20.6 版本之前要查看 SQL 语句的执行计划需要设置日志级别为 trace 才能可以看到,并且只能真正执行 SQL,在执行日志里面查看。在 20.6 版本引入了原生的执行计划的语法,在 20.6.3 版本成为正式版本的功能。

2. 基本语法

1
2
3
4
5
6
EXPLAIN [AST | SYNTAX | PLAN | PIPELINE | TABLE OVERRIDE] [setting = value, ...]
[
SELECT ... |
tableFunction(...) [COLUMNS (...)] [ORDER BY ...] [PARTITION BY ...] [PRIMARY KEY] [SAMPLE BY ...] [TTL ...]
]
[FORMAT ...]
  • AST: 用于查看语法树;
    • 支持查看所有类型的语句,不光是 select 语句:
    1
    explain ast select number from system.numbers limit 10;
  • SYNTAX: 查询优化后的 SQL 语句;
    1
    EXPLAIN SYNTAX SELECT * FROM system.numbers AS a, system.numbers AS b, system.numbers AS c;
  • PLAN: 用于查看执行计划,可以指定五个参数;
    • header:打印计划中各个步骤的 header 说明,默认 0(关闭);
    • description:打印计划中各个步骤的描述,默认 1(开启);
    • indexes:显示索引使用情况,默认 0(关闭);
    • actions:打印计划中各个步骤的详细信息,默认 0(关闭);
    • json:以 JSON 格式打印执行计划的详细信息,默认 0(关闭);
    1
    2
    EXPLAIN SELECT sum(number) FROM numbers(10) GROUP BY number % 4;
    EXPLAIN json = 1, description = 0 SELECT 1 UNION ALL SELECT 2 FORMAT TSVRaw;
  • PIPELINE: 用于查看 pipeline 计划,可以指定三个参数;
    • header:打印计划中各个步骤的 header 说明,默认 0(关闭);
    • graph:用 DOT 图形语言描述管道图,默认 0(关闭);
    • compact:如果开启了 graph,紧凑打印行,默认 1(开启);
    1
    EXPLAIN PIPELINE SELECT sum(number) FROM numbers_mt(100000) GROUP BY number % 4;

3. 案例实操

3.1 查看PLAN

  简单查询:

1
explain plan select arrayJoin([1,2,3,null,null]);


  复杂 SQL 的执行计划:

1
2
3
4
5
6
7
8
9
10
11
12
13
explain select
database,
table,
count(1) cnt
from system.parts
where database in ('datasets','system')
group by
database,
table
order by
database,
cnt desc
limit 2 by database;

3.2 AST语法树

1
EXPLAIN AST SELECT number from system.numbers limit 10;

3.3 SYNTAX语法优化

  先做一次查询:

1
SELECT number = 1 ? 'hello' : (number = 2 ? 'world' : 'atguigu') FROM numbers(10);


  查看语法优化:

1
EXPLAIN SYNTAX SELECT number = 1 ? 'hello' : (number = 2 ? 'world' : 'atguigu') FROM numbers(10);


  开启三元运算符优化:

1
SET optimize_if_chain_to_multiif = 1;


  再次查看语法优化:

1
EXPLAIN SYNTAX SELECT number = 1 ? 'hello' : (number = 2 ? 'world' : 'atguigu') FROM numbers(10);


  返回优化后的语句

1
SELECT multiIf(number = 1, 'hello', number = 2, 'world', 'xyz') FROM numbers(10)

3.4 查看PIPELINE

1
EXPLAIN PIPELINE SELECT sum(number) FROM numbers_mt(100000) GROUP BY number % 20;


  打开其他参数:

1
EXPLAIN PIPELINE header=1,graph=1 SELECT sum(number) FROM numbers_mt(10000) GROUP BY number%20;

参考文献

  【1】https://clickhouse.com/docs/zh/
  【2】https://www.bilibili.com/video/BV1Yh411z7os?from=search&seid=4579023877699743987&spm_id_from=333.337.0.0
  【3】https://clickhouse.com/docs/zh/