传统的SQL脚本查看表空间使用率,使用的关键视DBA_DATA_FILE和DBA_FREE_SPACE。Oracle 11g引入了DBA_TABLESPACE_USAGE_METRICS视图。事实上,Oracle 10g中就已经引入了该视图,可以使用,但在10g官方文档中查不到,11g官方文档对该视图作了说明。
Oracle 11g 官方文档中对DBA_TABLESPACE_USAGE_METRICS视图的说明:
DBA_TABLESPACE_USAGE_METRICS describes tablespace usage metrics for all types of tablespaces,including permanent,temporary,and undo tablespaces.
| Column |
Datatype |
NULL |
Description |
| TABLESPACE_NAME |
VARCHAR2(30) |
|
Tablespace name |
| USED_SPACE |
NUMBER |
|
Total space consumed by the tablespace,in database blocks |
| TABLESPACE_SIZE |
NUMBER |
|
Total size of the tablespace,in database blocks |
| USED_PERCENT |
NUMBER |
|
Percentage of used space,as a function of the maximum possible tablespace size |
从官网的说明来看,通过视图DBA_TABLESPACE_USAGE_METRICS可以很方便的查看各类型表空间的使用情况,包括永久、临时和undo表空间。但是,通过这个视图查询到的结果,和传统的SQL脚步查询到的结果不一致,而且相差很大。
看一下DBA_TABLESPACE_USAGE_METRICS视图的定义语句:
select text from dba_views where view_name='DBA_TABLESPACE_USAGE_METRICS';
TEXT
-----------------------------------------------------------------
SELECT t.name, sum(f.allocated_space), sum(f.file_maxsize),
(sum(f.allocated_space)/sum(f.file_maxsize))*100
FROM sys.ts$ t, v$filespace_usage f
WHERE
t.online$ != 3 and
t.bitmapped <> 0 and
t.contents$ = 0 and
bitand(t.flags, 16) <> 16 and
t.ts# = f.tablespace_id
GROUP BY t.name, f.tablespace_id
union
SELECT t.name, sum(f.allocated_space), sum(f.file_maxsize),
(sum(f.allocated_space)/sum(f.file_maxsize))*100
FROM sys.ts$ t, v$filespace_usage f
WHERE
t.online$ != 3 and
t.bitmapped <> 0 and
t.contents$ <> 0 and
f.flag = 6 and
t.ts# = f.tablespace_id
GROUP BY t.name, f.tablespace_id
union
SELECT t.name, sum(f.allocated_space), sum(f.file_maxsize),
(sum(f.allocated_space)/sum(f.file_maxsize))*100
FROM sys.ts$ t, gv$filespace_usage f, gv$parameter param
WHERE
t.online$ != 3 and
t.bitmapped <> 0 and
f.inst_id = param.inst_id and
param.name = 'undo_tablespace' and
t.name = param.value and
f.flag = 6 and
t.ts# = f.tablespace_id
GROUP BY t.name, f.tablespace_id
-----------------------------------------------------------------
可以看出,DBA_TABLESPACE_USAGE_METRICS查询到的关键数据出自v$filespace_usage视图。
对v$filespace_usage图,11g官方文档的说明如下:
V$FILESPACE_USAGE summarizes space allocation information of each datafile and tempfile.
| Column |
Datatype |
Description |
| TABLESPACE_ID |
nubmer |
ID of the tablespace to which the file belongs |
| RFNO |
NUMBER |
Relative file number of the file |
| ALLOCATED_SPACE |
NUMBER |
Total allocated space in the file |
| FILE_SIZE |
NUMBER |
Current file size |
| FILE_MAXSIZE |
NUMBER |
Maximum file size |
| CHANGESCN_BASE |
NUMBER |
SCN base of the last change to the file |
| CHANGESCN_WRAP |
NUMBER |
SCN wrap of the last change to the file |
| FLAG |
NUMBER |
Flags for file attributes |
根据以上分析,可以得出以下结论:
1、DBA_TABLESPACE_USED_SPACE是已经分配的空间,对应V$FILESPACE_USAGE的ALLOCATED_SPACE字段。
2、DBA_TABLESPACE_USAGE_METRICS的TABLESPACE_SIZE对应V$FILESPACE_USAGE的FILE_MAXSIZE字段(而不是FILE_SIZE)。注意:这里对应的是最大值。如果数据文件是自动增长的,那么,对于8k的block,这里的最大值就是32G,也就是通过DBA_TABLESPACE_USAGE_METRICS视图查询显示的4194302个blocks。
SQL> select * from dba_tablespace_usage_metrics;
TABLESPACE_NAME USED_SPACE TABLESPACE_SIZE USED_PERCENT
------------------------------ ---------- --------------- -------
SYSAUX 16576 4194302 .395202825
SYSTEM 33912 4194302 .