September 24, 2015 at 10:02 am
I have a table where I need to delete rows if the member does not exists in the member table.
Will the following syntax work ?
Delete T
FROM Event_Temp_Lead_Screen T
left join member M on ( M.MemberID = T.MemberID )
where
T.Gender is NULL
September 24, 2015 at 10:21 am
If Gender is a non nullable column, it will work.
I'd usually go for something like this:
Delete T
FROM Event_Temp_Lead_Screen T
WHERE NOT EXISTS( SELECT 1 FROM member M WHERE M.MemberID = T.MemberID )
September 24, 2015 at 10:27 am
Luis Cazares (9/24/2015)
If Gender is a non nullable column, it will work.I'd usually go for something like this:
Delete T
FROM Event_Temp_Lead_Screen T
WHERE NOT EXISTS( SELECT 1 FROM member M WHERE M.MemberID = T.MemberID )
This. It's clearer than the left join as to what's been done.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 24, 2015 at 10:29 am
This is just fine
DECLARE @t TABLE (memberID int);
DECLARE @m TABLE (memberID int);
INSERT @m VALUES (1),(2),(4);
INSERT @t VALUES (1),(2),(3),(4);
SELECT * FROM @t;
DELETE T
FROM @t T
LEFT JOIN @m M on T.memberID = M.memberID
WHERE M.memberID IS NULL
SELECT * FROM @t;
GO
-- Itzik Ben-Gan 2001
September 24, 2015 at 10:32 am
using the same sample data from above and taking Luis' solution you can also do this:
WITH T AS
(
SELECT T.*
FROM @t T
WHERE NOT EXISTS( SELECT 1 FROM @m M WHERE M.MemberID = T.MemberID )
)
DELETE FROM T;
What's good about this solution is (when you have perm tables) you can highlight the query inside the CTE to see what you are going to delete. This makes testing/troubleshooting a little easier than doing it based on a join.
-- Itzik Ben-Gan 2001
September 24, 2015 at 10:35 am
Great stuff
Thanks
September 24, 2015 at 12:34 pm
Alan.B (9/24/2015)
What's good about this solution is (when you have perm tables) you can highlight the query inside the CTE to see what you are going to delete. This makes testing/troubleshooting a little easier than doing it based on a join.
Just in case of interest, we do that with the syntax mentioned earlier
DELETE T
-- SELECT COUNT(*)
-- SELECT TOP 100 T.Col1, T.Col2, ...
FROM @SomeTable AS T
LEFT JOIN SomeOtherTable AS M
on T.memberID = M.memberID
WHERE M.memberID IS NULL -- or some other criteria
I highlight from one of the commented out SELECT lines (but excluding the "--" natch!) to the bottom to see either the Count or some Columns.
I highlight from the bottom-up, execute the appropriate SELECT to check that all is as expected and then we use Shift-ArrowKeys to extend the already-highlighted selection to include either the other SELECT or, moving the cursor further, the DELETE
Apologies if I am stating the blinking obvious π
September 24, 2015 at 1:29 pm
I prefer the EXISTS approach instead of the OUTER JOIN/IS NULL approach. I think it's much clearer.
September 24, 2015 at 1:43 pm
Ed Wagner (9/24/2015)
I prefer the EXISTS approach instead of the OUTER JOIN/IS NULL approach. I think it's much clearer.
I agree, although I've got used to the LEFT OUTER JOIN and WHERE PKeyColumn IS NULL as a detection for Not Exists.
There are times were I am messing around with INSERT (if not exists) UPDATE (if exists) and DELETE (where exists), and in that situation I find it easier, and less risk of a coding mistake, to keep the exact same code and just minimally twiddle the OUTER JOIN (for NOT EXISTS on INSERT) to an INNER JOIN (for EXISTS on UPDATE)
September 24, 2015 at 3:55 pm
Beleive me, the LEFT OUTER JOIN is faster.
I deal with large tables and the "EXITS" make the query run for hours..
September 24, 2015 at 4:15 pm
mw112009 (9/24/2015)
Beleive me, the LEFT OUTER JOIN is faster.I deal with large tables and the "EXITS" make the query run for hours..
You likely have something else at play than just the difference between LEFT JOIN/IS NULL and NOT EXISTS. At any rate, the blanket statement that LEFT JOIN/IS NULL is faster than NOT EXISTS doesn't hold up.
The comparison between the two methods has been discussed at length elsewhere, so I won't pretend to invent the wheel π
http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
http://sqlperformance.com/2012/12/t-sql-queries/left-anti-semi-join
http://sqlinthewild.co.za/index.php/2010/03/23/left-outer-join-vs-not-exists/
Cheers!
September 25, 2015 at 5:23 am
Jacob Wilkins (9/24/2015)
mw112009 (9/24/2015)
Beleive me, the LEFT OUTER JOIN is faster.I deal with large tables and the "EXITS" make the query run for hours..
You likely have something else at play than just the difference between LEFT JOIN/IS NULL and NOT EXISTS. At any rate, the blanket statement that LEFT JOIN/IS NULL is faster than NOT EXISTS doesn't hold up.
The comparison between the two methods has been discussed at length elsewhere, so I won't pretend to invent the wheel π
http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
http://sqlperformance.com/2012/12/t-sql-queries/left-anti-semi-join
http://sqlinthewild.co.za/index.php/2010/03/23/left-outer-join-vs-not-exists/
Cheers!
You're correct that the LEFT OUTER JOIN/IS NULL is faster. An even faster way is using EXCEPT. I'll see if I can find the thread where several different approaches where compared.
September 25, 2015 at 6:54 am
mw112009 (9/24/2015)
Beleive me, the LEFT OUTER JOIN is faster.I deal with large tables and the "EXITS" make the query run for hours..
Are we supposed to take your word for granted disregarding what Aaron Bertrand or Gail Shaw have posted with actual performance testing?
You realize that you just posted on a forum if the syntax is correct which takes away some credibility from your words.
However, you might not be wrong. Share with us what you used to test and compare the methods. Evidence through testing is better than any expert's words.
September 25, 2015 at 9:08 am
Luis Cazares (9/25/2015)
mw112009 (9/24/2015)
Beleive me, the LEFT OUTER JOIN is faster.I deal with large tables and the "EXITS" make the query run for hours..
Are we supposed to take your word for granted disregarding what Aaron Bertrand or Gail Shaw have posted with actual performance testing?
You realize that you just posted on a forum if the syntax is correct which takes away some credibility from your words.
However, you might not be wrong. Share with us what you used to test and compare the methods. Evidence through testing is better than any expert's words.
There was a thread a while back where the performance of just such a delete was debated. Someone (I believe Eirikur) did a performance comparison of the different methods. The approach using EXCEPT won hands down, but I can't find the thread. If anyone else can find it, I'd very much appreciate a link. I just don't have the knack of using the search here on SSC.
September 25, 2015 at 9:14 am
Ed Wagner (9/25/2015)
Luis Cazares (9/25/2015)
mw112009 (9/24/2015)
Beleive me, the LEFT OUTER JOIN is faster.I deal with large tables and the "EXITS" make the query run for hours..
Are we supposed to take your word for granted disregarding what Aaron Bertrand or Gail Shaw have posted with actual performance testing?
You realize that you just posted on a forum if the syntax is correct which takes away some credibility from your words.
However, you might not be wrong. Share with us what you used to test and compare the methods. Evidence through testing is better than any expert's words.
There was a thread a while back where the performance of just such a delete was debated. Someone (I believe Eirikur) did a performance comparison of the different methods. The approach using EXCEPT won hands down, but I can't find the thread. If anyone else can find it, I'd very much appreciate a link. I just don't have the knack of using the search here on SSC.
Doesn't work for me either. I always use Google with the site hint, like this:
site:sqlservercentral.com eirikur except delete
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply