2017-04-06 45 views
1

我正在加入HANA上的兩個表,爲了獲得一些統計信息,我將LEFT加入項目表3次以獲得總數,條目數處理和錯誤的數量,如下所示。在同一個表上聚合的多個左連接導致SAP HANA中的巨大性能下降

這是一個開發系統,項目表只有1500個項目。但下面的查詢運行17秒。

當我刪除三個聚合術語中的任何一個(但保留相應的JOIN)時,查詢幾乎立即執行。

我也嘗試在特定的JOIN中使用的字段上添加索引,但這沒有區別。

select rk.guid, rk.run_id, rk.status, rk.created_at, rk.created_by, 
count(distinct rp.guid), 
count(distinct rp2.guid), 
count(distinct rp3.guid) 
    from zbsbpi_rk as rk 
    left join zbsbpi_rp as rp 
     on rp.header = rk.guid 
    left join zbsbpi_rp as rp2 
     on rp2.header = rk.guid 
    and rp2.processed = 'X' 
    left join zbsbpi_rp as rp3 
     on rp3.header = rk.guid 
    and rp3.result_status = 'E' 
    where rk.run_id = '0000000010' 
    group by rk.guid, run_id, status, created_at, created_by 

回答

0

我很抱歉,但我忘了我在這裏發佈了這個問題。我在answers.sap.com上發佈了同樣的問題,因爲沒有得到任何喜悅:https://answers.sap.com/questions/172096/multiple-left-joins-with-aggregation-on-same-table.html

我終於想出瞭解決方案,這有點「doh!」時刻:

select rk.guid, rk.run_id, rk.status, rk.created_at, rk.created_by, 
    count(distinct rp.guid), 
    count(distinct rp2.guid), 
    count(distinct rp3.guid) 
    from zbsbpi_rk as rk 
    join zbsbpi_rp as rp 
     on rp.header = rk.guid 
    left join zbsbpi_rp as rp2 
     on rp2.guid = rp.guid 
    and rp2.processed = 'X' 
    left join zbsbpi_rp as rp3 
     on rp3.guid = rp.guid 
    and rp3.result_status = 'E' 
    where rk.run_id = '0000000010' 
    group by rk.guid, run_id, status, created_at, created_by 

只聯接所需的後續左側被連接到同一個表先加入,作爲首次加入包含的所有記錄的超集反正。

0

我想你可以重新寫你查詢以提高性能:

select rk.guid, rk.run_id, rk.status, rk.created_at, rk.created_by, 
count(distinct rp.guid), 
count(distinct (CASE WHEN rp.processed = 'X' then rp.guid else null end)), 
count(distinct (CASE WHEN rp.result_status = 'E' then rp.guid else null end)) 
    from zbsbpi_rk as rk 
    left join zbsbpi_rp as rp 
     on rp.header = rk.guid 
where rk.run_id = '0000000010' 
    group by rk.guid, run_id, status, created_at, created_by 

我不能完全肯定是否數不同的情況下構建上花工作,但你可以嘗試。

相關問題