ITPub博客

首页 > 数据库 > 数据库开发技术 > 关于索引何时需要重建 [转]

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

原创 数据库开发技术 作者:oo0yuki0oo 时间:2010-05-24 17:36:52 0 删除 编辑

关于索引何时需要重建。

通过一个视图(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博客 ” ,链接:https://blog.itpub.net/8337095/viewspace-1033891/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 查login 的info
请登录后发表评论 登录
全部评论

注册时间:2009-04-22

  • 博文量
    62
  • 访问量
    82173