我的目标是清除数据块,并考虑将ID_COl值放入#temptable之后循环遍历。
DECLARE @PD datetime = DATEADD(day, -5, GETUTCDATE());
DELETE GP
FROM dbo.Table_A N
INNER JOIN dbo.Table_BL GP
ON N.NOTIF_ID = GP.NOTIF_ID
WHERE N.FCT_AUD_IND = 'Y'
AND N.CRE_TMST < @PD
AND EXISTS ( SELECT 1 ID_COl
FROM dbo.Table_A AS N1
WHERE N1.NOTIF_APPL_ID = N.NOTIF_APPL_ID
AND N1.SOR_NOTIF_APPL_GP_ID = N.SOR_NOTIF_APPL_GP_ID
AND N1.NOTIF_DLT_IND = 'Y');
答案 0 :(得分:1)
您正在强迫SQL Server进行嵌套循环以查找Table_A中的记录,但是当您让他决定并加入JOIN时,也许SQL Server会找到更好的方法:
DECLARE @PD datetime = DATEADD(day, -5, GETUTCDATE());
WITH N1 AS (
SELECT DISTINCT NOTIF_APPL_ID, SOR_NOTIF_APPL_GP_ID
FROM dbo.Table_A
WHERE NOTIF_DLT_IND = 'Y'
)
DELETE GP
FROM dbo.Table_A N
INNER JOIN N1 ON N.NOTIF_APPL_ID = N1.NOTIF_APPL_ID
AND N.SOR_NOTIF_APPL_GP_ID = N1.SOR_NOTIF_APPL_GP_ID
INNER JOIN dbo.Table_BL GP ON N.NOTIF_ID = GP.NOTIF_ID
WHERE N.FCT_AUD_IND = 'Y'
AND N.CRE_TMST < @PD
此外,定义索引可能会有所帮助。