可传递性:是CBO在查询转换中所做的第一件事情,指的是CBO 对原目标SQL做简单的等价改写。
SQL> create table test1(t1 number,t2 varchar2(10));
Table created.
SQL> create table test2(t1 number,t2 varchar2(10));
Table created.
SQL> create index idx_test2 on test2(t1); test2表上的t1建索引
Index created.
SQL> insert into test1 values(1,'a');
1 row created.
SQL> insert into test1 values(2,'b');
1 row created.
SQL> insert into test1 values(3,'c');
1 row created.
SQL> insert into test2 values(3,'d');
1 row created.
SQL> insert into test2 values(4,'e');
1 row created.
SQL> insert into test2 values(5,'f');
1 row created.
SQL> select * from test1;
T1 T2
---------- ----------
1 a
2 b
3 c
SQL> select * from test2;
T1 T2
---------- ----------
3 d
4 e
5 f
SQL> set autotrace traceonly
SQL> select test1.t1,test2.t1 from test1,test2
2 where test1.t1 = test2.t1 and test1.t1=3;
Execution Plan
----------------------------------------------------------
Plan hash value: 4216916817
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | 3 (0)| 00:00:01 |
| 1 | MERGE JOIN CARTESIAN| | 1 | 26 | 3 (0)| 00:00:01 |
|* 2 | TABLE ACCESS FULL | TEST1 | 1 | 13 | 3 (0)| 00:00:01 |
| 3 | BUFFER SORT | | 1 | 13 | 0 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | IDX_TEST2 | 1 | 13 | 0 (0)| 00:00:01 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("TEST1"."T1"=3)
4 - access("TEST2"."T1"=3)
oracle还是走了 idx_test2 的索引范围扫描 但where语句中并没有test2.t1=3
所以目标sql 语句等价转换成了 select test1.t1,test2.t1 from test1,test2
where test1.t1 = test2.t1 and test1.t1=3 and test2.t1=3
CBO这么做的目的是增加了得到高效率执行计划的可能性。
本文通过实例演示了CBO(Cost-Based Optimizer)如何通过等价转换原目标SQL语句来提升查询效率。具体展示了通过增加隐式过滤条件提高执行计划质量的过程。
924

被折叠的 条评论
为什么被折叠?



