您的位置:首页 > 其它

何时重建索引_1

2014-03-24 15:21 316 查看
在什么情况下需要重建索引呢??重建索引需要如下两个条件

一:分析(analyze)指定索引之后,查询index_stats的height字段的值,如果这个值>=4 ,最好重建(rebuild)这个索引。虽然这个规则不是总是正确,但如果这个值一直都是不变的,则这个索引也就不需重建。

二:在分析(analyze)指定索引之后,查询index_stats的del_lf_rows和lf_rows的值,如果(del_lf_rows/lf_rows)*100
> = 20,则这个索引也需要重建

 举例如下:
SQL > analyze index IND_PK validate structure;(IND_PD是建的索引名)

SQL > select name,height,del_lf_rows,lf_rows,
(del_lf_rows/lf_rows) *100 from index_stats;
NAME    HEIGHT DEL_LF_ROWS  LF_ROWS (DEL_LF_ROWS/LF_ROWS)*100
------------------------------
INDX_PK  4   277353   990206   28.0096263
SQL>
alter index IND_PK rebuild

----------------------------------------------------------------

但可以通过一个视图(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的工作范围。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: