2017-10-05 37 views
1

當我有一個在整個select語句中重複多次的複雜子查詢時,在Hive中構建/寫入查詢的最佳方式是什麼?配置單元 - 如何在配置單元中以最優性能重用子查詢

我最初爲每次運行前刷新的子查詢創建了一個臨時表。然後,我開始使用CTE作爲原始查詢的一部分(丟棄臨時表)以提高可讀性並注意到性能下降。這讓我很好奇哪些實現方法在需要重新使用子查詢時的性能方面是最好的。

我正在使用的數據包含超過1000萬條記錄。以下是我使用CTE編寫的查詢的一個示例。

with temp as (
    select 
     a.id, 
     x.type, 
     y.response 
    from sandbox.tbl_form a 
    left outer join sandbox.tbl_formStatus b 
    on a.id = b.id 
    left outer join sandbox.tbl_formResponse y 
    on b.id = y.id 
    left outer join sandbox.tbl_formType x 
    on y.id = x.typeId 
    where b.status = 'Completed' 
) 
select 
    a.id, 
    q.response as user, 
    r.response as system, 
    s.response as agent, 
    t.response as owner 
from sandbox.tbl_form a 
left outer join (
    select * from temp x 
    where x.type= 'User' 
) q 
on a.id = q.id 
left outer join (
    select * from temp x 
    where x.type= 'System' 
) r 
on a.id = r.id 
left outer join (
    select * from temp x 
    where x.type= 'Agent' 
) s 
on a.id = s.id 
left outer join (
    select * from temp x 
    where x.type= 'Owner' 
) t 
on a.id = t.id; 

回答

1

查詢中有問題。

1)在CTE中,您有三個沒有ON子句的左連接。這可能會導致嚴重的性能問題,因爲沒有ON子句的連接是CROSS JOINS。

2)BTW where b.status = 'Completed'子句將表b的LEFT連接轉換爲內部連接,儘管仍然沒有ON子句,它將b中的所有記錄與a中的所有記錄相乘。 3)很可能你根本不需要CTE。只需與ON條款正確連接並使用case when type='User' then response end +使用min()max()的聚合使用id