雖然ORA-01555錯誤能夠經過種種手段來避免和減小,可是隨着時間的流逝,這些UNDO信息總會失去,那麼可否將這些信息保存起來,使得數據庫在必定的歷史週期以內能夠不斷向後追溯,使得咱們能夠看到一個數據表在任意歷史時間點上的切片呢? 算法
從Oracle Database 11g開始,Oracle 提供了一個這樣的功能:閃回數據歸檔(Flashback Data Archive)。經過這一功能Oracle數據庫能夠將UNDO數據進行歸檔,從而提供全面的歷史數據查詢,也所以Oracle引入一個新的概念Oracle Total Recall,也即Oracle全面回憶功能。閃回數據歸檔能夠和咱們一直熟悉的日誌歸檔類比,日誌歸檔記錄的是Redo的歷史狀態,用於保證恢復的連續性;而閃迴歸檔記錄的是UNDO的歷史狀態,能夠用於對數據進行閃回追溯查詢;後臺進程LGWR用於將Redo信息寫出到日誌文件,ARCH進程負責進行日誌歸檔;在Oracle 11g中,新增的後臺進程FBDA(Flashback Data Archiver Process)則用於對閃回數據進行歸檔寫出: 數據庫
[oracle@sp3: ~]$ps -ef | grep fbda | grep -v grep
oracle 3251 1 0 Jan07 ? 00:00:11 ora_fbda_ccdb oracle
閃迴歸檔數據甚至能夠以年爲單位進行保存,Oracle能夠經過內部分區和壓縮算法減小空間耗用,這一特性對於須要審計以及歷史數據分區的環境尤爲有用,可是注意,對於繁忙的數據庫環境,閃回數據存儲顯然要耗用更多的存儲空間。固然,用戶能夠根據須要,對部分表進行閃回數據歸檔,從而知足特定的業務需求。 ide
由於閃回數據歸檔須要獨立的存儲,因此在使用該特性以前須要建立獨立的ASSM(自動段空間管理)表空間: oop
sys@TQGZS11G> create tablespace fbda datafile '/oracle/oradata/tqgzs11g/FBDA.dbf' size 200M segment space management auto;
Tablespace created. 測試
而後能夠基於該表空間建立閃回數據歸檔區,FLASHBACK ARCHIVE ADMINISTER系統權限是建立閃回數據存檔所必需的,此處使用SYS用戶進行: this
sys@TQGZS11G> create flashback archive fda tablespace fbda retention 1 month;
Flashback archive created. spa
此後就可使用該歸檔區來記錄數據表的閃回數據量。爲了測試方便,先將UNDO表空間更改成較小,以使得UNDO數據可以儘快老化: 日誌
sys@TQGZS11G> create undo tablespace UNDOTBS2_SMALL datafile '/oracle/oradata/tqgzs11g/UNDOTBS2_SMALL.dbf' size 20M autoextend off;
Tablespace created.
sys@TQGZS11G> alter system set undo_tablespace= UNDOTBS2_SMALL;
System altered.
sys@TQGZS11G> show parameter undo
NAME TYPE VALUE
------------------------------------ ------------------------------ ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS2_SMALL orm
接下來使用測試用戶鏈接,對測試表執行閃迴歸檔設置,FLASHBACK ARCHIVE對象權限是啓用歷史數據跟蹤所必需的:
sys@TQGZS11G> conn tq/tq
Connected.
tq@TQGZS11G> select TABLE_NAME from user_tables;
TABLE_NAME
--------------------
T
EMP
tq@TQGZS11G> alter table t flashback archive fda;
Table altered.
取消對於數據表的閃迴歸檔可使用以下命令:
alter table table_name no flashback archive;
接下來記錄一下SCN,從數據庫表中刪除部分數據:
tq@TQGZS11G> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
1142115
tq@TQGZS11G> select count(*) from t;
COUNT(*)
----------
69266
tq@TQGZS11G> delete from t where rownum < 1001;
1000 rows deleted.
tq@TQGZS11G> commit;
Commit complete.
如今執行閃回查詢,則數據來自UNDO表空間:
tq@TQGZS11G> select count(*) from t as of scn 1142115;
COUNT(*)
----------
69266
Execution Plan
----------------------------------------------------------
Plan hash value: 2966233522
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 1048 (1)| 00:00:13 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T | 69266 | 1048 (1)| 00:00:13 |
-------------------------------------------------------------------
接下來執行一小段批量循環代碼,使UNDO數據老化覆蓋:
tq@TQGZS11G> begin
2 for i in 1 .. 100 loop
3 delete from t where rownum < 31;
4 commit;
5 end loop;
6 end;
7 /
PL/SQL procedure successfully completed.
如今來看一下閃回數據歸檔發揮做用的閃回查詢,經過執行計劃可以看到和以前查詢執行方式的不一樣:
tq@TQGZS11G> select count(*) from t as of scn 1142115;
COUNT(*)
----------
69266
Execution Plan
----------------------------------------------------------
Plan hash value: 1421074822
----------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
----------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 1151 (1)| 00:00:14 | | |
| 1 | SORT AGGREGATE | | 1 | | | | | |
| 2 | VIEW | | 20633 | | 1151 (1)| 00:00:14 | | |
| 3 | UNION-ALL | | | | | | | |
| 4 | PARTITION RANGE SINGLE| | 20295 | 515K| 69 (2)| 00:00:01 | 1 | 1 |
|* 5 | TABLE ACCESS FULL | SYS_FBA_HIST_72661 | 20295 | 515K| 69 (2)| 00:00:01 | 1 | 1 |
|* 6 | FILTER | | | | | | | |
|* 7 | HASH JOIN OUTER | | 338 | 669K| 1081 (1)| 00:00:13 | | |
|* 8 | TABLE ACCESS FULL | T | 3463 | 41556 | 1048 (1)| 00:00:13 | | |
| 9 | VIEW | | 19522 | 37M| 32 (0)| 00:00:01 | | |
|* 10 | TABLE ACCESS FULL | SYS_FBA_TCRV_72661 | 19522 | 37M| 32 (0)| 00:00:01 | | |
----------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
5 - filter("ENDSCN">1142115 AND "ENDSCN"<=1145328 AND ("STARTSCN" IS NULL OR "STARTSCN"<=1142115))
6 - filter("F"."STARTSCN"<=1142115 OR "F"."STARTSCN" IS NULL)
7 - access("T".ROWID=CHARTOROWID("F"."RID"(+)))
8 - filter("T"."VERSIONS_STARTSCN" IS NULL)
10 - filter(("ENDSCN" IS NULL OR "ENDSCN">1145328) AND ("STARTSCN" IS NULL OR "STARTSCN"<1145328))
Note
-----
- dynamic sampling used for this statement
經過以上執行計劃能夠看到,查詢閃回來自SYS_FBA_TCRV_72661系統表,該表隸屬於閃迴歸檔表空間,用於記錄閃回數據:
tq@TQGZS11G> desc SYS_FBA_TCRV_72661
Name Null? Type
----------------- -------- ---------------------
RID VARCHAR2(4000)
STARTSCN NUMBER
ENDSCN NUMBER
XID RAW(8)
OP VARCHAR2(1)
tq@TQGZS11G> select count(*) from SYS_FBA_TCRV_72661;
COUNT(*)
----------
18511
閃回功能生成的字典對象有多個,經過查詢USER_TABLES/USER_OBJECTS視圖能夠得到這些對象的詳細信息:
tq@TQGZS11G> select table_name,tablespace_name from user_tables where table_name like '%FBA%';
TABLE_NAME TABLESPACE_NAME
------------------------------ --------------------
SYS_FBA_DDL_COLMAP_72661 FBDA
SYS_FBA_TCRV_72661 FBDA
SYS_FBA_HIST_72661
tq@TQGZS11G> select object_name,object_type from user_objects where object_name like '%FBA%';
OBJECT_NAME OBJECT_TYPE
------------------------------ --------------------
SYS_FBA_DDL_COLMAP_72661 TABLE
SYS_FBA_HIST_72661 TABLE
SYS_FBA_HIST_72661 TABLE PARTITION
SYS_FBA_TCRV_72661 TABLE
SYS_FBA_TCRV_IDX_72661 INDEX
能夠經過數據字典視圖來查看關於閃迴歸檔表的記錄:
tq@TQGZS11G> select * from user_flashback_archive_tables;
TABLE_NAME OWNER_NAME FLASHBACK_ARCHIVE_NAME ARCHIVE_TABLE_NAME
---------- ---------- ------------------------------ --------------------
T TQ FDA SYS_FBA_HIST_72661
能夠經過dict字典查詢和閃迴歸檔有關的數據字典表:
sys@TQGZS11G> select table_name from dict where table_name like '%FLASHBACK_ARCHIVE%';
TABLE_NAME
-----------------------------------
DBA_FLASHBACK_ARCHIVE
DBA_FLASHBACK_ARCHIVE_TABLES
DBA_FLASHBACK_ARCHIVE_TS
USER_FLASHBACK_ARCHIVE
USER_FLASHBACK_ARCHIVE_TABLES
總之,閃回數據歸檔是Oracle 11g提供的重要加強之一,經過合理使用這一加強,能夠爲數據庫提供更爲全面的數據生命週期管理,Oracle關於UNDO技術的進化至此又邁進了重要的一步。
- The End -
簡單整理後的主要步驟以下:
oracle11g 啓動閃回功能
一、建立獨立的ASSM(自動段空間管理)表空間。
create tablespace fbda datafile'/oracle/oradata/orcl/FBDA.dbf' size 1000M segment space management auto;
二、在獨立的ASSM上建立閃回數據歸檔區。
create flashback archive fda tablespace fbda retention 1 month;
三、對指定的數據表執行閃回數據歸檔設置。
alter table t flashback archive fda;
四、取消對於表數據的閃迴歸檔設置
alter table table_name no flashback archive;
經過閃回功能找回刪除的數據 select dbms_flashback.get_system_change_number from dual; select count(*) from t as of scn 1142115