在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只是對子查詢的結果進行合併而已,並不會對結果去重。