I'm trying to get the fastest performance for this DELETE (and SELECT) query. Is there a better way to DELETE the records, because this takes over 10 minutes to run? I imagine it has to do it's own sort and merge until it can find the records.
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]
)
EDIT: I couldn't put the DELETE in front of that query like I did with the SELECT. But this was the end result for the DELETE statement.
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
)