当前位置: 首页 > news >正文

ClickHouse查看执行计划(EXPLAIN语法)

1.EXPLAIN 语法示例

EXPLAIN [AST | SYNTAX | QUERY TREE | PLAN | PIPELINE | ESTIMATE | TABLE OVERRIDE] [setting = value, ...]
    [
      SELECT ... |
      tableFunction(...) [COLUMNS (...)] [ORDER BY ...] [PARTITION BY ...] [PRIMARY KEY] [SAMPLE BY ...] [TTL ...]
    ]
    [FORMAT ...]

查询计划的类型说明:
 AST(抽象语法树):在AST级别优化之后的查询文本 
 SYNTAX(语法优化):在AST级别优化之后的查询文本,返回优化后的sql 
 QUERY TREE(查询树):在查询树级别优化之后的查询树
 PLAN(执行计划):查询执行计划,默认是此值
 PIPELINE(管道):查询执行管道

1.1AST(抽象语法树)

查询语法树执行计划

explain AST SELECT
            path,code,v1
        FROM
            autorun_t_index
        WHERE
            index_id = 1227
          AND cluster_id =27
          AND code IN('queue_name', 'queue_allocatedmb', 'queue_availablemb','queue_allocatedvcores','queue_availablevcores')
          AND update_date IN(SELECT MAX(update_date) FROM autorun_t_index WHERE index_id = 1227 AND cluster_id = 27 and update_date <='2023-08-04 01:38:48')

展示结果:

  SelectQuery (children 3)
   ExpressionList (children 3)
    Identifier path
    Identifier code
    Identifier v1
   TablesInSelectQuery (children 1)
    TablesInSelectQueryElement (children 1)
     TableExpression (children 1)
      TableIdentifier autorun_t_index
   Function and (children 1)
    ExpressionList (children 4)
     Function equals (children 1)
      ExpressionList (children 2)
       Identifier index_id
       Literal UInt64_1227
     Function equals (children 1)
      ExpressionList (children 2)
       Identifier cluster_id

       Literal UInt64_27
     Function in (children 1)
      ExpressionList (children 2)
       Identifier code
       Literal Tuple_('queue_name', 'queue_allocatedmb', 'queue_availablemb', 'queue_allocatedvcores', 'queue_availablevcores')
     Function in (children 1)
      ExpressionList (children 2)
       Identifier update_date
       Subquery (children 1)
        SelectWithUnionQuery (children 1)
         ExpressionList (children 1)
          SelectQuery (children 3)
           ExpressionList (children 1)
            Function MAX (children 1)
             ExpressionList (children 1)

              Identifier update_date
           TablesInSelectQuery (children 1)
            TablesInSelectQueryElement (children 1)
             TableExpression (children 1)
              TableIdentifier autorun_t_index
           Function and (children 1)
            ExpressionList (children 3)
             Function equals (children 1)
              ExpressionList (children 2)
               Identifier index_id
               Literal UInt64_1227
             Function equals (children 1)
              ExpressionList (children 2)
               Identifier cluster_id
               Literal UInt64_27
             Function lessOrEquals (children 1)
              ExpressionList (children 2)
               Identifier update_date
               Literal '2023-08-04 01:38:48'
 Identifier TabSeparatedWithNamesAndTypes

1.2SYNTAX(语法优化)

语法优化前执行sql

explain SYNTAX  SELECT
            path,code,v1
        FROM
            autorun_t_index
        WHERE
            index_id = 1227
          AND cluster_id =27
          AND code IN('queue_name', 'queue_allocatedmb', 'queue_availablemb','queue_allocatedvcores','queue_availablevcores')
          AND update_date IN(SELECT MAX(update_date) FROM autorun_t_index WHERE index_id = 1227 AND cluster_id = 27 and update_date <='2023-08-04 01:38:48')

语法优化后推荐的sql

SELECT
    path,
    code,
    v1
FROM autorun_t_index
WHERE (index_id = 1227) AND (cluster_id = 27) AND (code IN ('queue_name', 'queue_allocatedmb', 'queue_availablemb', 'queue_allocatedvcores', 'queue_availablevcores')) AND (update_date IN ((
    SELECT max(update_date)
    FROM autorun_t_index
    WHERE (index_id = 1227) AND (cluster_id = 27) AND (update_date <= '2023-08-04 01:38:48')
) AS _subquery132))

1.3QUERY TREE(查询树)

1.4PLAN(执行计划)

默认值就是PLAN,一般用来查看sql的执行计划,是否使用引擎,分区,索引等信息

未添加索引前

explain  PLAN  SELECT
            path,code,v1
        FROM
            autorun_t_index
        WHERE
            index_id = 1227
          AND cluster_id =27
          AND code IN('queue_name', 'queue_allocatedmb', 'queue_availablemb','queue_allocatedvcores','queue_availablevcores')
          AND update_date IN(SELECT MAX(update_date) FROM autorun_t_index WHERE index_id = 1227 AND cluster_id = 27 and update_date <='2023-08-04 01:38:48')

未添加索引查询计划

Expression (Projection)
  CreatingSets (Create sets before main query execution)
    Expression (Before ORDER BY)
      Filter (WHERE)
        SettingQuotaAndLimits (Set limits and quota after reading from storage)
          ReadFromStorage (Log)
    CreatingSet (Create set for subquery)
      Expression ((Projection + Before ORDER BY))
        Aggregating
          Expression (Before GROUP BY)
            Filter (WHERE)
              SettingQuotaAndLimits (Set limits and quota after reading from storage)
                ReadFromStorage (Log)

添加引擎,分区后优化结果

Expression ((Projection + Before ORDER BY))
  SettingQuotaAndLimits (Set limits and quota after reading from storage)
    ReadFromMergeTree

1.5PIPELINE(管道)

官网示例

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

执行结果:

(Expression)
ExpressionTransform
  (Aggregating)
  Resize 4 → 1
    AggregatingTransform × 4
      (Expression)
      ExpressionTransform × 4
        (SettingQuotaAndLimits)
          (ReadFromStorage)
          NumbersMt × 4 0 → 1


 

http://www.lryc.cn/news/121101.html

相关文章:

  • 线程池
  • 配置:Terminal和oh-my-posh
  • 数据结构--BFS求最短路
  • FPGA应用学习笔记----定点除法的gold算法流水线设计
  • Nginx转发的原理和负载均衡
  • 怎么换ip地址 电脑切换ip地址方法
  • 软件设计基础
  • OptaPlanner笔记5
  • PS注意事项优漫动游
  • matplotlib 判断鼠标是否点击在当前线上
  • bash中(冒号破折号)的用法 —— 筑梦之路
  • LeetCode150道面试经典题--同构字符串(简单)
  • Redis - 数据类型映射底层结构
  • MySQL数据库表的增删查改 - 进阶
  • 8086汇编语言工作环境 百度网盘下载
  • ES6 解构
  • React三个状态时触发的相应钩子
  • 阿里云服务器部署Drupal网站教程基于CentOS系统
  • 【广州华锐视点】VR燃气轮机故障判断模拟演练系统
  • 第01天 什么是CSRF ?
  • uniapp 自定义手机顶部状态栏不生效问题
  • C++语法中bitset位图介绍及模拟实现
  • Debezium系列之:深入理解消息过滤,实现过滤数据库删除事件,只采集数据库新增和更新事件
  • Substack 如何在去中心化内容创作领域掀起波澜
  • 【MFC】07.MFC六大机制:消息映射-笔记
  • python操作数据库
  • 【C语言】小游戏-三字棋
  • 多线程与并发编程面试题总结
  • 在多页面应用和单页面应用中(例如vue)怎么提高seo搜索引擎优化
  • Dubbo 2.7.0 CompletableFuture 异步