我正在尝试为这个 DELETE(和 SELECT)查询获得最快的性能。有没有更好的方法来删除记录,因为这需要 10 多分钟才能运行?我想它必须进行自己的排序和合并,直到它可以找到记录。
SELECT COUNT([VISIT_ID])
FROM [dbo].[I2B2_SRC_VISITS]
WHERE [PATIENT_ID] NOT IN (
SELECT [PATIENT_ID] FROM [dbo].[I2B2_SRC_PATIENT]
)
DELETE FROM [dbo].[I2B2_SRC_VISITS]
WHERE [PATIENT_ID] NOT IN (
SELECT [PATIENT_ID] FROM [dbo].[I2B2_SRC_PATIENT]
)
编辑:我不能像使用 SELECT 那样将 DELETE 放在该查询前面。但这是 DELETE 语句的最终结果。
DELETE FROM [dbo].[I2B2_SRC_VISITS]
WHERE [VISIT_ID] IN
(
SELECT a.[VISIT_ID]
FROM [dbo].[I2B2_SRC_VISITS] a
LEFT JOIN [dbo].[I2B2_SRC_PATIENT] b
ON a.[PATIENT_ID] = b.[PATIENT_ID]
WHERE b.[PATIENT_ID] IS NULL
)