索引的空間不能像堆表一樣重用
例如
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)