dbTalk Databases Forums  

"A left outer join B on B.ID=A.ID and <conditions>" versus "A left outer join B on B.ID=A.ID where <conditions>"

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss "A left outer join B on B.ID=A.ID and <conditions>" versus "A left outer join B on B.ID=A.ID where <conditions>" in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Toralf Förster
 
Posts: n/a

Default "A left outer join B on B.ID=A.ID and <conditions>" versus "A left outer join B on B.ID=A.ID where <conditions>" - 11-03-2010 , 03:31 AM






I'm faced here at work (IBM) with the fact that the runtime of a SQL query
can be reduced from 7 minutes to 2-3 seconds if <conditions> is moved from
the join to the where clause. (b/c of the left outer join "<conditions>" was
slightly changed into "B.ID is null or (<conditions>)"

Now I'm wondering whether in general it is better to use the where clause or
what else makes the difference (or why db2 can't optimize it itself) ?

--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3

Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: "A left outer join B on B.ID=A.ID and <conditions>" versus "Aleft outer join B on B.ID=A.ID where <conditions>" - 11-03-2010 , 05:31 AM






You compared...

query (1):
A left outer join B
on B.ID = A.ID
and <conditions>

with

query (2):
A left outer join B
on B.ID = A.ID
where B.ID is null
or (<conditions>)

If <conditions> contain a condition for A,
those two queries would not be equivalent.

Because, wheather a row of A does or doesn't satisfy the condition for
A,
all rows of A would be included in the result of query (1).
This nature of outer join is often forgot.

But a row of A that didn't satisfy the condition for A
and there was a row of B matching with the row,
the row of A would not be included in the result of query (2),

In conclusion,
the result set of (2) would be same or smaller than the result set of
(1).

Reply With Quote
  #3  
Old   
Tonkuma
 
Posts: n/a

Default Re: "A left outer join B on B.ID=A.ID and <conditions>" versus "Aleft outer join B on B.ID=A.ID where <conditions>" - 11-03-2010 , 06:01 AM



On Nov 3, 8:31*pm, Tonkuma <tonk... (AT) fiberbit (DOT) net> wrote:
Quote:
You compared...

query (1):
A left outer join B
* on *B.ID = A.ID
* and <conditions

with

query (2):
A left outer join B
* on *B.ID = A.ID
where B.ID is null
* or *(<conditions>)

Correction:

Those two queries would not be equivalent.
The result set of (b) would be same or smaller than the result set of
(a).

Because, wheather a row of A does or doesn't satisfy the
<conditions>,
all rows of A would be included in the result of query (a).
This nature of outer join is often forgot.

But,
a pair of a row of A and a row of B which didn't satisfy the
<conditions>
would not be included in the result of query (b).

Reply With Quote
  #4  
Old   
ChrisC
 
Posts: n/a

Default Re: "A left outer join B on B.ID=A.ID and <conditions>" versus "Aleft outer join B on B.ID=A.ID where <conditions>" - 11-03-2010 , 12:58 PM



On Nov 3, 5:01*am, Tonkuma <tonk... (AT) fiberbit (DOT) net> wrote:
Quote:
But,
*a pair of a row of A and a row of B which didn't satisfy the
conditions
*would not be included in the result of query (b).
In other words, if the conditions in the outer join would have caused
it not to join, then you will get the row from A returned.
In teh second query where conditions are not matched in the where
cause, the row from A is not returned.

Reply With Quote
  #5  
Old   
Toralf Förster
 
Posts: n/a

Default Re: "A left outer join B on B.ID=A.ID and <conditions>" versus "A left outer join B on B.ID=A.ID where <conditions>" - 11-04-2010 , 04:12 AM



Thx for the answers, if I understood it correctly, I've to verify, if it is
ok to move <conditions> for this special purpose.

--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3

Reply With Quote
  #6  
Old   
--CELKO--
 
Posts: n/a

Default Re: "A left outer join B on B.ID=A.ID and <conditions>" versus "Aleft outer join B on B.ID=A.ID where <conditions>" - 11-04-2010 , 09:39 AM



These are not the same query!

Get a copy of SQL FOR SMARTIES where I work out a full example with
all the steps to show the differences. There is usually a copy
sitting around the office at every IBM shop I have visited.

Reply With Quote
  #7  
Old   
Tonkuma
 
Posts: n/a

Default Re: "A left outer join B on B.ID=A.ID and <conditions>" versus "Aleft outer join B on B.ID=A.ID where <conditions>" - 11-04-2010 , 10:09 AM



On Nov 3, 9:01*pm, Tonkuma <tonk... (AT) fiberbit (DOT) net> wrote:
Quote:
On Nov 3, 8:31*pm, Tonkuma <tonk... (AT) fiberbit (DOT) net> wrote:> You compared....

query (1):
A left outer join B
* on *B.ID = A.ID
* and <conditions

with

query (2):
A left outer join B
* on *B.ID = A.ID
where B.ID is null
* or *(<conditions>)

Correction:

Those two queries would not be equivalent.
The result set of (b) would be same or smaller than the result set of
(a).

Here is an example.

Query (1) or (a):
------------------------------ Commands Entered
------------------------------
WITH
a(id , col_x) AS (
VALUES
(1 , 10)
, (2 , 25)
, (3 , 35)
, (4 , 40)
, (5 , 55)
)
,b(id , col_y) AS (
VALUES
(1 , 11)
, (2 , 13)
, (4 , 14)
, (5 , 22)
)
SELECT a.* , b.*
FROM a
LEFT OUTER JOIN
b
ON b.id = a.id
AND (MOD(col_x, 10) = 0
AND
MOD(col_y, 2) = 1
)
;
------------------------------------------------------------------------------

ID COL_X ID COL_Y
----------- ----------- ----------- -----------
1 10 1 11
2 25 - -
3 35 - -
4 40 - -
5 55 - -

5 record(s) selected.

Query (2) or (b)
(using same data):
SELECT a.* , b.*
FROM a
LEFT OUTER JOIN
b
ON b.id = a.id
WHERE b.id IS NULL
OR (MOD(col_x, 10) = 0
AND
MOD(col_y, 2) = 1
)
;
------------------------------------------------------------------------------

ID COL_X ID COL_Y
----------- ----------- ----------- -----------
1 10 1 11
3 35 - -

2 record(s) selected.

Reply With Quote
  #8  
Old   
Tonkuma
 
Posts: n/a

Default Re: "A left outer join B on B.ID=A.ID and <conditions>" versus "Aleft outer join B on B.ID=A.ID where <conditions>" - 11-04-2010 , 11:42 AM



Test data were made as followings:

WITH
a(id , col_x) AS (
VALUES
(1 , 10)
, (2 , 25)
, (3 , 35)
, (4 , 40)
, (5 , 55)
)
,b(id , col_y) AS (
VALUES
(1 , 11)
, (2 , 13)
, (4 , 14)
, (5 , 22)
)
SELECT .....
FROM a
LEFT OUTER JOIN
b
ON b.id = a.id
AND (MOD(col_x, 10) = 0
AND
MOD(col_y, 2) = 1
)
;
------------------------------------------------------------------------------

ID COL_X ID COL_Y cond for A cond for B
----------- ----------- ----------- ----------- ---------- ----------
1 10 1 11 Yes Yes
2 25 - - No Yes
3 35 - - No matched row in B
4 40 - - Yes No
5 55 - - No No

5 record(s) selected.

Reply With Quote
  #9  
Old   
Toralf Förster
 
Posts: n/a

Default Re: "A left outer join B on B.ID=A.ID and <conditions>" versus "A left outer join B on B.ID=A.ID where <conditions>" - 11-05-2010 , 09:13 AM



Tonkuma wrote:

Quote:
Test data were made as followings:
Great example - thx Tonkuma.

Now I think I understood it.

--
MfG/Sincerely
Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.