索引空间使用

来源:互联网 发布:淘宝客服中心组织架构 编辑:程序博客网 时间:2024/05/29 18:14

索引的空间不能像堆表一样重用

例如

create table t (x int,y int ,constraint con_t_pk primary key(x)  ) ;
analyze index con_t_pk validate structure;
select t.height,t.lf_blks,t.br_blks,t.btree_space from index_stats t ;

insert  into t values(1,1);

insert into t  values(2,2);

analyze index con_t_pk validate structure;
select t.height,t.lf_blks,t.br_blks,t.btree_space from index_stats t ;----------获取空间一

begin
for i in 3..99998 loop
    insert into t values(i,i);
end loop;
commit;
end;

analyze index con_t_pk validate structure;
select t.height,t.lf_blks,t.br_blks,t.btree_space from index_stats t ;----------获取空间二

delete from t;

analyze index con_t_pk validate structure;
select t.height,t.lf_blks,t.br_blks,t.btree_space from index_stats t ;----------获取空间三

begin
for i in 3..99998 loop
    insert into t values(i,i);
end loop;
commit;
end;

analyze index con_t_pk validate structure;
select t.height,t.lf_blks,t.br_blks,t.btree_space from index_stats t ;----------获取空间四

获取空间一~-获取空间四都是一样空间大小

如果不强制释放空间 索引的空间非常难重用(例如放索引键值为1 的空间删除后放2)

alter index con_t_pk coalesce ;
alter index con_t_pk rebuild ;

analyze index con_t_pk validate structure;
select t.height,t.lf_blks,t.br_blks,t.btree_space from index_stats t ;

听说回收空间可以使用这个dbms_space.free_blocks

declare
 free_blks number:=0;
begin
dbms_space.free_blocks(segment_owner => user,segment_name => 'T',
                       segment_type => 'Table',freelist_group_id => 0,free_blks => free_blks);
dbms_output.put_line(free_blks);
                       
end

但是在11G上失败,因为freelist_group_id已经不是用了。


监控索引使用

alter index con_t_pk monitoring usage;
select * from v$object_usage;
alter index con_t_pk nomonitoring usage;


关于创建多字段的索引中的字段顺序:

1、优先将出现在where谓语后面出现频率高的字段靠前。

2、优先将字段值出现重复率高的字段放在前面(有助于压缩索引,从而减少IO)




0 0
原创粉丝点击