If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Exception Join vs Not Exists

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-13-11, 06:58
juliusquitain juliusquitain is offline
Registered User
 
Join Date: Sep 2011
Posts: 1
Exception Join vs Not Exists

Hi everyone, i would like to ask what is the difference between Exception Join vs Not Exists. What are the advantages and disadvantages. Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 09-13-11, 07:35
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
What do you mean by "Exception Join"?
Reply With Quote
  #3 (permalink)  
Old 09-13-11, 15:54
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by juliusquitain View Post
Hi everyone, i would like to ask what is the difference between Exception Join vs Not Exists. What are the advantages and disadvantages.
Actually, there are syntactically three different ways to write "give me all rows satisfying condition A, except for those satisfying condition B":
Code:
SELECT ... FROM tbl_1
WHERE  condition_A
  AND NOT EXISTS (correlated subquery on tbl_2
                            WHERE condition_B)
Code:
SELECT ... FROM tbl_1
WHERE  condition_A
EXCEPT
SELECT ... FROM tbl_2
WHERE  condition_B
Code:
SELECT ... FROM tbl_1 LEFT OUTER JOIN tbl_2 ON condition_B
WHERE condition_A AND tbl_2.pk IS NULL
Sorry for the pseudo-code; a "real" example would help here, of course, to see how you may rewrite from one form into the other (or in which cases this would be impossible).
Which of the three is "best"?
Well, of course, "it depends"!
First of all, not all three formulations make sense for a particular real-world question; and more precisely, one of the three is often the "most natural" and hence the most readable formulation.
So go for that one! Readability of SQL means maintainability, which means: lower maintenance cost in the longer run.

What about runtime performance? In the ideal world, the DB2 optimizer will turn your three formulations into exactly the same "byte code". Of course, there will be some situations (hopefully less than 5% of the cases) where the most readable SQL turns into the least performant implementation. In those cases, it may be useful (or even mandatory) to rewrite the query into one of the other formulations. Use EXPLAIN to find out! And always keep in mind that this should be the exception, not the rule!
The DB2 optimizer is often better than you expect
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Tags
exception join, not exists

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On