2014-05-19 130 views
0

我有複雜的查詢:優化DB2查詢加入

select rma.RELATION_MANAGER_ID, 
     rm.ORG_STRUCTURE_ID, 
     rm.RELATIONSHIP_MANAGER_NM, 
     count(distinct ppa.PARTY_ID) as count_party 
from RELATIONSHIP_MANAGER rm --15808 row 
join RELATIONSHIP_MANAGER_MARKET rmm --1560 row 
    on rm.RELATIONSHIP_MANAGER_ID = rmm.RELATIONSHIP_MANAGER_ID 
     and rmm.INCLUDE_IN_REPORT = 'Y' 
join MARKET_SEGMENT rm_ms --4 row 
    on rmm.MARKET_SEGMENT_ID = rm_ms.MARKET_SEGMENT_ID 
     and rm_ms.MARKET_SEGMENT = '01' 
join RELATIONSHIP_MANAGER_ALLOCATION rma --61349 row 
    on rm.RELATIONSHIP_MANAGER_ID = rma.RELATIONSHIP_MANAGER_ID 
join CMD_PARTY_PORTFOLIO_ALLOCATION ppa --3114096 row 
    on ppa.PORTFOLIO_ID = rma.PORTFOLIO_ID 
join person ps --3112575 row 
    on ps.IS_DELETED != 1 and ppa.party_id = ps.party_id 
join PARTY p --3114146 row 
    on ppa.party_id=p.party_id 
join MARKET_SEGMENT ms --4 row 
    on p.MARKET_SEGMENT_ID = ms.MARKET_SEGMENT_ID and ms.MARKET_SEGMENT = '01' 
    where rm.IS_CM = 1 and rm.IS_DELETED != 1 
group by rm.RELATIONSHIP_MANAGER_NM, rma.RELATIONSHIP_MANAGER_ID, rm.ORG_STRUCTURE_ID 

表列存在索引:

rm.RELATIONSHIP_MANAGER_ID, 
rmm.RELATIONSHIP_MANAGER_ID, 
rmm.MARKET_SEGMENT_ID, 
rm_ms.MARKET_SEGMENT_ID, 
rma.RELATIONSHIP_MANAGER_ID, 
ppa.PORTFOLIO_ID, 
rma.PORTFOLIO_ID, 
ppa.party_id, 
ps.party_id, 
p.party_id, 
p.MARKET_SEGMENT_ID, 
ms.MARKET_SEGMENT_ID 

表PARTY,人有〜1-3百萬行,查詢 運行時間〜 20秒。我的評論

join MARKET_SEGMENT ms 
on p.MARKET_SEGMENT_ID = ms.MARKET_SEGMENT_ID --and ms.MARKET_SEGMENT = '01' 

查詢運行時間變成了~3秒。 請解釋爲什麼發生這種情況? 解釋計劃不幫我..我如何優化查詢?

編輯: 平臺是針對z/OS V9.7, 增加表的大小DB2

EDIT2:解釋計劃顯示,第一總是加入表的小尺寸

+1

'MARKET_SEGMENT'沒有索引,它是文本。如果你在'On'子句中有這個比較,你將會爲每個'Join'執行比較。將條件放在'Where'子句中或者在'MARKET_SEGMENT'上放置一個索引。 – Siyual

+0

當market_segment與文本比較時,index可能不工作。 –

+0

優化與平臺有關,如果不依賴於版本的話。您的DB2服務器運行在哪個平臺上:IBM i,LUW還是z/OS? – WarrenT

回答

0

只是爲了咧嘴笑,看看這是否有任何區別:

WITH 
MktSeg(MARKET_SEGMENT_ID) AS 
(SELECT MARKET_SEGMENT_ID 
    FROM MARKET_SEGMENT 
    WHERE MARKET_SEGMENT = '01') 
select rma.RELATION_MANAGER_ID, 
     rm.ORG_STRUCTURE_ID, 
     rm.RELATIONSHIP_MANAGER_NM, 
     count(distinct ppa.PARTY_ID) as count_party 
from RELATIONSHIP_MANAGER rm --15808 row 
join RELATIONSHIP_MANAGER_MARKET rmm --1560 row 
    on rm.RELATIONSHIP_MANAGER_ID = rmm.RELATIONSHIP_MANAGER_ID 
     and rmm.INCLUDE_IN_REPORT = 'Y' 
join MktSeg rm_ms --4 row 
    on rmm.MARKET_SEGMENT_ID = rm_ms.MARKET_SEGMENT_ID 
join RELATIONSHIP_MANAGER_ALLOCATION rma --61349 row 
    on rm.RELATIONSHIP_MANAGER_ID = rma.RELATIONSHIP_MANAGER_ID 
join CMD_PARTY_PORTFOLIO_ALLOCATION ppa --3114096 row 
    on ppa.PORTFOLIO_ID = rma.PORTFOLIO_ID 
join person ps --3112575 row 
    on ps.IS_DELETED != 1 and ppa.party_id = ps.party_id 
join PARTY p --3114146 row 
    on ppa.party_id=p.party_id 
join MktSeg ms --4 row 
    on p.MARKET_SEGMENT_ID = ms.MARKET_SEGMENT_ID 
WHERE rm.IS_CM = 1 AND rm.IS_DELETED != 1 
group by rm.RELATIONSHIP_MANAGER_NM, rma.RELATIONSHIP_MANAGER_ID, 
     rm.ORG_STRUCTURE_ID, rma.RELATION_MANAGER_ID 

請注意,我已經添加了一個項目到Group By子句。

+0

運行時間是一樣的。我注意到解釋計劃太相似了。我試圖重寫不同的設計,但解釋計劃沒有改變。 –