oracle索引管理脚本
oracle索引管理脚本
来自metalink
--检测某一个索引使用的情况
select to_char(sn.begin_interval_time, 'yyyy-mm-dd hh24') c1,
p.search_columns c2,
count(*) c3
from dba_hist_snapshot sn, dba_hist_sql_plan p, dba_hist_sqlstat st
where st.sql_id = p.sql_id
and sn.snap_id = st.snap_id
and p.object_name = '&idxname'
group by sn.begin_interval_time, p.search_columns;
--需要重建的索引
declare
c_name int;
ignore int;
heights index_stats.height%type := 0;
lf_rowss index_stats.lf_rows%type := 0;
del_lf_rowss index_stats.del_lf_rows%type := 0;
distinct_keyss index_stats.distinct_keys%type := 0;
cursor c_indx is
select owner, table_name, index_name
from dba_indexes
where owner like upper('&schema')
and owner not in ('SYS', 'SYSTEM');
BEGIN
dbms_output.enable(10000000);
dbms_output.put_line('Owner index name %Deleted Entries Blevel Distinctiveness');
dbms_output.put_line('-----------------------------------------------------');
c_name := dbms_sql.open_cursor;
for r_indx in c_indx loop
dbms_sql.parse(c_name,
'analyze index ' || r_indx.owner || '.' ||
r_indx.index_name || ' validate structure',
dbms_sql.native);
ignore := dbms_sql.execute(c_name);
select height,
decode(lf_rows, 0, 1, lf_rows) lf_rows_status,
del_lf_rows,
decode(distinct_keys, 0, 1, distinct_keys) dis_keys_status
into heights, lf_rowss, del_lf_rowss, distinct_keyss
from index_stats;
if (heights > 5) or ((del_lf_rowss / lf_rowss) > 0.2) then
dbms_output.put_line(rpad(r_indx.owner, 16, ' ') ||
rpad(r_indx.index_name, 40, ' ') ||
lpad(round((del_lf_rowss / lf_rowss) * 100, 3),
17,
' ') || lpad(heights - 1, 7, ' ') ||
lpad(round((lf_rowss - distinct_keyss) * 100 /
lf_rowss,
3),
16,
' '));
end if;
end loop;
dbms_sql.close_cursor(c_name);
END;