簡體   English   中英

在特定日期連接兩個日期分區拼花 hive 表

[英]join two date partitioned parquet hive tables on specific dates

我有兩個 hive 鑲木地板表,它們根據日期進行分區,兩個表都有數百萬條記錄,我需要根據數百個特定日期和 select 大約 150 列連接這兩個表。 當我嘗試下面的 SQL 時,查詢一直在運行。 我在 pyspark 中運行這個 SQL,還有其他方法可以優化它嗎?

SQL1:

select table_a.a_col1, table_a.a_col2, table_b.b_col1, table_b.b_col2,..., table_a.a_col150, table_b.b_col150
from table_a a join table_b b
on a.col1 = b.col2
and a.date_col = b.date_col
where a.date_col in ('2022-01-01','2022-01-02','2022-01-03','2022-01-04')
and b.date_col in ('2022-01-01','2022-01-02','2022-01-03','2022-01-04')

SQL2:

select table_a.a_col1, table_a.a_col2, table_b.b_col1, table_b.b_col2,..., table_a.a_col150, table_b.b_col150
from table_a a join table_b b
on a.col1 = b.col2
where a.date_col in ('2022-01-01','2022-01-02','2022-01-03','2022-01-04')
and b.date_col in ('2022-01-01','2022-01-02','2022-01-03','2022-01-04')

您可以將所有日期存儲到 lkp 表中,並將其連接到 table_a、table_b。

第一步——首先創建一個查找表—— create table lkp_date_filter (dt timestamp);
第 2 步 - 將過濾器日期插入其中 - insert into lkp_date_filter values('2022-01-04')
第 3 步 - 將其加入您的主查詢並刪除 IN 子句。

select table_a.a_col1, table_a.a_col2, table_b.b_col1, table_b.b_col2,..., table_a.a_col150, table_b.b_col150
from table_a a 
join table_b b on a.col1 = b.col2
join lkp_date_filter on a.date_col =lkp.dt and b.date_col =lkp.dt

第 3 步將避免昂貴的 IN 子句並使 SQL 更快。 Step2 將使您可以根據需要靈活地更改過濾器值。 您可以在 date_col 上對表 a 和 b 進行分區,以使 SQL 更快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM