如何降低索引的clustering_factor

分类: Oracle

 

环境:
OS:Red Hat Linux As 5
DB:10.2.0.4
 
我们知道判断一个索引的好坏可以通过该索引的clustering_factor高低来衡量,clustering_factor越低,索引的使用效果就越好,那怎么样才能降低索引的clustering_factor呢,通常使用的方法是让表的索引字段值按顺序存储,下面通过一个例子说明.
 
1.创建表并构造无序的数据
create table scott.tb_index_test
(
  id number not null,
  name varchar2(30)
);
 
create index scott.idx_tb_index_test  on scott.tb_index_test(id);
 
declare
  l_random_value number;
begin
  for i in  1 .. 100000 loop
    l_random_value := round(dbms_random.value(1,100000));
    insert into scott.tb_index_test(id) values (l_random_value);
    commit;
  end loop;
end;
 
2.分析表
 
begin
  dbms_stats.gather_table_stats(ownname => 'SCOTT',
                                tabname => 'TB_INDEX_TEST',
                                cascade => true);
end;
 
3.查看索引当前的clustering_factor
 
SQL> select ui.clustering_factor, ui.num_rows, ui.index_type, ui.distinct_keys
  from dba_indexes ui where ui.table_name = 'TB_INDEX_TEST';
CLUSTERING_FACTOR   NUM_ROWS INDEX_TYPE                  DISTINCT_KEYS
----------------- ---------- --------------------------- -------------
            99742     100000 NORMAL                             100000
 
4.将表中的数据按照索引字段存储
 
create table scott.tmp as select * from scott.tb_index_test;
truncate table scott.tb_index_test;
insert into scott.tb_index_test select * from scott.tmp order by id;
 
5.再次分析表
 
begin
  dbms_stats.gather_table_stats(ownname => 'SCOTT',
                                tabname => 'TB_INDEX_TEST',
                                cascade => true);
end;
 
6.这个时候索引的clustering_factor明显降低了
 
SQL> select ui.clustering_factor, ui.num_rows, ui.index_type, ui.distinct_keys
  from dba_indexes ui where ui.table_name = 'TB_INDEX_TEST';
CLUSTERING_FACTOR   NUM_ROWS INDEX_TYPE                  DISTINCT_KEYS
----------------- ---------- --------------------------- -------------
              372     100000 NORMAL                             100000
 
-- The End --

转载于:https://www.cnblogs.com/weixun/archive/2013/03/27/2985567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值