oracle 回收站

hexel發表於2013-12-22

回收站(RecycleBin)是一個資料字典表,放置使用者刪除(drop)掉的資料庫物件資訊。使用者進行刪除操作的物件並沒有被資料庫刪除,仍然會佔用空間。如果一個表被刪除,那麼與該表有關聯的物件,例如表、索引、約束和其他依賴物件都會在前面加bin$$這個字首。

hr@HX> create table t as select * from dba_objects;

Table created.

hr@HX> create index idx_t_id on t(object_id);

Index created.

hr@HX> drop table t;

Table dropped.

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE from RECYCLEBIN;

OBJECT_NAME                   |ORIGINAL_NAME                   |OPERATION|TYPE                     |     SPACE

------------------------------|--------------------------------|---------|-------------------------|----------

BIN$7hpJclDOBi3gQwEAAH+23A==$0|T                               |DROP     |TABLE                    |       256

BIN$7hpJclDNBi3gQwEAAH+23A==$0|IDX_T_ID                        |DROP     |INDEX                    |        40

2 rows selected.

1.回收站功能啟動和關閉

回收站功能受引數recyclebin影響,預設是開啟的

hr@HX> show parameter recyclebin;

NAME                                |TYPE       |VALUE

------------------------------------|-----------|------------------------------

recyclebin                          |string     |on

可以在會話或者系統級別開啟或者關閉回收站功能:

ALTER SYSTEM SET recyclebin = ON;      

ALTER SESSION SET recyclebin = ON;      

ALTER SYSTEM SET recyclebin = OFF;      

ALTER SESSION SET recyclebin = OFF;   

獲取回收站裡的內容

SELECT * FROM RECYCLEBIN;      

SELECT * FROM USER_RECYCLEBIN;      

SELECT * FROM DBA_RECYCLEBIN; 

2.從回收站還原表:  

刪除表後,可以利用回收站還原:

FLASHBACK TABLE <> TO BEFORE DROP RENAME TO <>;  

這裡的RENAME是給刪除的物件進行重新命名,可選:

hr@HX> flashback table t to before drop rename to t1;  

Flashback complete.

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE from RECYCLEBIN;

no rows selected

恢復後索引仍然可以使用:

hr@HX> select index_name,table_name,status from user_indexes  where table_name='T1';

INDEX_NAME               |TABLE_NAME     |STATUS

-------------------------|---------------|--------

BIN$7hpJclDNBi3gQwEAAH+23|T1             |VALID

A==$0                    |               |

1 row selected.

hr@HX> select count(object_id) from t1;

COUNT(OBJECT_ID)

----------------

15596

1 row selected.

Execution Plan

----------------------------------------------------------

Plan hash value: 572134920

--------------------------------------------------------------------------------------------------------

| Id  | Operation             | Name                           | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT      |                                |     1 |    13 |    12   (0)| 00:00:01 |

|   1 |  SORT AGGREGATE       |                                |     1 |    13 |            |          |

|   2 |   INDEX FAST FULL SCAN| BIN$7hpJclDNBi3gQwEAAH+23A==$0 | 18409 |   233K|    12   (0)| 00:00:01 |

--------------------------------------------------------------------------------------------------------

3. 清空回收站

清空回收站包含兩種情況,有條件清空和全部清空

(1)清空特定的表:

PURGE TABLE <>; 

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE from RECYCLEBIN;

OBJECT_NAME                   |ORIGINAL_NAME                   |OPERATION|TYPE           |  SPACE

------------------------------|--------------------------------|---------|---------------|----------

BIN$7hpJclDQBi3gQwEAAH+23A==$0|T1                              |DROP     |TABLE          |    256

BIN$7hpJclDPBi3gQwEAAH+23A==$1|BIN$7hpJclDNBi3gQwEAAH+23A==$0  |DROP     |INDEX          |     40

2 rows selected.

hr@HX> purge table t1;

Table purged.

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE from RECYCLEBIN;

no rows selected

和表相關的物件會被一併清除

(2)清空一個特定的索引:

PURGE INDEX <>; 

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE from RECYCLEBIN;

OBJECT_NAME                   |ORIGINAL_N|OPERATION|TYPE      |     SPACE

------------------------------|----------|---------|----------|----------

BIN$7hpJclDSBi3gQwEAAH+23A==$0|T         |DROP     |TABLE     |       256

BIN$7hpJclDRBi3gQwEAAH+23A==$0|IDX_T_ID  |DROP     |INDEX     |        40

2 rows selected.

hr@HX> purge index idx_t_id;

Index purged.

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE from RECYCLEBIN;

OBJECT_NAME                   |ORIGINAL_N|OPERATION|TYPE      |     SPACE

------------------------------|----------|---------|----------|----------

BIN$7hpJclDSBi3gQwEAAH+23A==$0|T         |DROP     |TABLE     |       256

1 row selected.

(3)清空與該表空間有關聯的物件:

PURGE TABLESPACE <>; 

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE,ts_name from RECYCLEBIN;

OBJECT_NAME                   |ORIGINAL_N|OPERATION|TYPE      |     SPACE|TS_NAME

------------------------------|----------|---------|----------|----------|------------------------------

BIN$7hpJclDSBi3gQwEAAH+23A==$0|T         |DROP     |TABLE     |       256|HR

1 row selected.

hr@HX> purge tablespace hr;

Tablespace purged.

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE,ts_name from RECYCLEBIN;

no rows selected

(4)清空一個表空間中特定使用者的資訊:

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE,ts_name from RECYCLEBIN;

OBJECT_NAME                   |ORIGINAL_N|OPERATION|TYPE      |     SPACE|TS_NAME

------------------------------|----------|---------|----------|----------|---------------

BIN$7hpJclDWBi3gQwEAAH+23A==$0|T         |DROP     |TABLE     |       256|HR

BIN$7hpJclDVBi3gQwEAAH+23A==$0|IDX_T_ID  |DROP     |INDEX     |       256|HR_20M

2 rows selected.

hr@HX> purge tablespace hr_20m user hr;

Tablespace purged.

hr@HX> select object_name,ORIGINAL_NAME,OPERATION,TYPE,SPACE,ts_name from RECYCLEBIN;

OBJECT_NAME                   |ORIGINAL_N|OPERATION|TYPE      |     SPACE|TS_NAME

------------------------------|----------|---------|----------|----------|----------------

BIN$7hpJclDWBi3gQwEAAH+23A==$0|T         |DROP     |TABLE     |       256|HR

1 row selected.

(5)清空回收站(比較常用):

PURGE RECYCLEBIN; 

(6)刪除表時直接清除回收站資訊:

DROP TABLE <> PURGE; 

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

相關文章