hive中的union和union all的区别

在hive中,union和union all都是对两个子查询的结果合并,不过还是有区别的,union会对两个子查询的结果去重合并,而union all不会对子查询结果去重处理。

接下来验证通过实战进行验证,下面是两张表的数据。
注意:
Hive 1.2.0之前的版本仅支持UNION ALL,其中重复的行不会被删除。
Hive 1.2.0和更高版本中,UNION的默认行为是从结果中删除重复的行。

select * from temp.temp_mobile_two_tb t;

t.mobile
+8615613526666
8613598985656
18955996677
+8615613526666
8613598985656
18955996677
select * from temp.temp_mobile_tb t;

t.mobile
+8615613526666
8613598985656
18955996677

使用union查看结果

select mobile col from temp.temp_mobile_tb t1
 union 
select mobile from temp.temp_mobile_two_tb t2;

_u1.mobile
+8615613526666
18955996677
8613598985656

由此可以看出,已经去重了。

使用union all查看结果

select mobile from temp.temp_mobile_tb t1
 union all
select mobile from temp.temp_mobile_two_tb t2;

_u1.mobile
+8615613526666
8613598985656
18955996677
+8615613526666
8613598985656
18955996677
+8615613526666
8613598985656
18955996677

可以看出,union all只是对子查询的结果进行合并而已,并不会对结果去重。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章