你能幫我優化這個查詢:能不能幫我優化SQL查詢DB2
select distinct PUBLSEQ from PUBLCOVER P
WHERE EXISTS
(SELECT * FROM PUBLCOVER A, COVERAREA B, PRODCODEDICT C
WHERE A.PUBLSEQ = P.PUBLSEQ
AND A.COVER_STATUS = 'Y'
AND A.COVAREA = B.COVAREA
AND B.COVAREA_PRODCODE IS NOT NULL
AND B.COVAREA_PRODCODE = C.PRODCODE
AND C.STATUS = 'A'
AND A.COVAREA = 1823
)
AND NOT EXISTS
(SELECT * FROM PUBLCOVER A, COVERAREA B, PRODCODEDICT C
WHERE A.PUBLSEQ = P.PUBLSEQ
AND A.COVER_STATUS = 'Y'
AND A.COVAREA = B.COVAREA
AND B.COVAREA_PRODCODE IS NOT NULL
AND B.COVAREA_PRODCODE = C.PRODCODE
AND C.STATUS = 'A'
AND A.COVAREA NOT IN (1823, 34)
)
我想從EXISTS
或NOT EXISTS
逃跑,只留下一個。
您要求A.COVAREA = 1823,也不是1823或34.與A.COVAREA = 1823和(1823或34)不一樣。這與1823年一樣?換句話說,第二個EXISTS是否真的做了什麼? –
@MichaelY。大概有一個給定值爲'PUBLSEQ'的多條記錄,所以'EXISTS'確保至少有一個匹配項具有'1823'的'COVAREA'值,'NOT EXISTS'確保所有匹配項都是'1823'或'34'。或者,我只是累了。 –
您的查詢實際上是否返回除1823之外的任何內容? –