TSQL Efficient "Not Exists"
A more efficient way of writing a not-exists statement
I used to write queries such as...
DELETE FROM [MyTable]
WHERE [Id] NOT IN (SELECT [Id] FROM [OtherTable])
However, I've recently discovered that the NOT IN is very inefficient. Instead I now use the following format...
DELETE FROM MT
FROM [MyTable] MT
WHERE NOT EXISTS (
SELECT 1
FROM [OtherTable] OT
WHERE OT.[Id] = MT.[Id]
)
At first glance it looks more inefficient, but apparently not
Added 01/12/2026 11:49