Home>Question Details



Venkat -- Thanks for the question regarding "SQL Query where clause order", version 10g

Submitted on 6-Aug-2009 1:16 Central time zone
Last updated 6-Aug-2009 9:54

You Asked

Hi Tom,

I have a SQL query like below:

desc dept
deptno varchar2(10)
dname varchar2(10)

10 records in dept table

desc emp
empno varchar2(20)
deptno varchar2(10)
proj_no varchar2(20)
10,000 records in emp table

desc sal
empno varchar2(20)
salary number
10,000 records in sal table


desc proj
proj_no varchar2(20) (PK)
proj_name varchar2(100)
100,000 records in proj table


#1
select d.*
from dept d, emp e, sal s, proj p
where d.deptno=e.deptno
and e.empno=s.empno
and e.proj_no=p.proj_no


We would like to know if we change the order of join clauses in the where clause.

For example:

#2
select d.*
from dept d, emp e, sal s, proj p
where e.deptno=d.deptno
and e.proj_no=p.proj_no
and s.empno=e.empno

Is Oracle internally handles #1 and #2 in the same way or different way?

Thanks in advance.
Venkat

and we said...

the cost based optimizer is rather insensitive to the ordering of where clauses, it assigns costs in order to determine what to do first and how to do things.

the old unsupported rule based optimizer was sensitive to the ordering, but not so the CBO.

Will I guarantee the SAME EXACT PLAN based on predicates in different order with the CBO? No - ties (operations that are computed to take the same/similar amount of work) could result in different plans given different predicates.

But in short, as a SQL coder - you should put the predicate in an order that makes sense to you, makes the query more readable.
Reviews    
3 stars   August 6, 2009 - 10am Central time zone
Reviewer: Venkat Waran 
Thanks tom.





All information and materials provided here are provided "as-is"; Oracle disclaims all express and implied warranties, including, the implied warranties of merchantability or fitness for a particular use. Oracle shall not be liable for any damages, including, direct, indirect, incidental, special or consequential damages for loss of profits, revenue, data or data use, incurred by you or any third party in connection with the use of this information or these materials.

About Oracle | Legal Notices and Terms of Use | Privacy Statement