何时重建索引_1

在什么情况下需要重建索引呢??重建索引需要如下两个条件

一:分析(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的工作范围。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值