- 監控當前業務用戶索引
- 一段時間后查詢從未被使用的索引,刪除無用索引
- 停止監控索引
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; ```