Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Except

Author  Topic 

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2007-09-26 : 06:55:10
Is it better to use the new EXCEPT set operator than NOT EXISTS?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-26 : 07:10:38
It depends on many things.

1) How the two compared resultset look like?
2) How are the two resultsets gathered?
3) What are you trying to do with the EXCEPT resultset?



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-09-26 : 07:11:35
Execution plan generated is same for EXCEPT as well as NOT EXISTS, but you get much cleaner and highly readable code with EXCEPT.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2007-09-26 : 07:14:44
quote:
Originally posted by harsh_athalye

Execution plan generated is same for EXCEPT as well as NOT EXISTS, but you get much cleaner and highly readable code with EXCEPT.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"



Same plan hey?
Very interesting.
Thanks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-26 : 07:23:23
quote:
Originally posted by harsh_athalye

Execution plan generated is same for EXCEPT as well as NOT EXISTS, but you get much cleaner and highly readable code with EXCEPT.
Do you have an example?
I don't feel like putting sample data together myself



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2007-10-01 : 06:11:34
I'm finding EXCEPT to be less useful in cases where you want to exclude based on the join to one column (the reason being that it operates like UNION - expecting the same amount of columns):

So I can't change this:
select A, B
from C
where not exists (select * from D where D.A=C.A)

into this:

select A, B
from C
except
select A
from D

So it really does depend on what you are doing.

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-10-01 : 07:05:28
I am pleased to see you did some research yourself!
Kudos.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2007-10-01 : 08:34:03
Outch
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-10-01 : 14:15:18
I know your question was about EXISTS and EXCEPT. But, you might want to try LEFT OUTER JOIN. Try both ways and see what performs better.
SELECT 
C.a,
C.b
FROM
C
LEFT OUTER JOIN
D
ON C.a = D.a
WHERE
D.a IS NULL
Go to Top of Page

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2007-10-01 : 17:27:10
Yes Lamprey, that is indeed the "most set-based" way to write the query in my opinion. It shows a good and clear understanding of joining and the sequence of steps that are followed (Join, where, select - in that order.)
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2007-10-02 : 11:47:31
Now, check the query plans against the left join and the EXCEPT and see what the difference is

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -