Tom Kyte
Thanks for the question, Tony.
Asked: November 02, 2003 - 2:26 pm UTC
Last updated: February 20, 2008 - 8:18 am UTC
Version: 9.2
Viewed 10K+ times! This question is
You Asked
Hi Tom,
While looking into the documents of 9i, I found that Oracle now supports ANSI joins. Since Oracle optimizer uses where clause to draw the execution plan (cost etc.), how it does with new natural joins? Is it just another layer on the top of existing Oracle's way of joins? I was wondering that if this not the case, then did Oracle change its Kernel in 9i to accommodate this. I am curious to know more details about this. Any help in this regard will be highly appreciated.
Regards,
and Tom said...
natural joins -- a bug waiting to happen -- are done exactly the same way as they were when you specified the join conditions yourself. There is no difference there, just the syntax is different. We look up in both tables for all of the common column names and build the where clause for you
scott@ORA920> select ename, dname from emp natural join dept;
ENAME DNAME
---------- --------------
SMITH RESEARCH
ALLEN SALES
WARD SALES
JONES RESEARCH
MARTIN SALES
BLAKE SALES
CLARK ACCOUNTING
SCOTT RESEARCH
KING ACCOUNTING
TURNER SALES
ADAMS RESEARCH
JAMES SALES
FORD RESEARCH
MILLER ACCOUNTING
14 rows selected.
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=6 Card=14 Bytes=252)
1 0 HASH JOIN (Cost=6 Card=14 Bytes=252)
2 1 TABLE ACCESS (FULL) OF 'DEPT' (Cost=3 Card=4 Bytes=44)
3 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=3 Card=14 Bytes=98)
scott@ORA920> select ename, dname from emp, dept
2 where emp.deptno = dept.deptno;
ENAME DNAME
---------- --------------
SMITH RESEARCH
ALLEN SALES
WARD SALES
JONES RESEARCH
MARTIN SALES
BLAKE SALES
CLARK ACCOUNTING
SCOTT RESEARCH
KING ACCOUNTING
TURNER SALES
ADAMS RESEARCH
JAMES SALES
FORD RESEARCH
MILLER ACCOUNTING
14 rows selected.
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=6 Card=14 Bytes=252)
1 0 HASH JOIN (Cost=6 Card=14 Bytes=252)
2 1 TABLE ACCESS (FULL) OF 'DEPT' (Cost=3 Card=4 Bytes=44)
3 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=3 Card=14 Bytes=98)
i strongly, vehemently strongly, discourage the use of this "feature" called natural joins
to join tables by their colum names -- how patently "just a bad idea". We've bugs logged saying "hey, this natural join returns the wrong answer" -- but wait -- they were comparing it to what they THOUGHT should have been the query, not with what the query really way. Consider a model like this:
create table emp ( empno, deptno, ename );
create table dept ( deptno, dname, empno /* person responsible for this dept */ );
now, generate a list of enames and the dname they work in using a natural join, whoops.
Never never never use this "feature"
Rating
(8 ratings)
Is this answer out of date? If it is, please let us know via a Comment