说明:
应用程序在开发时,可能会建立众多索引,但是这些索引的使用到底怎么样,是否有些索引一直都没有用到过,这需要我们对这些索引进行监控,以便确定他们的使用情况,并为是否可以清除它们给出依据。
本文介绍两种方式:
第一:开启监控功能;
第二:查看历史的执行计划,进行分析;
环境:
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
测试:
第一:
CREATE TABLE tt
AS
SELECT * FROM dba_objects;
DELETE FROM tt
WHERE object_id IS NULL;
ALTER TABLE tt ADD CONSTRAINT pk_tt PRIMARY KEY(object_id);
注:使用alter index nomonitoring usage取消监控。
SELECT object_id, object_name
FROM tt
WHERE object_id = 333;
SELECT INDEX_NAME, MONITORING, USED, START_MONITORING, END_MONITORING
FROM V$OBJECT_USAGE;
为了避免V$OBJECT_USAGE只能查询到当前用户下索引的监控情况,可以使用如下语句查询数据库中所有监控索引的使用情况:
SELECT U.NAME OWNER,
IO.NAME INDEX_NAME,
T.NAME TABLE_NAME,
DECODE(BITAND(I.FLAGS, 65536), 0, 'NO', 'YES') MONITORING,
DECODE(BITAND(OU.FLAGS, 1), 0, 'NO', 'YES') USED,
OU.START_MONITORING START_MONITORING,
OU.END_MONITORING END_MONITORING
FROM SYS.USER$ U,
SYS.OBJ$ IO,
SYS.OBJ$ T,
SYS.IND$ I,
SYS.OBJECT_USAGE OU
WHERE I.OBJ# = OU.OBJ#
AND IO.OBJ# = OU.OBJ#
AND T.OBJ# = I.BO#
AND U.USER# = IO.OWNER#
第二:
1 利用library cache数据
SELECT OBJECT_NAME
FROM V$SQL_PLAN A, V$SQLAREA B
WHERE A.SQL_ID = B.SQL_ID
AND A.OBJECT_TYPE = 'INDEX'
AND OBJECT_OWNER IN ('XXXX', 'XXXX');
2 利用STATSPACK
SELECT A.OBJECT_OWNER, A.OBJECT_NAME
FROM STATS$SQL_PLAN A, STATS$SQL_PLAN_USAGE B
WHERE A.PLAN_HASH_VALUE = B.PLAN_HASH_VALUE
AND A.OBJECT_TYPE = 'INDEX';
注:需要开启Statspack的快照功能,否则查不到数据;
3 如果是10g以上版本的数据,可以使用AWR
SELECT B.OBJECT_NAME
FROM DBA_HIST_SNAPSHOT A, DBA_HIST_SQL_PLAN B, DBA_HIST_SQLSTAT C
WHERE A.SNAP_ID = C.SNAP_ID
AND B.SQL_ID = C.SQL_ID
AND B.OBJECT_TYPE = 'INDEX'
AND B.OBJECT_OWNER IN ('XXX', 'XXXX');
注:可以通过如下语句查询AWR的运行情况
SELECT *
FROM DBA_HIST_WR_CONTROL;
参考:
有待进一步学习的内容:
补充:
关于索引监控的或者说跟踪,本文仅仅是停留在简单的理论阶段,到实践和深入还有很多内容要了解。
阅读(1540) | 评论(0) | 转发(0) |