CBO與RBO

来源:互联网 发布:网络恐怖主义的兴起 编辑:程序博客网 时间:2024/06/05 17:55

CBO是统计数据驱动的cost-based SQL optimizer 基于成本的优化器
RBO是指定了20来个访问规则(优先级不同)。rule-based SQL optimizer

所以,RBO容易生成错误的或者不佳的执行计划。


给你个例子,自己按照这个思路测试下,就理解了。

1:建两个表结构一样的表
     
create table t1(a int ,b char(400));
create table t2(b int, b char2(400));

2:插入测试数据
insert into t1 (a,b)  select a,b from  (select level  a ,'x' b  from dual connect by level <50001) order by a;
insert into t2 (a,b) select a,b from t1 order by dbms_random.value ;

3:建立索引
create index t1_inx on t1(a);
create index t2_inx on t2(a);

4:收集两张表的统计数据
用dbms_stats.
或者analyze table  t1|t2 compute statistics
                                     all index statistics
                                    all  column  statistics
这里收集统计数据的语句记不住了,自己查下吧。
  最终是把表的统计数据收集了,包括索引、直方图、列的统计信息。

5:执行SQL,比较执行计划,

select *
from t2
where a>49900;

select *
from t1
where a>49900;

select  /*+rule*/ *
from t2
where a>49900;
select /*+rule*/ *
from t1
where a>49900;

分别比较这4个SQL的执行计划,同时注意查看查询时间.

最简单的方式就是在SQLPLUS下
set timging on  
用autotrace工具。

结果就比较出来了。

大致就是这样的。

原创粉丝点击