儲存管理之段收縮、可恢復空間

物理狂人發表於2011-12-05
表空間的使用情況監控:
SQL> select tablespace_name,sum(bytes)/1024/1024 "free(M)" from dba_free_space g
roup by tablespace_name;

TABLESPACE_NAME                   free(M)
------------------------------                  ----------
UNDOTBS1                                     284.9375
SYSAUX                                           14.5625
USERS                                             4.375
SYSTEM                                           6.5
EXAMPLE                                         22.3125

擴充套件users表空間到100m
SQL> alter database datafile 4 resize 100m;

資料庫已更改。

SQL>
SQL> select tablespace_name,sum(bytes)/1024/1024 "free(M)" from dba_free_space g
roup by tablespace_name;

TABLESPACE    free(M)
---------- ----------
UNDOTBS1     284.9375
SYSAUX        14.5625
USERS          94.375
SYSTEM            6.5
EXAMPLE       22.3125

建立T1表,插入資料,檢視錶空間變化
SQL> create table t1 as select * from dba_objects;

表已建立。

SQL> insert into t1 select * from t1;

已建立50646行。

SQL> select tablespace_name,sum(bytes)/1024/1024 "free(M)" from dba_free_space g
roup by tablespace_name;

TABLESPACE    free(M)
---------- ----------
UNDOTBS1     284.9375
SYSAUX        14.5625
USERS          82.375
SYSTEM            6.5
EXAMPLE       22.3125

段收縮測試:
SQL> select table_name,blocks from user_tables where table_name='T1';

TABLE_NAME                         BLOCKS
------------------------------ ----------
T1

收集統計資訊
SQL> exec dbms_stats.gather_table_stats('SCOTT','T1');

PL/SQL 過程已成功完成。

SQL> select table_name,blocks from user_tables where table_name='T1';

TABLE_NAME                         BLOCKS
------------------------------ ----------
T1                                   1524

啟動行移動
SQL> alter table t1 enable row movement;

表已更改。

刪除5000行資料,檢視錶的塊數量
SQL> delete from t1 where rownum<50000;

已刪除49999行。

SQL> select table_name,blocks from user_tables where table_name='T1';

TABLE_NAME                         BLOCKS
------------------------------ ----------
T1                                   1524


此時沒有改變,即使收集統計資訊後,也沒有改變,因為刪除並不釋放資料塊
SQL> exec dbms_stats.gather_table_stats('SCOTT','T1');

PL/SQL 過程已成功完成。

SQL> select table_name,blocks from user_tables where table_name='T1';

TABLE_NAME                         BLOCKS
------------------------------ ----------
T1                                   1524

然後進行段收縮操作
SQL> alter table t1 shrink space ;

表已更改。

SQL> select table_name,blocks from user_tables where table_name='T1';

TABLE_NAME                         BLOCKS
------------------------------ ----------
T1                                   1524

SQL> exec dbms_stats.gather_table_stats('SCOTT','T1');

PL/SQL 過程已成功完成。

SQL> select table_name,blocks from user_tables where table_name='T1';

TABLE_NAME                         BLOCKS
------------------------------ ----------
T1                                    706

這樣發現塊數量減少到了706達到了釋放塊的目的

管理可恢復空間
建立不能自動擴充套件的表空間
SQL> create tablespace test datafile 'd:\test.dbf' size 1m
  2  /

表空間已建立。

SQL> create table tt as select * from user_tables;

表已建立。

SQL> alter table tt move tablespace test;

表已更改。

SQL> insert into tt select * from tt;

已建立5行。

SQL> insert into tt select * from tt;

已建立10行。

SQL> insert into tt select * from tt;

已建立20行。

SQL> insert into tt select * from tt;

已建立40行。

SQL> insert into tt select * from tt;

已建立80行。

SQL> insert into tt select * from tt;

已建立160行。

SQL> insert into tt select * from tt;

已建立320行。

SQL> insert into tt select * from tt;

已建立640行。

SQL> insert into tt select * from tt;

已建立1280行。

SQL> insert into tt select * from tt;
insert into tt select * from tt
*
第 1 行出現錯誤:
ORA-01653: unable to extend table SCOTT.TT by 8 in tablespace TEST

此時沒有開啟可恢復功能,於是該操作將被回滾掉

然後開啟可恢復功能
SQL> commit;

提交完成。

SQL> alter session enable resumable;

會話已更改。

SQL> insert into tt select * from tt;
這時發現這個插入操作將被掛起

在另一個session,檢視test.dbf的檔案號
SQL> col file_name format a50;
SQL> select file_name,file_id from dba_data_files;

FILE_NAME                                             FILE_ID
-------------------------------------------------- ----------
F:\ORACLE\PRODUCT\10.2.0\DB_1\ORADATA\ORCL\USERS01          4
.DBF

F:\ORACLE\PRODUCT\10.2.0\DB_1\ORADATA\ORCL\SYSAUX0          3
1.DBF

F:\ORACLE\PRODUCT\10.2.0\DB_1\ORADATA\ORCL\UNDOTBS          2
01.DBF

F:\ORACLE\PRODUCT\10.2.0\DB_1\ORADATA\ORCL\SYSTEM0          1
1.DBF

FILE_NAME                                             FILE_ID
-------------------------------------------------- ----------

F:\ORACLE\PRODUCT\10.2.0\DB_1\ORADATA\ORCL\EXAMPLE          5
01.DBF

D:\TEST.DBF                                                 6

已選擇6行。

將test.dbf擴充套件到10M
SQL> alter database datafile 6 resize 10m;

資料庫已更改。


然後回到之前的session
SQL> insert into tt select * from tt;

已建立2560行。
掛起的操作此時完成了

另外可以設定掛起時間,10秒
SQL> alter session enable resumable timeout 10;

會話已更改。

。。。。。。

然後掛起10秒後,還沒有處理該問題就報錯

SQL> insert into tt select * from tt;

已建立5120行。

SQL>
SQL> insert into tt select * from tt;

已建立10240行。

SQL> insert into tt select * from tt;
insert into tt select * from tt
*
第 1 行出現錯誤:
ORA-30032: the suspended (resumable) statement has timed out
ORA-01653: unable to extend table SCOTT.TT by 128 in tablespace TEST



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24104518/viewspace-712745/,如需轉載,請註明出處,否則將追究法律責任。

相關文章