谓词下推
刘梦凯 Lv3

学习谓词下推相关笔记

谓词下推

谓词

​ 概念:计算机科学中谓词解释,谓词不是像语言中的某一种词性,比如英语中的动词或者名词,谓词是一个返回值为真、假、未知(True/False/Unknow)的函数,一般我们以元素列表的形式应用于给定的谓词函数,并且根据函数返回值决定是否返回该元素,这个函数就叫做谓词。

​ SQL中哪些常见的谓词:Like、Between、Is Null、Is Not Null 等等

下推

​ 概念:将过滤表达式尽可能移动至靠近数据源的位置,以使真正执行时能直接跳过无关的数据。

1
2
# A,B 两表进行join操作 并且有过滤条件 A.a > 10 and B.b < 100
select count(1) from A Join B on A.id = B.id where A.a > 10 and B.b < 100;

​ 执行的时候会先将过滤条件尽可能的前提,比如到对表进行TableScan的时候进行,在提取每一行数据的时候会进行一个判断该表是否满足条件,如果不满足直接不会读取,最后再进行Join操作,这样可以大大降低数据量,比如上面例子我们执行的时候优化筛选条件 A.a > 10 and B.b < 100 放到扫描表的时候进行,可以提高性能。

​ 像Parquet这种特殊的格式对谓词下推做了特殊的处理 Parquet谓词下推参考连接

​ 这块值得展开学习:

  1. Parquet谓词下推如何优化
  2. 行存储数据库以及列存储数据库谓词下推的方法不同。

Outer Join中的谓词下推

​ 如果阅读英文文档没有问题最好阅读一遍官方文档

​ 在弄清楚Hive如何处理谓词下推之前需要搞清楚四个概念:

Preserved Row table:Outer Join操作中必须返回所有行的表,例如left join的时候左表就是Preserved Row table,相当于固定住了一个表的内容

Null Supplying table:Outer Join操作中不用必须返回所有行的表,比如left join的时候左表是全部都要返回,当右表不满足on条件的时候他就会返回null,所以形象的称为Null Supplying Table

During Join predicate:指join…on…中的谓词

After Join predicate:指在join操作后,where中的谓词

了解上面四个概念之后,开始看一下Hive的谓词下推原理概念(Predicate Pushdown PPD)

Hive中的谓词下推主要有两个原理,下面我们讲解这两个原理

1
2
3
# Hive Predicate Pushdown
During Join predicates cannot be pushed past Preserved Row tables.
After Join predicates cannot be pushed past Null Supplying tables.
  1. During Join predicates cannot be pushed past Preserved Row tables.

    在join..on…条件中Preserved Row tables表的谓词不能下推,举一个例子就是left join的时候on中的左表的条件不能下推。

    1
    2
    # E.eid='HZ001'不能下推 因为此时左表需要全部保留
    select ename,dept_name from E left outer join D on ( E.dept_id = D.dept_id and E.eid='HZ001');
  2. After Join predicates cannot be pushed past Null Supplying tables.

    在After Join predicate中 Null Supplying tables表的谓词是不能下推的,这个也比较好理解,还是以left join举例,左关联的时候左表全部保留,右表数据是否保留应该取决于on中的关联条件是否满足,其次取决于where中右表条件,而不能先使用where中右表的条件过滤右表数据。

    1
    2
    # D.dept_id='D001' 不能下推 因为此时右表数据应取决于on中关联
    select ename,dept_name from E left outer join D on E.dept_id = D.dept_id where D.dept_id='D001';
  3. Full Outer Join形式是任何条件都不能下推谓词,因为需要左右两表全都保留数据

    1
    2
    3
    4
    5
    # full outer join 任何条件都不能下推 下面四个都不能下推
    select ename,dept_name from E full outer join D on ( E.dept_id = D.dept_id and E.eid='HZ001');
    select ename,dept_name from E full outer join D on E.dept_id = D.dept_id where E.eid='HZ001';
    select ename,dept_name from E full outer join D on ( E.dept_id = D.dept_id and D.dept_id='D001');
    select ename,dept_name from E full outer join D on E.dept_id = D.dept_id where D.dept_id='D001';

Inner Join中的谓词下推

​ 对于inner join情况,因为左右表都不是Preserved Row table 或者Null Supplying table,都需要被条件过滤,此时During Join predicate条件的谓词全都可以前提,也就是join on中条件,同时After Join predicate,也就是where中的条件也可以被前置。

参考文献:

https://blog.csdn.net/strongyoung88/article/details/81156271

https://cwiki.apache.org/confluence/display/Hive/OuterJoinBehavior

  • Post title:谓词下推
  • Post author:刘梦凯
  • Create time:2022-06-07 22:28:55
  • Post link:https://liumengkai.github.io/2022/06/07/谓词下推/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.