我正在尝试为此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]
)
编辑:我无法将DELETE放在该查询的前面,就像我使用SELECT一样。但这是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
)
答案 0 :(得分:3)
如何通过JOIN
来完成?
DELETE a
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
确保两个表中的列[PATIENT_ID]
都具有键定义,这使得它更快。
右。 NOT EXIST
比较好。
DELETE a
FROM [dbo].[I2B2_SRC_VISITS] a
WHERE NOT EXISTS
(
SELECT 1
FROM [dbo].[I2B2_SRC_PATIENT] b
WHERE a.[PATIENT_ID] = b.[PATIENT_ID]
)