Oracle 監控索引特性為我們提供了一個大致判斷索引是否被使用的情形。之所以這麼說,是因為在Oracle 10g 中收集統計資訊時會導致索引被監控,此並非sql語句而產生。而在11g則不會出現類型的情形。其次對於存在子表存在外鍵的情形,對於主表進行操作時是否會導致索引被監控呢?下面描述的是這個話題。
1、普通監控索引的情形
--示範環境
SQL> select * from v$version where rownum<2;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
--建立主表
SQL> create table ptb(deptno number constraint ptb_pk primary key,dname varchar2(20));
Table created.
--從scott.dept帳戶複製資料
SQL> insert into ptb select deptno,dname from dept;
4 rows created.
SQL> commit;
Commit complete.
--開啟索引監控
SQL> alter index ptb_pk monitoring usage;
--為主表收集統計資訊
SQL> exec dbms_stats.gather_table_stats('SCOTT','PTB',cascade=>true);
PL/SQL procedure successfully completed
SQL> select * from v$object_usage where index_name='PTB_PK';
INDEX_NAME TABLE_NAME MON Use START_MONITORING END_MONITORING
------------------------------ ------------------------- --- --- ------------------- -------------------
PTB_PK PTB YES NO 03/22/2013 17:15:37
--注意上面的情形,收集統計資訊時,索引被使用沒有被監控到,在10g中則會被監控到
--下面開啟autotrace
SQL> set autot trace exp;
SQL> select * from ptb where deptno=10;
Execution Plan
----------------------------------------------------------
Plan hash value: 3991869509
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 12 | 1 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| PTB | 1 | 12 | 1 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | PTB_PK | 1 | | 0 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
SQL> set autot off;
SQL> select * from v$object_usage where index_name='PTB_PK'; --索引使用被監控到
INDEX_NAME TABLE_NAME MON Use START_MONITORING END_MONITORING
------------------------------ ------------------------- --- --- ------------------- -------------------
PTB_PK PTB YES YES 03/22/2013 17:15:37