oracle CBO 的可传递性

本文通过实例演示了CBO(Cost-Based Optimizer)如何通过等价转换原目标SQL语句来提升查询效率。具体展示了通过增加隐式过滤条件提高执行计划质量的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可传递性:是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这么做的目的是增加了得到高效率执行计划的可能性。
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值