Reputation: 13278
Let me explain by way of example. Consider the following table:
Customer Id | Food
------------+---------
1 | Pizza
1 | Burger
1 | Hot Dog
2 | Milkshake
2 | Burger
3 | Pizza
I want to delete all the records for customers who have NEVER ordered Pizza. So, I should be left with this (customer #2 deleted):
Customer Id | Food
------------+---------
1 | Pizza
1 | Burger
1 | Hot Dog
3 | Pizza
I know I can do a NOT IN
but the performance is horrible.
What is the most performant way to write this query to achieve this against 100,000+ records in SQL Server?
Upvotes: 7
Views: 200
Reputation: 3466
Upvotes: 0
Reputation: 460238
A simple NOT EXISTS
should be efficient with proper indexes.
DELETE c1 FROM Customers c1
WHERE NOT EXISTS
(
SELECT 1 FROM Customers c2
WHERE c1.[Customer Id] = c2.[Customer Id]
AND c2.[Food] = 'Pizza'
);
Create an index on [Customer Id]
and a non-clustered index on Food
.
Upvotes: 9
Reputation: 24144
try
delete t from t
left join (select distinct [Customer Id] from t where Food='Pizza') t2
on t.[Customer Id]=t2.[Customer Id]
where t2.[Customer Id] is null
Upvotes: 0
Reputation: 263843
how about NOT EXISTS
DELETE a
FROM table1 a
WHERE NOT EXISTS
(
SELECT 1
FROM table1 b
WHERE a.customerID = b.customerID AND
b.Food = 'Pizza'
)
Upvotes: 4