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 

Order of execution of Query

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


Joined: 27 Aug 2007
Posts: 102
Topics: 42
Location: Chennai

PostPosted: Thu Sep 18, 2008 6:30 am    Post subject: Order of execution of Query Reply with quote

Suppose I have a query as follows

Code:
SELECT
   A.FIELD1
   B.FIELD2
   C.FIELD3
   D.FIELD4
FROM   TABLE1   AS   A,
   TABLE2   AS   B,
   TABLE3   AS   C
LEFT OUTER JOIN               --left outer join between table3 and table4
   TABLE4   AS   D
ON
   C.LJOINFIELD   =   D.LJOINFIELD
WHERE   A.FIELD1   >   0      --some filter conditions on table1
   C.FIELD2   =   'ABC'      --some filter conditions on table3
   B.IJOINFIELD   =   D.IJOINFIELD   --inner join between table2 and table4
   A.IJOINFIELD   =   C.IJOINFIELD   --inner join between table1 and table3


In the above query I would like to know how the query would proceed.. which join happends first, what filtering happens next...

can someone please help..
_________________
Thanks
Back to top
View user's profile Send private message Yahoo Messenger
jim haire
Beginner


Joined: 30 Dec 2002
Posts: 140
Topics: 40

PostPosted: Thu Sep 18, 2008 8:12 am    Post subject: Reply with quote

You could run an Explain on the query which would tell you what was performed first. There is also a free tool called Visual Explain from IBM which you can download which would show the order.

We can't tell from the query which order the statements would be performed in. The optimizer makes choices depending on the number of rows in each table, filter factors, and many other criteria.
Back to top
View user's profile Send private message
arungr
Beginner


Joined: 12 Feb 2005
Posts: 16
Topics: 4

PostPosted: Mon Jan 12, 2009 10:14 pm    Post subject: Outer Join - execution order Reply with quote

I have a similar qustion. All I wanted to know which will get executed first in the above query. "ON" or "WHERE".
Back to top
View user's profile Send private message
arungr
Beginner


Joined: 12 Feb 2005
Posts: 16
Topics: 4

PostPosted: Mon Jan 12, 2009 10:23 pm    Post subject: Reply with quote

I had asked the above question because I do not have access to Plan_Table and hence I wont be able to run "Explain". Any inputs are appreciated.
Back to top
View user's profile Send private message
CZerfas
Intermediate


Joined: 31 Jan 2003
Posts: 211
Topics: 8

PostPosted: Tue Jan 13, 2009 4:31 am    Post subject: Reply with quote

Regarding your access to a PLAN_TABLE:
You need access to a PLAN_TABLE of another userid (table qualifier different from your User-ID), if you want to explain the access path of an existing DB2 package. If you want to know the access path of a single SQL statement, you can create a PLAN-TABLE with your own User-ID as qualifier and then issue an EXPLAIN SQL statement through any interactive SQL facility (SPUFI, BMC catalog manager, etc.). The create statement for the latest version of PLAN_TABLE can be found in the DB2 manuals or derived by you from an existing PLAN_TABLE.

Regarding to the sequence of ON and WHERE: As far as I have understood it, ON is issued first, but only when the "outer-joined table" is accessed by the optimizer during the preparation process of the statement. So some predicates of the WHERE clause, that have nothing to do with your "outer-joined table", can already be evaluated.

regards
Christian
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