對於索引的調整,我們可以通過Oracle提供的索引監控特性來跟蹤索引是否被使用。儘管該特性並未提供索引使用的頻度,但仍不失為我們參考的方式之一。然而,最近在Oracle 10.2.0.3中發現收集統計資訊時導致索引也被監控,而不是用於sql查詢引發的索引監控。如此這般,索引監控豈不是雞肋?
1、基於Oracle 10g 收集統計資訊索引被監控情形
scott@CNMMBO> select * from v$version where rownum<2;
BANNER
----------------------------------------------------------------
Oracle Database 10g Release 10.2.0.3.0 - 64bit Production
--建立暫存資料表t
scott@CNMMBO> create table t(id number constraint t_pk primary key);
Table created.
--啟用索引監控
scott@CNMMBO> alter index t_pk monitoring usage;
Index altered.
--查看對象的使用方式
scott@CNMMBO> select * from v$object_usage where index_name='T_PK';
INDEX_NAME Table Name MON USE START_MONITORING END_MONITORING
------------------------------ ----------------- --- --- ------------------- -------------------
T_PK T YES NO 03/22/2013 20:53:23
--收集表t上的統計資訊
scott@CNMMBO> exec dbms_stats.gather_table_stats('SCOTT','T',cascade=>true);
PL/SQL procedure successfully completed.
--下面的查詢中提示索引沒有被使用
--這應該是由於表上沒有資料的緣故,也就不存在對應的索引段
scott@CNMMBO> select * from v$object_usage where index_name='T_PK';
INDEX_NAME Table Name MON USE START_MONITORING END_MONITORING
------------------------------ ----------------- --- --- ------------------- -------------------
T_PK T YES NO 03/22/2013 20:53:23
--下面嘗試插入兩條資料
scott@CNMMBO> insert into t select 1 from dual;
1 row created.
scott@CNMMBO> insert into t select 2 from dual;
1 row created.
--再次收集統計資訊
scott@CNMMBO> exec dbms_stats.gather_table_stats('SCOTT','T',cascade=>true);
PL/SQL procedure successfully completed.
--Author : Robinson
--Blog : http://blog.csdn.net/robinson-0612
--這下子,索引變成了已經被使用
scott@CNMMBO> select * from v$object_usage where index_name='T_PK';
INDEX_NAME Table Name MON USE START_MONITORING END_MONITORING
------------------------------ ------------------ --- --- ------------------- -------------------
T_PK T YES YES 03/22/2013 20:53:23