Oracle监控用户索引使用情况,删除无用索引

1. 监控当前用户所有索引

得到监控所有索引的语句:

select ‘alter index ’ || index_name || ’ monitoring usage;’ from user_indexes;

2. 一段时间后查询从未被使用的索引,删除无用索引

注意:视具体业务情况,选择一周后,一月后,两月后(总之要保证应用的所有SQL都至少跑一遍)

2.1 查看这段时间内,从未被使用的索引:

select * from v$object_usage where used=’NO’;

2.2 得到删除从未被使用的索引的语句:

select ‘drop index ‘||o.index_name||’;’ from v$object_usage o join user_indexes u on o.index_name = u.index_name where o.used=’NO’ and u.uniqueness=’NONUNIQUE’;

特别注意:直接drop index操作,从未被使用的索引中,主键不会被删除(会给出错误ORA-02429),但唯一性索引会被删掉。
所以我这里join了user_indexes,从而判断只删除NONUNIQUE的索引。

3. 停止监控用户所有索引

得到停止监控所有索引的语句:

select ‘alter index ’ || index_name || ’ nomonitoring usage;’ from user_indexes;

猜你喜欢

转载自blog.csdn.net/dhf984721977/article/details/81207793