11G新特性:FLASHBACK ARCHIVE

liglewang發表於2011-12-10

11G新特性:FLASHBACK ARCHIVE

閃回資料歸檔(FLASHBACK ARCHIVE)與UNDO表空間是沒有任何關係的,並且與flashback logs(開啟資料庫閃回生成的日誌)也無關。

閃回資料歸檔需要單獨為其建立相應的表空間(falshback tablespace)用於儲存對指定表修改的歷史資料。

下面演示針對DML操作的閃回查詢

建立FLASHBACK ARCHIVE

SQL> create flashback archive default tbs_archive01
2 tablespace tbs_ht02
3 retention 1 day;
Flashback archive created.

查詢ligle.t01表資料

SQL> col owner for a10
SQL> col object_name for a20
SQL> col object_type for a20
SQL> select owner,object_id,object_name,object_type from ligle.t01 where owner='SCOTT'
OWNER OBJECT_ID OBJECT_NAME OBJECT_TYPE
---------- ---------- --------------------
-------------------
SCOTT 75334 PK_DEPT INDEX
SCOTT 75333 DEPT TABLE
SCOTT 75335 EMP TABLE
SCOTT 75336 PK_EMP INDEX
SCOTT 75337 BONUS TABLE
SCOTT 75338 SALGRADE TABLE
刪除一條資料

SQL> delete from t01 where object_id=75334;
1 row deleted.
SQL> commit;
Commit complete.

再次查詢ligle.t01表資料

SQL> select owner,object_id,object_name,object_type from ligle.t01 where owner='SCOTT';
OWNER OBJECT_ID OBJECT_NAME OBJECT_TYPE
---------- ---------- -------------------- --------------------
SCOTT 75333 DEPT TABLE
SCOTT 75335 EMP TABLE
SCOTT 75336 PK_EMP INDEX
SCOTT 75337 BONUS TABLE
SCOTT 75338 SALGRADE TABLE

可以發現OBJECT_ID75334的資料已被刪除。

透過FLASHBACK ARCHIVE查詢過去某時刻該表的資料:

SQL> select owner,object_id,object_name,object_type from t01 as of timestamp (systimestamp-interval '10' minute) where owner='SCOTT';
OWNER OBJECT_ID OBJECT_NAME OBJECT_TYPE
---------- ---------- -------------------- --------------------
SCOTT 75334 PK_DEPT INDEX
SCOTT 75333 DEPT TABLE
SCOTT 75335 EMP TABLE
SCOTT 75336 PK_EMP INDEX
SCOTT 75337 BONUS TABLE
SCOTT 75338 SALGRADE TABLE
6 rows selected.

我們知道,在Oracle 11g以前,透過閃回查詢也可以查出過去某時刻的資料狀態(該方法的前提是UNDO表空間必須還存在該表被修改時的undo資料,否則將會報ORA-01555),那麼比較細心的朋友會發現,在上面的閃迴歸檔查詢中是不是也是用的UNDO資料呢?

可以透過切換當前的undo表空間來實驗下:

SQL> conn /as sysdba
SQL> create undo tablespace UNDOTBS2 datafile '/soft/oradata/horizon/undotbs02.dbf' size 10M;
SQL> alter system set undo_tablespace='UNDOTBS2';
SQL> conn ligle/ligle

Connected.
SQL> select owner,object_id,object_name,object_type from t01 as of timestamp (systimestamp-interval '20' minute) where owner='SCOTT';
OWNER OBJECT_ID OBJECT_NAME OBJECT_TYPE
---------- ---------- -------------------- --------------------
SCOTT 75334 PK_DEPT INDEX
SCOTT 75333 DEPT TABLE
SCOTT 75335 EMP TABLE
SCOTT 75336 PK_EMP INDEX
SCOTT 75337 BONUS TABLE
SCOTT 75338 SALGRADE TABLE

至此,可以明白,在當前UNDO表空間已被切換的情況下,依然可以查詢到過去某時刻的表資料狀態,所以可以證明FLASHBACK ARCHIVEUNDO表空間是沒有關係的。

透過FLASHBACK ARCHIVE恢復表資料到某時刻:

SQL> alter table t01 enable row movement;
Table altered.
SQL> flashback table t01 to timestamp (systimestamp - interval '40' minute);
Flashback complete.
SQL> select owner,object_id,object_name,object_type from t01 where owner='SCOTT';
OWNER OBJECT_ID OBJECT_NAME OBJECT_TYPE
---------- ---------- -------------------- --------------------
SCOTT 75333 DEPT TABLE
SCOTT 75335 EMP TABLE
SCOTT 75336 PK_EMP INDEX
SCOTT 75337 BONUS TABLE
SCOTT 75338 SALGRADE TABLE
SCOTT 75334 PK_DEPT INDEX
6 rows selected.

透過建立FLASHBACK ARCHIVE表空間來儲存指定表的變更歷史資料,避免了對UNDO表空間的過度依賴。

[@more@]

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

相關文章