1、先分析索引,語句如下:
analyze index index_name validate structure
然後查詢 index_stats
select name, height, pct_used, del_lf_rows/lf_rows from index_stats;
DEL_LF_ROWS於LF_ROWS的比例 這是最基本的了
如果查詢結果中:
Height > 4
PCT_USER < 50
del_lf_rows/lf_rows > 0.2
則說明索引需要重建。
可以通過程序來實現該功能
注意:analyze index 和查詢index_stats 必須在同一個session中
create table MONITORINDEX
(
INDEX_NAME VARCHAR2(50),
DEL_LF_ROWS NUMBER,
LF_ROWS NUMBER,
RATE NUMBER(4,2),
MONITORDATE DATE default sysdate not null
)
create or replace procedure analyzeindex is
v_sql varchar2(100);
begin
for a in (select index_name from all_indexes where owner = USER) loop
v_sql := ' analyze index ' || a.index_name || ' validate structure';
execute immediate v_sql;
insert into monitorindex
(index_name, del_lf_rows, lf_rows, rate)
select name,
del_lf_rows,
lf_rows,
round(del_lf_rows * 100 / (lf_rows + del_lf_rows), 2)
from index_stats;
end loop;
end analyzeindex;