天天看點

資料庫資源回收筒

文檔課題:資料庫資源回收筒.
1、概念
資料庫資源回收筒從oracle10g引入.從原理上來說,資源回收筒是一個資料字典表,放置使用者删掉的資料庫對象.被存儲在資源回收筒中對象,仍然會占用空間,除非使用者手工進行purge或因儲存空間不夠而被清掉.如果一個表被删除,那麼與該表相關聯的對象,如索引、限制和其他依賴對象都會加bin$$字首.
1.1、啟動狀态
查資源回收筒啟動情況
SQL> show parameter recyclebin;

NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
recyclebin                           string                 on
1.2、修改狀态
SQL> alter system set recyclebin=on scope=spfile;
SQL> alter session set recyclebin=on scope=spfile;
SQL> alter system set recyclebin=off scope=spfile;
SQL> alter session set recyclebin=off scope=spfile;
1.3、直接删除
以下幾種drop不會将相關對象放進recyclebin.
drop tablespace:會将recyclebin中所有屬于該tablespace的對象清除
drop user:會将recyclebin中所有屬于該使用者的對象清除
drop cluster:會将recyclebin中所有屬于該cluster的成員對象清除
drop type:會将recyclebin中所有依賴該type的對象清除
1.4、閃回表
查user_recyclebin擷取被删除的表資訊,然後使用語句
flashback table tablename to before drop [rename to];
将資源回收筒裡的表恢複為原名或指定新名,表中資料不會丢失.
若要徹底删除表,則使用語句drop table tablename purge;
1.5、清理資源回收筒
清除資源回收筒裡的資訊
清除指定表:purge table tablename;
清除目前使用者的資源回收筒:purge recyclebin;
清除所有使用者的資源回收筒:purge dba_recyclebin;
資料庫更新前資源回收筒處理:
SQL> select sum(bytes)/1024/1024 gb from dba_segments where segment_name in (select object_name from dba_recyclebin);

        GB
----------

SQL> purge dba_recyclebin;

DBA 資源回收筒已清空.
2、實際操作
資料庫版本:oracle 19.3
2.1、異名對象
SQL> create table t1 as select * from employees;

表已建立.

SQL> create table t2 as select * from t1;

表已建立.
SQL> select * from tab;

TNAME                TABTYPE                     CLUSTERID
-------------------- -------------------------- ----------
T1                   TABLE
T2                   TABLE

已選擇 2 行.
SQL> drop table t2;

表已删除.

SQL> select object_name,original_name,operation,type from user_recyclebin;
OBJECT_NAME                    ORIGINAL_NAME        OPERATION            TYPE
------------------------------ -------------------- -------------------- ----------
BIN$zPmMbTcsRMSn4EdPl5O29Q==$0 T2                   DROP                 TABLE

SQL> flashback table t2 to before drop;

閃回完成.

SQL> select * from tab;

TNAME                TABTYPE                     CLUSTERID
-------------------- -------------------------- ----------
T1                   TABLE
T2                   TABLE

已選擇 2 行.
SQL> select object_name,original_name,operation,type from user_recyclebin;

未標明行
SQL> drop table t2 purge;  --加purge

表已删除.

SQL> select * from tab;

TNAME                TABTYPE                     CLUSTERID
-------------------- -------------------------- ----------
T1                   TABLE

已選擇 1 行.
SQL> select object_name,original_name,operation,type from user_recyclebin;
未標明行
2.2、同名對象
SQL> drop table t1;

表已删除.

SQL> create table t1 as select * from DEPARTMENTS;

表已建立.

SQL> drop table t1;

表已删除.

SQL> create table t1 as select * from employees;

表已建立.

SQL> drop table t1;

表已删除.

SQL> select original_name,droptime from user_recyclebin where original_name='T1' order by droptime;

ORIGINAL_NAME        DROPTIME
-------------------- --------------------------------------
T1                   2022-04-23:10:44:14
T1                   2022-04-23:10:44:48
T1                   2022-04-23:10:45:33
SQL> purge table t1;

表已清除.

SQL> select original_name,droptime from user_recyclebin where original_name='T1' order by droptime;

ORIGINAL_NAME        DROPTIME
-------------------- --------------------------------------
T1                   2022-04-23:10:44:48
T1                   2022-04-23:10:45:33
SQL> select object_name,original_name,operation,type from user_recyclebin;

OBJECT_NAME                    ORIGINAL_NAME        OPERATION            TYPE
------------------------------ -------------------- -------------------- ----------
BIN$ZQ9GjdgUSU6vwafF4mMFPw==$0 T1                   DROP                 TABLE
BIN$+XXS+h6aR8GvUZkwFSBsSA==$0 T1                   DROP                 TABLE
結論:當recyclebin中存在同名對象,purge table table_name時,最先被删除的對象先從recyclebin中釋放.