检测表空间的使用情况以及碎片,要到两个数据字典表:dba_data_files和dba_free_space.其中dba_data_files描述了每个数据文件的信息,而dba_free_space则描述了表空间中每一段连续空闲空间的情况,为此可以透过看空闲空间的数量来初步估算表空间的碎片情况。
SQL> col "表空间" format A20;
SQL> set linesize 100 pagesize 100;
SQL> select df.tablespace_name "表空间",
2 df.total "最大空间M",
3 df.allocated "已分配空间M",
4 df.unallocated "未分配空间M",
5 df.allocated-ds.freespace "已使用空间M",
6 round((df.allocated-ds.freespace)/df.allocated*100,2) "使用率%",
7 ds.fragment "碎片量"
8 from
9 (select tablespace_name,
10 round(sum(maxbytes/1024/1024),2) total,
11 round(sum(bytes/1024/1024),2) allocated,
12 round(sum((maxbytes-bytes)/1024/1024),2) unallocated
13 from dba_data_files group by tablespace_name) df,
14 (select tablespace_name,
15 round(sum(bytes/1024/1024),2) freespace,
16 count(*) fragment
17 from dba_free_space group by tablespace_name)ds
18 where df.tablespace_name=ds.tablespace_name;
表空间 最大空间M 已分配空间M 未分配空间M 已使用空间M 使用率% 碎片量
-------------------- ---------- ----------- ----------- ----------- ---------- ----------
SYSAUX 32767.98 600 32167.98 568.19 94.7 1
UNDOTBS1 32767.98 100 32667.98 24.12 24.12 3
USERS 32767.98 33.75 32734.23 31.19 92.41 2
SYSTEM 32767.98 690 32077.98 687.75 99.67 2
EXAMPLE 32767.98 100 32667.98 78.75 78.75 3
TEST01 25 19 6 1 5.26 1
已选择6行。
SQL>