Oracle dba_data_files dba_segments dba_free_space

1, dba_data_files,dba_segments,dba_free_space
得出结论:一般情况下(没有drop表的时候):dba_data_files bytes = (dba_segments bytes + dba_free_space bytes)

SQL>

select TABLESPACE_NAME,
       BYTES/1024/1024,BLOCKS, 
       AUTOEXTENSIBLE,
       MAXBYTES,
       MAXBLOCKS,
       INCREMENT_BY,
       USER_BYTES, 
       USER_BLOCKS 
from   dba_data_files
where  tablespace_name='TS_20120626';
TABLESPACE_NAME      BYTES/1024/1024     BLOCKS AUT   MAXBYTES  MAXBLOCKS INCREMENT_BY USER_BYTES USER_BLOCKS
-------------------- --------------- ---------- --- ---------- ---------- ------------ ---------- -----------
TS_20120626                      120      15360 YES 3.4360E+10    4194302        12800  125763584       15352

SQL>select OWNER,SEGMENT_NAME,TABLESPACE_NAME,BYTES/1024/1024,BLOCKS fromdba_segmentswhere TABLESPACE_NAME='TS_20120626';
OWNER                    SEGMENT_NAME                   TABLESPACE_NAME                BYTES/1024/1024     BLOCKS
-----------------------  ------------------------------ ------------------------------ --------------- ----------
USER_20120626            TB_20120626_001                TS_20120626                                 61       7808

SQL>select tablespace_name,bytes/1024/1024||' MB' Total_Free fromdba_free_spacewhere tablespace_name='TS_20120626';
TABLESPACE_NAME                TOTAL_Free
------------------------------ -------------------------------------------
TS_20120626                    58.9375 MB

2, delete,truncate,drop对空间的变化影响
delete表数据,对上述3个视图查询没有任何变化,满足上述相等原则.
truncate表数据,表空间一次性回收,dba_free_space增加,dba_segments减少,让满足上述相等原则.
drop表,表空间一次性回收,但dba_segment会多出个回收站,但实际上dba_free_segment也把回收站的那部分空间计算在内了,所以回收站的内容实际也是可以使用的部分,表面上看这个等式不成立了,但实质上做了些改变,看下面的例子.

SQL>select TABLESPACE_NAME,BYTES/1024/1024,BLOCKS, AUTOEXTENSIBLE,MAXBYTES,MAXBLOCKS,INCREMENT_BY,USER_BYTES,USER_BLOCKS from dba_data_files where tablespace_name='TS_20120626';

TABLESPACE_NAME           BYTES/1024/1024     BLOCKS AUT   MAXBYTES  MAXBLOCKS INCREMENT_BY USER_BYTES USER_BLOCKS
------------------------- --------------- ---------- --- ---------- ---------- ------------ ---------- -----------
TS_20120626                          320      40960 YES 3.4360E+10    4194302        12800  335478784       40952

SQL>select OWNER,SEGMENT_NAME,TABLESPACE_NAME,BYTES/1024/1024,BLOCKS from dba_segments where TABLESPACE_NAME='TS_20120626';

OWNER                    SEGMENT_NAME                   TABLESPACE_NAME     BYTES/1024/1024     BLOCKS
-----------------------  -----------------------------  ------------------- --------------- ----------
USER_20120626            TB_20120626_002                TS_20120626        168           21504
USER_20120626            TB_20120626_001                TS_20120626       .0625          8  //truncate的表数据
USER_20120626            BIN$w1rH4uiTmbbgQKjANgEvlg==$0 TS_20120626        28             3584//drop后的回收站部分

SQL>select tablespace_name,bytes/1024/1024||' MB' Total_Free from dba_free_space where tablespace_name='TS_20120626';

TABLESPACE_NAME                TOTAL_FREE
------------------------------ -------------------------------------------
TS_20120626                    27.9375 MB//truncate后,一次性回收的部分.
TS_20120626                    95.9375 MB
TS_20120626                    .0625 MB //drop后,从这里开始都是回收站的部分.这里和上面的
BIN$w1rH4uiTmbbgQKjANgEvlg==$0是同一空间
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    .0625 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
TS_20120626                    1 MB
45 rows selected.

将上述查询结果用tablespace_name分组加起来
SQL>select tablespace_name,count(tablespace_name) ,sum(bytes/1024/1024)||' MB' Total_Free from dba_free_space where tablespace_name='TS_20120626' group by tablespace_name;
TABLESPACE_NAME                COUNT(TABLESPACE_NAME) TOTAL_FREE
------------------------------ ---------------------- -------------------------------------------
TS_20120626                                        45151.875MB

所以上述等式需要改为(有drop时):dba_data_files bytes = (dba_segments bytes + dba_free_space bytes- dba_segment回收站部分)

你可能感兴趣的:(Oracle,Administration,SQL,Script,Tools)