clickhouse21.7.11.3 SQL语法优化-谓词下推

说明

当 group by 有 having 子句,但是没有 with cube 、with rollup 或者 with totals 修饰的时候,having 过滤会下推到 where 提前过滤,即:having 变成 where

例子

下面的查询,having sid 变成了 where sid ,在 group by 之前过滤:

explain syntax select sid,avg(age) from t_student_mt group by sid having sid = '2'

执行结果如下:

SELECT           
    sid,         
    avg(age)     
FROM t_student_mt
WHERE sid = '2'  
GROUP BY sid

子查询也支持谓词下推

explain syntax select * from (select * from t_student_mt) where sid = '2'

执行结果如下:

SELECT               
    sid,             
    name,            
    age,             
    height,          
    create_time      
FROM                 
(                    
    SELECT           
        sid,         
        name,        
        age,         
        height,      
        create_time  
    FROM t_student_mt
    WHERE sid = '2'  # 过滤条件出现在子语句中
)                    
WHERE sid = '2'

原文出处:/show_1IX3QpqNC0hE.html