我有下面的oracle查询。运行需要很长时间。你能否为这个查询建议一些性能调整。
select ss.registration_id as REGISTRATION_ID,
ss.batch_id as BATCH_ID
from submtd_srvc PARTITION(SUBMTD_SRVC_821370) ss,
(select a.exceptn_criteria_val,
a.exceptn_criteria_rtrn_val
from EXCEPTN_CRITERIA a,
EXCEPTN_EXPRESSION b
where a.EXCEPTN_EXPRESSION_ID = b.EXCEPTN_EXPRESSION_ID
and b.EXCEPTN_EXPRESSION_NAME = 'NC_CUSTOM_REV_CAT'
and b.IS_CURRENT_INDCTR = 1
and a.IS_CURRENT_INDCTR = 1) sub_query
where ss.REVENUE_CD = sub_query.exceptn_criteria_val
and ss.batch_id = 821370
and exists (select 'x'
from submtd_srvc PARTITION(SUBMTD_SRVC_821370) ss2,
(select a.exceptn_criteria_val,
a.exceptn_criteria_rtrn_val
from EXCEPTN_CRITERIA a,
EXCEPTN_EXPRESSION b
where a.EXCEPTN_EXPRESSION_ID = b.EXCEPTN_EXPRESSION_ID
and b.EXCEPTN_EXPRESSION_NAME = 'NC_CUSTOM_REV_CAT'
and b.IS_CURRENT_INDCTR = 1
and a.IS_CURRENT_INDCTR = 1) sub_query2
where ss2.REVENUE_CD = sub_query2.exceptn_criteria_val
and ss2.registration_id = ss.registration_id
and ss2.batch_id = ss.batch_id
and ss2.batch_id = 821370
and sub_query2.exceptn_criteria_rtrn_val <> sub_query.exceptn_criteria_rtrn_val)
Order By Ss.Registration_Id,
ss.batch_id;
答案 0 :(得分:2)
如果我理解正确,您可以使用关联子查询来查找具有多个不同 exceptn_criteria_rtrn_val 值的行。如果我正确的话会更好地运用分析函数:
select vw.registration_id as REGISTRATION_ID, vw.batch_id as BATCH_ID
from
(select ss.registration_id as REGISTRATION_ID, ss.batch_id as BATCH_ID
,count(distinct sub_query.exceptn_criteria_rtrn_val) over(partition by ss.registration_id, ss.batch_id) as cnt
from submtd_srvc PARTITION(SUBMTD_SRVC_821370) ss,
(select a.exceptn_criteria_val, a.exceptn_criteria_rtrn_val
from EXCEPTN_CRITERIA a, EXCEPTN_EXPRESSION b
where a.EXCEPTN_EXPRESSION_ID = b.EXCEPTN_EXPRESSION_ID
and b.EXCEPTN_EXPRESSION_NAME = 'NC_CUSTOM_REV_CAT'
and b.IS_CURRENT_INDCTR = 1
and a.IS_CURRENT_INDCTR = 1) sub_query
where ss.REVENUE_CD = sub_query.exceptn_criteria_val
and ss.batch_id = 821370) vw
where cnt > 1
Order By vw.Registration_Id, vw.batch_id;
答案 1 :(得分:1)
您可以将EXISTS
替换为GROUP BY
和HAVING
:
SELECT ss.registration_id,
ss.batch_id
FROM submtd_srvc PARTITION(SUBMTD_SRVC_821370) ss
INNER JOIN
EXCEPTN_CRITERIA a
ON ( ss.REVENUE_CD = a.exceptn_criteria_val )
INNER JOIN
EXCEPTN_EXPRESSION b
ON ( a.EXCEPTN_EXPRESSION_ID = b.EXCEPTN_EXPRESSION_ID )
WHERE b.EXCEPTN_EXPRESSION_NAME = 'NC_CUSTOM_REV_CAT'
AND b.IS_CURRENT_INDCTR = 1
AND a.IS_CURRENT_INDCTR = 1
AND ss.batch_id = 821370
GROUP BY
ss.Registration_Id,
ss.batch_id
HAVING COUNT( DISTINCT a.exceptn_criteria_rtrn_val ) = 1
ORDER BY
ss.Registration_Id,
ss.batch_id;
我还摆脱了所有的子查询,并使用了ANSI标准连接,而不是传统的Oracle连接。