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

Oracle表变化趋势追踪记录(原创)

这几天数据库上的用户表空间疯涨,一天的增长量达到了20~30GB,平时就4~5GB的增长量。问了开发商,他们也说不出个所以然。PS:那些鸟开发商,现在我已经对他们绝望了,什么都不知道。情急之下直接对该表空间进行了扩容,问题总算解决了。事后总想弄个明白,想知道是那张表导致的表空间增长如此之快,以免下次出现同样的问题又是不知所措。

google了一番后发现DBA_HIST_SEG_STAT可以看出对象的使用趋势
构造如下SQL查询出每个时间段内数据库对象的增长量,其中DB_BLOCK_CHANGES_DELTA为块个数

select c.SNAP_ID,to_char(c.END_INTERVAL_TIME,'yyyy-mm-dd') SNAP_TIME,a.OWNER ,a.OBJECT_NAME, a.OBJECT_TYPE, b.DB_BLOCK_CHANGES_DELTA
from dba_objects a,
(select SNAP_ID,obj#,DB_BLOCK_CHANGES_DELTA from DBA_HIST_SEG_STAT where DB_BLOCK_CHANGES_DELTA > 20000 order by snap_id desc, DB_BLOCK_CHANGES_DELTA desc) b,
DBA_HIST_SNAPSHOT c
where a.object_id=b.obj# and object_type='TABLE' and b.SNAP_ID=c.SNAP_ID
order by DB_BLOCK_CHANGES_DELTA

下面的SQL查询出某个时间段内,数据库对象的变化大小,单位以换算成GB

select a.OBJECT_NAME, to_char(c.END_INTERVAL_TIME,'yyyy-mm-dd') SNAP_TIME, sum(b.DB_BLOCK_CHANGES_DELTA)/1024/1024 GB from dba_objects a,(select SNAP_ID,obj#,DB_BLOCK_CHANGES_DELTA from DBA_HIST_SEG_STAT where DB_BLOCK_CHANGES_DELTA > 20000 order by snap_id desc, DB_BLOCK_CHANGES_DELTA desc) b,DBA_HIST_SNAPSHOT c
where a.object_id=b.obj# and object_type='TABLE' and b.SNAP_ID=c.SNAP_ID
group by a.OBJECT_NAME,c.END_INTERVAL_TIME
order by GB

简单介绍下DBA_HIST_SEG_STAT

DBA_HIST_SEG_STAT视图内记录了数据库实例启动以来段的变化信息,该视图的数据来自于v$segstat

ColumnDatatypeNULLDescription
SNAP_ID NUMBER ? Unique snapshot ID
DBID NUMBER ? Database ID for the snapshot
INSTANCE_NUMBER NUMBER ? Instance number for the snapshot
TS# NUMBER ? Tablespace number
OBJ# NUMBER ? Dictionary object number
DATAOBJ# NUMBER ? Data object number
LOGICAL_READS_TOTAL NUMBER ?