随着时间的累积,在没有很好的规划的情况下,数据库中也许会存在大量长期不被使用的索引,如果快速的定位这些索引以便清理便摆在
随着时间的累积,,在没有很好的规划的情况下,数据库中也许会存在大量长期不被使用的索引,如果快速的定位这些索引以便清理便摆在案头。我们可以使用alter index ××× monitoring usage;命令将索引至于监控状态下,经过一定的监控周期,那些不被使用到的索引便会在具体schema下的v$object_usage视图中得以体现。展示一下这个过程,供参考。
友情提示:生产数据库中的索引添加和删除一定要慎重,需要做好充分的测试。
1.环境准备
--1、创建表t
sql> create table t (x int);
table created.
--2、初始化一条数据
sql> insert into t values (1);
1 row created.
sql> select * from t;
x
----------
1
--3、在表t的x字段上创建索引
sql> create index i_t on t(x);
index created.
2.将索引i_t置于监控状态下
sql> alter index i_t monitoring usage;
index altered.
3.查看v$object_usage视图中记录的信息
sql> col index_name for a10
sql> col table_name a10
sql> col start_monitoring for a20
sql> col end_monitoring for a20
sql> select * from v$object_usage;
index_name table_name monitoring used start_monitoring end_monitoring
---------- ---------- ---------- --------- -------------------- -----------------
i_t t yes no 07/17/2010 22:27:13
此时monitoring字段内容为“yes”,表示i_t已经处于被监控状态。used字段内容为“no”表示该索引还未被使用过。
4.模拟索引被使用
sql> set autot on
sql> select * from t where x = 1;
x
----------
1
execution plan
----------------------------------------------------------
plan hash value: 2616361825
-------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-------------------------------------------------------------------------
| 0 | select statement | | 1 | 13 | 1 (0)| 00:00:01 |
|* 1 | index range scan| i_t | 1 | 13 | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - access(x=1)
note
-----
- dynamic sampling used for this statement
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
1 consistent gets
0 physical reads
0 redo size
508 bytes sent via sql*net to client
492 bytes received via sql*net from client
2 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
从执行计划上可以看出,该查询使用到了索引i_t
5.再次查看v$object_usage视图中记录的信息
sql> set autot off
sql> select * from v$object_usage;
index_name table_name monitorin used start_monitoring end_monitoring
---------- ---------- --------- --------- -------------------- -----------------
i_t t yes yes 07/17/2010 22:27:13
此时used字段内容变为“yes”,表示i_t索引在监控的这段时间内被使用过。
如果在一个较科学的监控周期下used字段一直处于“no”的状态,则可以考虑将此类索引删掉。
6.停止对索引的监控,观察v$object_usage状态变化
sql> alter index i_t nomonitoring usage;
index altered.
sec@ora10g> select * from v$object_usage;
index_name table_name monitorin used start_monitoring end_monitoring
---------- ---------- --------- --------- -------------------- -------------------
i_t t no yes 07/17/2010 22:27:13 07/17/2010 22:32:27
此时monitorin字段内容为“no”,表示已经停止对索引i_t的监控。
7.再次启用索引监控,观察v$object_usage状态变化
sql> alter index i_t monitoring usage;
index altered.
sec@ora10g> select * from v$object_usage;
