order of tables/conditions [message #60442] |
Sun, 08 February 2004 20:24 |
ora
Messages: 47 Registered: June 2002
|
Member |
|
|
Hi,
I have one more stuff for u. I want to know that is the order of tables in a select query and order of conditions in a select query really matters, if the optimizer mode is COST.
Thanx
|
|
|
|
Re: order of tables/conditions [message #60458 is a reply to message #60450] |
Mon, 09 February 2004 05:11 |
Thiru
Messages: 1089 Registered: May 2002
|
Senior Member |
|
|
Quote " In the old days it was recommended to put the "driving table" last in the list."
in the Rule Based Optimizer , Yes. Not in CBO. In CBO, all things being equal, the driving table was the first table in the FROM clause, and not the last .
Even now,all things being equal, even in CBO, the table that appears FIRST in the FROM clause, is choosen to be the driving table ! , but the keyword is 'all things being equal' which is rare.
The order of tables in the FROM clause is irrelavant in almost all of the cases in CBO(except overridden by ORDERED or LEADING hint and the 'all things being equal' stuff) and to some extent RBO also. RBO ,has some intelligence too and would try to decide the join order based on indexes etc, but when everything is tied, it chooses the driving order starting from RIGHT to LEFT.
You'll see this behaviour demonstrated in one of my earlier post. Driving table
However,the order of joins is relevant and important in both CBO and RBO .
-Thiru
[Updated on: Fri, 18 February 2005 23:32] Report message to a moderator
|
|
|
|
Re: order of tables/conditions [message #60472 is a reply to message #60471] |
Tue, 10 February 2004 02:16 |
Thiru
Messages: 1089 Registered: May 2002
|
Senior Member |
|
|
Maaher,
CBO is not influenced by the order of tables in the FROM clause in almost all of the cases ,
but as you saw in my demo , when the participating tables are of the same size with same number of rows with equal indexes ,then they do get influenced by the order of tables.
But as I said earlier, this is very rare ! (All things being equal is pretty rare,dont you think ?) .
I showed that possibility also in the demo and also to show that in CBO,they get processed from LEFT to RIGHT in those rare cases).
Also the CBO can get influenced by the order of tables on specifying hints like ORDERED or LEADING .
I can prove this again and again,no problem.
-- Lets create two identical tables emp and emp2
thiru@9.2.0:SQL>create table emp2 as select * from emp;
Table created.
thiru@9.2.0:SQL>analyze table emp compute statistics;
Table analyzed.
thiru@9.2.0:SQL>analyze table emp2 compute statistics;
Table analyzed.
-- They have same number of rows and size. Neither of them have any indexes
thiru@9.2.0:SQL>select table_name,blocks,num_rows from user_Tables where table_name in ('EMP','EMP2');
TABLE_NAME BLOCKS NUM_ROWS
------------------------------ ---------- ----------
EMP 4 8
EMP2 4 8
-- Lets start with EMP followed by EMP2 .
thiru@9.2.0:SQL>select count(*) from <B>emp,emp2</B> where emp.empno=emp2.empno;
COUNT(*)
----------
8
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=5 Card=1 Bytes=6)
1 0 SORT (AGGREGATE)
2 1 HASH JOIN (Cost=5 Card=8 Bytes=48)
3 2 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=8 Bytes=24)
4 2 TABLE ACCESS (FULL) OF 'EMP2' (Cost=2 Card=8 Bytes=24)
-- as you see above, EMP is processed first here. Join order is EMP to EMP2 . This is expected.
-- Now,lets change the order. this time EMP2 followed by EMP
thiru@9.2.0:SQL>select count(*) from <B>emp2,emp</B> where emp.empno=emp2.empno;
COUNT(*)
----------
8
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=5 Card=1 Bytes=6)
1 0 SORT (AGGREGATE)
2 1 HASH JOIN (Cost=5 Card=8 Bytes=48)
3 2 TABLE ACCESS (FULL) OF 'EMP2' (Cost=2 Card=8 Bytes=24)
4 2 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=8 Bytes=24)
-- AS you see above, Emp2 is processed first . The join order is Emp2 to Emp. The CBO did get influuenced by the order specified in the FROM cluase.
This is one of those special conditions(all things being equal) where this is possible. I am sure Tom was simplifying things.
-- But when I disturb this 'all things being equal' condition,
then the order of tables becomes irrelevant .We will get the same join order over and over again.
-- Let me create an index on EMP now
thiru@9.2.0:SQL>create index emp_idx on emp(empno);
Index created.
thiru@9.2.0:SQL>analyze table emp compute statistics;
Table analyzed.
thiru@9.2.0:SQL>select count(*) from emp,emp2 where emp.empno=emp2.empno;
COUNT(*)
----------
8
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=6)
1 0 SORT (AGGREGATE)
2 1 NESTED LOOPS (Cost=2 Card=8 Bytes=48)
3 2 TABLE ACCESS (FULL) OF 'EMP2' (Cost=2 Card=8 Bytes=24)
4 2 INDEX (RANGE SCAN) OF 'EMP_IDX' (NON-UNIQUE)
thiru@9.2.0:SQL>
thiru@9.2.0:SQL>select count(*) from emp2,emp where emp.empno=emp2.empno;
COUNT(*)
----------
8
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=6)
1 0 SORT (AGGREGATE)
2 1 NESTED LOOPS (Cost=2 Card=8 Bytes=48)
3 2 TABLE ACCESS (FULL) OF 'EMP2' (Cost=2 Card=8 Bytes=24)
4 2 INDEX (RANGE SCAN) OF 'EMP_IDX' (NON-UNIQUE)
-- As you see above, when I have an index on one of the tables,
I get the same plan(ie a Nested loops join from EMP2 to EMP) irrespective of the order of the tables in the FROM clause.
This is what you'll observe in most of the cases as the participating tables and indexes will have something or the other different!
thiru@9.2.0:SQL>
|
|
|
Re: order of tables/conditions [message #60473 is a reply to message #60472] |
Tue, 10 February 2004 04:01 |
Thiru
Messages: 1089 Registered: May 2002
|
Senior Member |
|
|
Also note that there are two different things I am talking about
i) order of the tables in the FROM clause , which is IRRELEVANT in almost all the cases.
ii) the join order of tables , which is RELEVANT & important in almost all of the cases. There are cases where the join order is irrelevant eg) when doing a sort merge(involving FTS) between two or more tables. In this case,all the participating tables have to be scanned in whole,before joining to the other tables. The other case is the one ,which I was talking about earlier (ie when the tables are exactly same and hence the cost is the same).
Even when 'all things are equal' and the CBO does get influenced by the order of the tables in the FROM clause, the net response is likely to be same becos the tables/indexes are equal anyway!(ie join order becomes irrelevant here).
I can say 'for all practical purposes the order of the tables in the FROM clause in CBO is irrelevant' and get away with it, but I do know that there are some cases where the CBO's join order gets influenced by the order of tables in the FROM clause.
-Thiru
|
|
|
|
Re: order of tables/conditions [message #60497 is a reply to message #60495] |
Wed, 11 February 2004 01:45 |
Thiru
Messages: 1089 Registered: May 2002
|
Senior Member |
|
|
Ofcourse no , I mentioned this also in my follow up post !
Two points I was making
i) There are cases where the join order is influenced by the order in FROM clause . 'All things being equal' and hints are those cases . Do hints work all the time ? Not necessarily. But do they have the potential to change the plan ? Sure,Yes.
So it is incorrect to say CBO is not at all influenced by the order of tables in the FROM clause in all cases,thats all.
ii) In older days ( and in those special cases now) , CBO processed the tables from LEFT to RIGHT for its driving order. The FIRST table was the driving table in CBO and not the LAST , which was the case with RBO.
-Thiru
|
|
|