应用场景:最近遇到客户想知道数据库最近一年以来实际数据的增长量,当时只统计了数据文件的增长的情况,但作为DBA都很清楚,通过查看数据文件的增长情况是不够严谨的, 但当时笔者并没有确认到更合适的sql语句. 所以通过以下测试来验证解决这个问题.

以下语句适用于12c:

SELECT a.snap_id,
c.tablespace_name ts_name,
to_char(to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss'),
'yyyy-mm-dd hh24:mi') rtime,
round(a.tablespace_size * c.block_size / 1024 / 1024, 2) ts_size_mb,
round(a.tablespace_usedsize * c.block_size / 1024 / 1024, 2) ts_used_mb,
round((a.tablespace_size - a.tablespace_usedsize) * c.block_size / 1024 / 1024,
2) ts_free_mb,
round(a.tablespace_usedsize / a.tablespace_size * 100, 2) pct_used
FROM dba_hist_tbspc_space_usage a,
(SELECT tablespace_id,
substr(rtime, 1, 10) rtime,
max(snap_id) snap_id
FROM dba_hist_tbspc_space_usage nb
group by tablespace_id, substr(rtime, 1, 10)) b,
dba_tablespaces c,
v$tablespace d
where a.snap_id = b.snap_id
and a.tablespace_id = b.tablespace_id
and a.tablespace_id = d.TS#
and d.NAME = c.tablespace_name
and to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss') >= sysdate - 30
order by a.tablespace_id, to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss') desc;

笔者使用的数据库为oracle 19.14,表test为cdb环境下sys用户新建的测试表,表空间为system,将其增加4049m.

SQL> select bytes/1024/1024 from user_segments where segment_name='TEST';

BYTES/1024/1024
---------------
4049

通过以上查询语句在pl sql中执行,可以看到TS_USED_MB从1002.94m增加到5051.88m,数据在整数位吻合.

查数据库实际数据增长情况_sql

备注:对于数据增长后,又执行rollback,显示的数据量并不会再减少.通过这种方法可以显示出每个表空间的增长情况,然后通过简单的运算就可以估计出数据库实际数据的增长情况.

以下验证10g和11g的sql语句:

此处使用的数据库为oracle 11.2.0.4

SELECT a.snap_id,
c.tablespace_name ts_name,
to_char(to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss'),
'yyyy-mm-dd hh24:mi') rtime,
round(a.tablespace_size * c.block_size / 1024 / 1024, 2) ts_size_mb,
round(a.tablespace_usedsize * c.block_size / 1024 / 1024, 2) ts_used_mb,
round((a.tablespace_size - a.tablespace_usedsize) * c.block_size / 1024 / 1024,
2) ts_free_mb,
round(a.tablespace_usedsize / a.tablespace_size * 100, 2) pct_used
FROM dba_hist_tbspc_space_usage a,
(SELECT tablespace_id,
substr(rtime, 1, 10) rtime,
max(snap_id) snap_id
FROM dba_hist_tbspc_space_usage nb
group by tablespace_id, substr(rtime, 1, 10)) b,
dba_tablespaces c,
v$tablespace d
where a.snap_id = b.snap_id
and a.tablespace_id = b.tablespace_id
and a.tablespace_id = d.TS#
and d.NAME = c.tablespace_name
and to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss') >= sysdate - 30
order by a.tablespace_id, to_date(a.rtime, 'mm/dd/yyyy hh24:mi:ss') desc;

验证:

SQL> select default_tablespace,temporary_tablespace,username from dba_users where username='LIUJUN';

DEFAULT_TABLESPACE TEMPORARY_TABLESPACE USERNAME
------------------------------ ------------------------------ ------------------------------
USERS TEMP1 LIUJUN
SQL> select bytes/1024/1024 from user_segments where segment_name='T1';

BYTES/1024/1024
---------------
4
SQL> show user
USER is "LIUJUN"

将T1表由4m增加到485m.

SQL> select bytes/1024/1024 from user_segments where segment_name='T1';

BYTES/1024/1024
---------------
485

通过以上sql查询的结果,我们可以看到表空间增加了481m,数据吻合.

注意:此处也一样,对于执行rollback的操作,数据量的显示并不会减少.

查数据库实际数据增长情况_数据_02

参考网址:https://www.cnblogs.com/ataoxz/p/14144218.html