日期:2014-05-16  浏览次数:20533 次

收集统计信息导致索引被监控

      对于索引的调整,我们可以通过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

2、基于Oracle 11g收集统计信息索引被监控情形

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 t(id number constraint t_pk primary key);

Table created.

SQL> alter index t_pk monitoring usage;

Index altered.

SQL> set linesize 190
SQL> 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:37:57

SQL> exec dbms_stats.gather_table_stats('SCOTT','T',cascade=>true);

PL/SQL procedure successfully completed.

SQL> 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:37:57

SQL> insert into t select 111 from dual;

1 row created.

SQL> insert into t select 222 from dual;

1 row created.

SQL> commit;

Commit complete.

SQL> exec dbms_stats.gather_table_stats('SCOTT','T',cascade=>true);

PL/SQL procedure successfully completed.

--可以看出,插入数据后,收集统计信息并不会导致索引被使用
SQL> select * from v$object_us