hive 谓词下推实例分析(on与where的区别)

测试数据

  • t1 表
bash 复制代码
select * from t1;
+--------+----------+---------+--------+
| t1.id  | t1.name  | t1.age  | t1.dt  |
+--------+----------+---------+--------+
| 1      | aa       | 12      | 01     |
| 1      | aa       | 12      | 02     |
| 2      | aa       | 14      | 01     |
| 2      | bb       | 14      | 02     |
| 3      | cc       | 16      | 02     |
| NULL   | aa       | 12      | 01     |
+--------+----------+---------+--------+
  • t2 表
bash 复制代码
select * from t2;
+--------+----------+---------+--------+
| t2.id  | t2.name  | t2.age  | t2.dt  |
+--------+----------+---------+--------+
| 1      | 1        | aa      | 12     |
| 2      | 1        | aa      | 12     |
| 1      | NULL     | aa      | 12     |
| 1      | 2        | aa      | 14     |
| 2      | 2        | bb      | 14     |
| 2      | 3        | cc      | 16     |
+--------+----------+---------+--------+

关联查询

  • t1 left join t2
bash 复制代码
select * from t1 a left join t2 b on a.id=b.id;
+-------+---------+--------+-------+-------+---------+--------+-------+
| a.id  | a.name  | a.age  | a.dt  | b.id  | b.name  | b.age  | b.dt  |
+-------+---------+--------+-------+-------+---------+--------+-------+
| 3     | cc      | 16     | 02    | NULL  | NULL    | NULL   | NULL  |
| NULL  | aa      | 12     | 01    | NULL  | NULL    | NULL   | NULL  |
| 1     | aa      | 12     | 01    | 1     | NULL    | aa     | 12    |
| 1     | aa      | 12     | 01    | 1     | 1       | aa     | 12    |
| 1     | aa      | 12     | 01    | 1     | 2       | aa     | 14    |
| 1     | aa      | 12     | 02    | 1     | NULL    | aa     | 12    |
| 1     | aa      | 12     | 02    | 1     | 1       | aa     | 12    |
| 1     | aa      | 12     | 02    | 1     | 2       | aa     | 14    |
| 2     | aa      | 14     | 01    | 2     | 3       | cc     | 16    |
| 2     | aa      | 14     | 01    | 2     | 1       | aa     | 12    |
| 2     | aa      | 14     | 01    | 2     | 2       | bb     | 14    |
| 2     | bb      | 14     | 02    | 2     | 3       | cc     | 16    |
| 2     | bb      | 14     | 02    | 2     | 1       | aa     | 12    |
| 2     | bb      | 14     | 02    | 2     | 2       | bb     | 14    |
+-------+---------+--------+-------+-------+---------+--------+-------+
  • t1 left join t2 on a.id=b.id where a.dt ='01',保留表谓词下推,map端提前过滤
bash 复制代码
select * from t1 a left join t2 b on a.id=b.id where a.dt ='01';
+-------+---------+--------+-------+-------+---------+--------+-------+
| a.id  | a.name  | a.age  | a.dt  | b.id  | b.name  | b.age  | b.dt  |
+-------+---------+--------+-------+-------+---------+--------+-------+
| NULL  | aa      | 12     | 01    | NULL  | NULL    | NULL   | NULL  |
| 1     | aa      | 12     | 01    | 1     | NULL    | aa     | 12    |
| 1     | aa      | 12     | 01    | 1     | 1       | aa     | 12    |
| 1     | aa      | 12     | 01    | 1     | 2       | aa     | 14    |
| 2     | aa      | 14     | 01    | 2     | 3       | cc     | 16    |
| 2     | aa      | 14     | 01    | 2     | 1       | aa     | 12    |
| 2     | aa      | 14     | 01    | 2     | 2       | bb     | 14    |
+-------+---------+--------+-------+-------+---------+--------+-------+
  • t1 left join t2 on a.id=b.id and a.dt ='01',保留表非谓词下推,reduce端过滤
bash 复制代码
select * from t1 a left join t2 b on a.id=b.id and a.dt ='01';
+-------+---------+--------+-------+-------+---------+--------+-------+
| a.id  | a.name  | a.age  | a.dt  | b.id  | b.name  | b.age  | b.dt  |
+-------+---------+--------+-------+-------+---------+--------+-------+
| 1     | aa      | 12     | 02    | NULL  | NULL    | NULL   | NULL  |
| 2     | bb      | 14     | 02    | NULL  | NULL    | NULL   | NULL  |
| 3     | cc      | 16     | 02    | NULL  | NULL    | NULL   | NULL  |
| NULL  | aa      | 12     | 01    | NULL  | NULL    | NULL   | NULL  |
| 1     | aa      | 12     | 01    | 1     | 2       | aa     | 14    |
| 1     | aa      | 12     | 01    | 1     | NULL    | aa     | 12    |
| 1     | aa      | 12     | 01    | 1     | 1       | aa     | 12    |
| 2     | aa      | 14     | 01    | 2     | 2       | bb     | 14    |
| 2     | aa      | 14     | 01    | 2     | 3       | cc     | 16    |
| 2     | aa      | 14     | 01    | 2     | 1       | aa     | 12    |
+-------+---------+--------+-------+-------+---------+--------+-------+

说明:保留表应用谓词下推,提前过滤,会把不符合条件的数据提前过滤掉;保留表不应用谓词下推,不提前过滤,只能在join发生时,不符合条件的数据不参与关联计算;

  • t1 left join t2 on a.id=b.id and b.dt ='12',空表谓词下推,map端过滤
bash 复制代码
select * from t1 a left join t2 b on a.id=b.id and b.dt ='12';
+-------+---------+--------+-------+-------+---------+--------+-------+
| a.id  | a.name  | a.age  | a.dt  | b.id  | b.name  | b.age  | b.dt  |
+-------+---------+--------+-------+-------+---------+--------+-------+
| 2     | aa      | 14     | 01    | 2     | 1       | aa     | 12    |
| 2     | bb      | 14     | 02    | 2     | 1       | aa     | 12    |
| 3     | cc      | 16     | 02    | NULL  | NULL    | NULL   | NULL  |
| NULL  | aa      | 12     | 01    | NULL  | NULL    | NULL   | NULL  |
| 1     | aa      | 12     | 01    | 1     | NULL    | aa     | 12    |
| 1     | aa      | 12     | 01    | 1     | 1       | aa     | 12    |
| 1     | aa      | 12     | 02    | 1     | NULL    | aa     | 12    |
| 1     | aa      | 12     | 02    | 1     | 1       | aa     | 12    |
+-------+---------+--------+-------+-------+---------+--------+-------+
  • t1 left join t2 on a.id=b.id where b.dt ='12',空表非谓词下推,reduce端过滤
bash 复制代码
select * from t1 a left join t2 b on a.id=b.id where b.dt ='12';
+-------+---------+--------+-------+-------+---------+--------+-------+
| a.id  | a.name  | a.age  | a.dt  | b.id  | b.name  | b.age  | b.dt  |
+-------+---------+--------+-------+-------+---------+--------+-------+
| 2     | aa      | 14     | 01    | 2     | 1       | aa     | 12    |
| 2     | bb      | 14     | 02    | 2     | 1       | aa     | 12    |
| 1     | aa      | 12     | 01    | 1     | NULL    | aa     | 12    |
| 1     | aa      | 12     | 01    | 1     | 1       | aa     | 12    |
| 1     | aa      | 12     | 02    | 1     | NULL    | aa     | 12    |
| 1     | aa      | 12     | 02    | 1     | 1       | aa     | 12    |
+-------+---------+--------+-------+-------+---------+--------+-------+

说明:空表应用谓词下推,提前过滤,会把不符合条件的数据提前过滤掉;空表不应用谓词下推,不提前过滤,只能在join完成时,过滤不符合条件的数据;

总结 : 是否应用谓词下推,最后产生的结果往往不同,这里需要特别注意。

参考:
一文弄懂Hive中谓词下推(on与where的区别)

相关推荐
恋猫de小郭1 小时前
Flutter 新春第一弹,Dart 宏功能推进暂停,后续专注定制数据处理支持
android·java·flutter
vibag1 小时前
Kubernetes(一)
java·云原生·容器·kubernetes
ChinaRainbowSea6 小时前
四.4 Redis 五大数据类型/结构的详细说明/详细使用( zset 有序集合数据类型详解和使用)
java·javascript·数据库·redis·后端·nosql
苏-言7 小时前
SSM框架探秘:Spring 整合 Mybatis 框架
java·spring·mybatis
qq_447663057 小时前
java-----多线程
java·开发语言
a辰龙a7 小时前
【Java报错解决】警告: 源发行版 11 需要目标发行版 11
java·开发语言
听海边涛声7 小时前
JDK长期支持版本(LTS)
java·开发语言
IpdataCloud7 小时前
Java 获取本机 IP 地址的方法
java·开发语言·tcp/ip
MyMyMing7 小时前
Java的输入和输出
java·开发语言
忆~遂愿7 小时前
3大关键点教你用Java和Spring Boot快速构建微服务架构:从零开发到高效服务注册与发现的逆袭之路
java·人工智能·spring boot·深度学习·机器学习·spring cloud·eureka