SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Caution with EXCEPT


Caution with EXCEPT

Author
Message
zintp
zintp
SSC Veteran
SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)

Group: General Forum Members
Points: 285 Visits: 378
Comments posted to this topic are about the item Caution with EXCEPT
mohd.nizamuddin
mohd.nizamuddin
SSC-Addicted
SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)SSC-Addicted (456 reputation)

Group: General Forum Members
Points: 456 Visits: 198
Very nice and informative article...
This is what we do in real life as well (Use of SELECT *). Even though we hear from everybody that to avoid SELECT * always.
So, if we make practice of writing SELECT col1, col2, col3,.... then we would not get that problem.
Enjoy.
Tobar
Tobar
SSChasing Mays
SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)

Group: General Forum Members
Points: 653 Visits: 758
Another issue with select * is that if you have identity keys and the tables are independently generated (as in deve and staging) and an identical data entry has separate "identities" on the different boxes select * will always return all the rows.

Instead of the Union I usually just

select * from A
except
select * from B

select * from B
except
select * from A

then you can it is easier to know where the "differences" originate.

<><
Livin' down on the cube farm. Left, left, then a right.
Tobar
Tobar
SSChasing Mays
SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)

Group: General Forum Members
Points: 653 Visits: 758
mohd.nizamuddin (1/17/2010)
Very nice and informative article...
This is what we do in real life as well (Use of SELECT *). Even though we hear from everybody that to avoid SELECT * always.
So, if we make practice of writing SELECT col1, col2, col3,.... then we would not get that problem.
Enjoy.


Sorry for 2 responses. More thoughts occurred as the coffee kicked in.

I "always" ;-) start to worry when someone used the phrase "always", there are always reasons why always is never appropriate. (Same goes with "never"). Sometimes it is just the thing to do.

When needing to "select col1, col2, etc." I use the Management Studio tactic of "Script table as->Select into->new query window" and then just hack out any columns I do not want. It "Always" works for me.

<><
Livin' down on the cube farm. Left, left, then a right.
Stephen Hirsch
Stephen Hirsch
Old Hand
Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)Old Hand (324 reputation)

Group: General Forum Members
Points: 324 Visits: 161
Here's a better way:

SELECT 'IN A NOT B' location, a, b, c, d, ...
FROM tablea
EXCEPT
SELECT 'IN A NOT B' location, a, b, c, d, ...
FROM tableb
UNION ALL
SELECT 'IN B NOT A' location, a, b, c, d, ...
FROM tableb
EXCEPT
SELECT 'IN B NOT A' location, a, b, c, d, ...
FROM tablea

No parentheses this way.
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21607 Visits: 19706
Stephen Hirsch (1/18/2010)
Here's a better way:

SELECT 'IN A NOT B' location, a, b, c, d, ...
FROM tablea
EXCEPT
SELECT 'IN A NOT B' location, a, b, c, d, ...
FROM tableb
UNION ALL
SELECT 'IN B NOT A' location, a, b, c, d, ...
FROM tableb
EXCEPT
SELECT 'IN B NOT A' location, a, b, c, d, ...
FROM tablea

No parentheses this way.


Wait a minute! If ever there was a case for parentheses, it's right here - what was the intent of the programmer? Parentheses make the intent self-documenting:

SELECT 'IN A NOT B' location, a, b, c, d, ...
FROM tablea
EXCEPT
(SELECT 'IN A NOT B' location, a, b, c, d, ...
FROM tableb
UNION ALL
SELECT 'IN B NOT A' location, a, b, c, d, ...
FROM tableb
EXCEPT
SELECT 'IN B NOT A' location, a, b, c, d, ...
FROM tablea)

Cheers

ChrisM

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Tobar
Tobar
SSChasing Mays
SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)SSChasing Mays (653 reputation)

Group: General Forum Members
Points: 653 Visits: 758
The programmer "intended" it to work the way the programmer thought they were programming it to work. :-D [this said in the universal sense]

<><
Livin' down on the cube farm. Left, left, then a right.
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21607 Visits: 19706
Tobar (1/18/2010)
The programmer "intended" it to work the way the programmer thought they were programming it to work. :-D [this said in the universal sense]


LOL you used to work there too!

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
bugmenot-573553
bugmenot-573553
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 5439
We have seen poor performance with EXCEPT on large data sets.
Sam Jumper
Sam Jumper
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 78
Thanks for the article, I've never used EXCEPT because I didn't realize it existed.

On the point of poor performance with large datasets, how can we expect anything else? Probably too many columns to put in a covering index. SQL has to compare every column to every column, it's going to either table scan or bookmark lookup every row. Give your SQL Server a break! I suppose when I think about it, you could include a WHERE statement to break your large datasets in to smaller ones if appropriate to what you are looking for.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search