Here is a simple test case to demonstrate How the optimizer re-arranges the predicates (in case of cpu costing).
rajesh@ORA11G> create table t
2 as
3 select to_char( mod(rownum,20)) v1,
4 rownum n1,
5 mod(rownum,20) n2
6 from all_objects
7 where rownum <=3000;
Table created.
rajesh@ORA11G>
rajesh@ORA11G> exec dbms_stats.gather_table_stats(user,'T');
PL/SQL procedure successfully completed.
rajesh@ORA11G>
rajesh@ORA11G> explain plan set statement_id ='PLAN01'
2 for
3 select /*+ no_cpu_costing */ *
4 from t
5 where v1 = 1
6 and n2 =1
7 and n1=981;
Explained.
rajesh@ORA11G>
rajesh@ORA11G> explain plan set statement_id ='PLAN02'
2 for
3 select *
4 from t
5 where v1 = 1
6 and n2 =1
7 and n1=981;
Explained.
rajesh@ORA11G> select statement_id,id,operation,options,
2 object_name,filter_predicates
3 from plan_table
4 order by statement_id,id;
STATEMENT_ ID OPERATION OPTIONS OBJECT_NAM FILTER_PREDICATES
---------- ---------- -------------------- ---------- ---------- ---------------------------------------------
PLAN01 0 SELECT STATEMENT
PLAN01 1 TABLE ACCESS FULL T TO_NUMBER("V1")=1 AND "N2"=1 AND "N1"=981
PLAN02 0 SELECT STATEMENT
PLAN02 1 TABLE ACCESS FULL T "N1"=981 AND "N2"=1 AND TO_NUMBER("V1")=1
4 rows selected.
rajesh@ORA11G>
With no-cpu costing, the optimizer evaluated the predicates as such. from a table of 3000 rows
a) perform 3000 implicit conversion to find 150 rows (having TO_NUMBER("V1")=1)
b) then from that 150 rows apply filter n2=1 to return 150 rows.
c) then finally "N1"=981 to just one row.
But with CPU Costing, the optimizer re-arranges the predicates like this.
a) apply n1=981 and just get one row
b) then do n2=1 to return just one row
c) then perform just one implicit conversion to return that row by TO_NUMBER("V1")=1
CPU Costing is available since Oracle 9i, so feel free to write the queries with required predicates in any order, let the optimizer find its best way to re-arrange them.
Thanks to optimizer for continuous evolution.
To know more about CPU costing, refer to
Book : Cost Based optimizer Fundamentals by Jonathan lewis
Chapter#2 : Tablescans
Topic: The power of CPU Costing
Page no# : 22