MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Does Sequence matter for INNER JOIN or LEFT OUTER JOIN

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
seekaysk
Beginner


Joined: 31 Aug 2007
Posts: 49
Topics: 15

PostPosted: Wed Oct 07, 2009 1:09 pm    Post subject: Does Sequence matter for INNER JOIN or LEFT OUTER JOIN Reply with quote

Hi,

I am running an explain onto a query where I am puzzled to see that the number of columns that match for a table changes if I change the sequence of INNER JOIN and LEFT OUTER JOIN. i.e., instead of first INNER JOIN and then LEFT OUTER JOIN, if I put first LEFT OUTER JOIN and then INNER JOIN and the number of columns that matches, changes. Could somebody help decipher this puzzle for me.

if you have still not understood, below are some gory details.

details: Query is extremely big so showing a sample type for reference.

Code:
Table1 (GTT)  - 3 columns: A, B, C      Index-none (It's a GTT)
Table2        - 4 columns: A, B, P, Q   Index-A,B
Table3 (GTT)  - 1 columnS: A            Index-none (It's a GTT)
Table4 (GTT)  - 4 columns: A, B, C, E   Index-none (It's a GTT)

GTT - Global Temporary Table

You would have noted that column A is common in all 4 tables and similarly other columns as shown.

Query:

Code:
Select
    table1.A
   ,table1.B
   ,table1.C
   ,table2.P
   ,table2.Q
   ,COALESCE(table4.E,0)
FROM TABLE1
INNER JOIN TABLE2
ON TABLE1.A = TABLE2.A
AND TABLE1.B = TABLE2.B

INNER JOIN TABLE3
ON TABLE1.A = TABLE3.A

LEFT OUTER JOIN TABLE4
ON TABLE1.A = TABLE4.A
AND TABLE1.B = TABLE4.B
AND TABLE1.C = TABLE4.C

While running explain, I am getting 1 column match on Table2.

But If I change the query and bring the INNER JOIN TABLE2 at the end of the query like below, the column match is fine with 2 columns. Can somebody explain why ?

Code:
Select
    table1.A
   ,table1.B
   ,table1.C
   ,table2.P
   ,table2.Q
   ,COALESCE(table4.E,0)
FROM TABLE1
INNER JOIN TABLE3
ON TABLE1.A = TABLE3.A

LEFT OUTER JOIN TABLE4
ON TABLE1.A = TABLE4.A
AND TABLE1.B = TABLE4.B
AND TABLE1.C = TABLE4.C

INNER JOIN TABLE2
ON TABLE1.A = TABLE2.A
AND TABLE1.B = TABLE2.B

_________________
Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group