| 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" |
 |
|
|
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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
Same plan hey?Very interesting.Thanks |
 |
|
|
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" |
 |
|
|
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, Bfrom Cwhere not exists (select * from D where D.A=C.A)into this:select A, Bfrom Cexceptselect Afrom DSo it really does depend on what you are doing. |
 |
|
|
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" |
 |
|
|
coolerbob
Aged Yak Warrior
841 Posts |
Posted - 2007-10-01 : 08:34:03
|
| Outch |
 |
|
|
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.bFROM CLEFT OUTER JOIN D ON C.a = D.aWHERE D.a IS NULL |
 |
|
|
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.) |
 |
|
|
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.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
|