何时重建索引_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的工作范围。
一:分析(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的工作范围。
相关文章推荐
- 何时需要重建索引
- 金蝶数据库重建索引
- 数据库重组或者重建索引
- SQL SERVER数据库维护与重建索引
- 索引重建
- Scripts:重建索引的脚本(看懂脚本再用)rebuild_indx.sql
- oracle 索引重建提示脚本
- DBCC DBREINDEX重建索引提高SQL Server性能
- elasticsearch以scroll方式 大数据重建索引
- 重建和失效索引
- SQL SEVER数据库重建索引的方法
- 索引什么时候重建和重建方法讨论
- 并行重建本地分区索引
- 重建索引提高SQL Server性能<转>
- 重建索引时,一些数值
- Oracle关于重建索引争论的总结
- 重建索引的相关内容(from itpub)
- SQLServer 重建索引前后对比
- 什么时候需要重建索引?如何判断是否需要重建索引?为何要重建索引?
- SQLServer 索引重建存储过程