当前位置: 代码网 > it编程>数据库>大数据 > hive anti join 的几种写法

hive anti join 的几种写法

2024年07月28日 大数据 我要评论
注意 left join 中 b.c1 is null 不能谓词下推。我们要在t_a 中出现,而不在 t_b中出现的记录。t_a 表的记录如下。t_b 表的记录如下。

t_a 表的记录如下

c1 |
:———— |
a |
b |
c |

生成 sql 如下:

create table t_a(c1 string);
insert into t_a values("a"),("b"),("c");

t_b 表的记录如下

c1
b
m

生成 sql 如下:

create table t_b(c1 string);
insert into t_b values("b"),("m");

我们要在t_a 中出现,而不在 t_b中出现的记录。
结果需要为:

c1
a
c
  • 写法1 — 使用not in
select * from t_a 
where c1 not in(select c1 from t_b);
  • 写法2 —使用 left join 关联上的去掉
    这种写法不容易读懂。
select a.* from t_a a left join t_b b
on a.c1=b.c1
where b.c1 is null;

注意 left join 中 b.c1 is null 不能谓词下推。

生成的执行计划如下。注意,在 join 后才过滤 _col1 is null,关联上的 _col1 肯定是 not null,所以关联上的全去掉。

plan optimized by cbo.

vertex dependency in root stage
map 1 <- map 2 (broadcast_edge)

stage-0
  fetch operator
    limit:-1
    stage-1
      map 1 vectorized
      file output operator [fs_20]
        select operator [sel_19] (rows=1 width=93)
          output:["_col0"]
          filter operator [fil_18] (rows=1 width=93)
            predicate:_col1 is null
            map join operator [mapjoin_17] (rows=2 width=93)
              conds:sel_16._col0=rs_15._col0(left outer),output:["_col0","_col1"]
            <-map 2 [broadcast_edge] vectorized
              broadcast [rs_15]
                partitioncols:_col0
                select operator [sel_14] (rows=2 width=85)
                  output:["_col0"]
                  tablescan [ts_2] (rows=2 width=85)
                    ods@t_b,b,tbl:complete,col:none,output:["c1"]
            <-select operator [sel_16] (rows=2 width=85)
                output:["_col0"]
                tablescan [ts_0] (rows=2 width=85)
                  ods@t_a,a,tbl:complete,col:none,output:["c1"]

time taken: 0.159 seconds, fetched: 29 row(s)
  • 写法3 — except
    这种写法运行速度比较慢,并且如果每个表有多个字段,但是,仅按少数的字段进行判断的话就不适合。
select * from t_a except select * from t_b;
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com