我遇到的情况是我正在运行如下的查询,
select column1 from ABC
except
select column1 from XYZ
它已经运行了几个小时但仍未完成。有什么问题?
ABC表有16,000条记录,XYZ有5,000,000条记录。
下面是实际查询:
SELECT WFA1.ACTIVITYNM + '/' + WFA2.ACTIVITYNM AS "ACTIVITY_NAME"
FROM WFAACTIVITY WFA1, WFAACTIVITY WFA2
WHERE WFA1.ACTIVITYNM IN (SELECT DISTINCT UPPER(WORKORDER)
FROM TEMP_ACTIVITY_DETAILED_INBOUND
WHERE WORKORDER IS NOT NULL
AND PROCESS_FLAG = 'PROCESS'
AND STATUS = 'O'
AND JOB_TYPE='Direct'
)
AND WFA1.WFAACTIVITYID = WFA2.PARENTID
AND WFA1.ACTIVITYNM <> WFA2.ACTIVITYNM
EXCEPT
SELECT (UPPER(WORKORDER) + '/' + UPPER(OPERATION_NUMBER)) AS "ACTIVITY_NAME"
FROM TEMP_ACTIVITY_DETAILED_INBOUND
WHERE PROCESS_FLAG = 'PROCESS'
AND STATUS = 'O'
AND JOB_TYPE='Direct'
答案 0 :(得分:0)
您可以使用exists
:
select column1
from ABC a
where not exists (select 1 from XYZ x where a.column1 = x.column1);
要使此功能成功运行,您需要XYZ(column1)
上的索引。实际上,索引可能有助于您的原始查询。
您的查询可能很慢的其他原因: