关于索引何时需要重建 [转]

关于索引何时需要重建。

通过一个视图(index_stats),参考里面的数值来判断是否需要重建

select name,del_lf_rows,lf_rows, round(del_lf_rows/decode(lf_rows,0,1,lf_rows)*100,0)||'%' frag_pct
from index_stats where round(del_lf_rows/decode(lf_rows,0,1,lf_rows)*100,0)>10;

del_lf_rows:索引删除行数
lf_rows:索引总行数

这里 round(del_lf_rows/decode(lf_rows,0,1,lf_rows)*100,0) 求的是删除行数除以总行数的百分比,
我在相关书籍上看到是,比率大于10%需要重建,当然看情况而定,也可以是是20%,但不要超过30%。

当然得到这个结果要首先分析索引:
analyze index index_name validate structure;

因为index_stats每次只存入一个索引的统计信息,所以要是很多索引都检测就不方便,
下面是我写的存储过程,用于批量分析。

1,建立临时表用来存放数据
create table index_rebuid (name varchar2(30),del_lf_rows number(12),lf_rows number(12),frag_pct varchar2(10));
2,授予用户权限
grant select on dba_ind_columns to 用户名;
3,建立存储过程
create or replace procedure reb
is
tx varchar2(1000);
begin
for i in
(
select 'analyze index '||index_owner||'.'||index_name||' validate structure' as sql_text from dba_ind_columns
where index_owner='用户名') loop
tx :=i.sql_text;
execute immediate tx;
insert into index_rebuid (name,del_lf_rows,lf_rows,frag_pct)
select name,del_lf_rows,lf_rows, round(del_lf_rows/decode(lf_rows,0,1,lf_rows)*100,0)||'%' frag_pct
from index_stats where round(del_lf_rows/decode(lf_rows,0,1,lf_rows)*100,0)>10;
end loop;
commit;
end;
4,运行存储
execute reb;
5,检测
select * from index_rebuid;

重建索引时dba的工作范围。
重建索引一般采用的是rebuild的方法,关于重建索引,合并索引,可以看我另外一篇文章。

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8337095/viewspace-1033891/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8337095/viewspace-1033891/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值