oracle 11g 新特性 Flashback Data Archive 說明

roominess發表於2012-05-08

Oracle 11g裡又出了一個新特性:Oracle Flashback Data Archive.  11g的官方文件裡搜到了相關內容說明,參考:

            Using Oracle Flashback Technology

                        --Using Flashback Data Archive (Oracle Total Recall)

            http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17125/adfns_flashback.htm#BJFIEJGG

 

 

 

.  Flashback Data Archive 說明

 

官網的定義如下:

            A Flashback Data Archive provides the ability to track and store transactional changes to a table over its lifetime. A Flashback Data Archive is useful for compliance with record stage policies and audit reports.

            --Flashback Data Archive 在它的有效期內將儲存事務改變的資訊。

            A Flashback Data Archive consists of one or more tablespaces or parts thereof. You can have multiple Flashback Data Archives. If you are logged on as SYSDBA, you can specify a default Flashback Data Archive for the system. A Flashback Data Archive is configured with retention time. Data archived in the Flashback Data Archive is retained for the retention time.

            -- FDA 包含一個或者多個表空間,我們可以建立多個FDA 當以sysdba 登陸時,可以指定default FDA

            By default, flashback archiving is off for any table. You can enable flashback archiving for a table if all of these conditions are true:

            -- 預設情況下,FDA 是關閉的,當具備一下條件時,我們可以啟用FDA

 

(1).  You have the FLASHBACK ARCHIVE object privilege on the Flashback Data Archive to use for that table.

(2).  The table is neither nested, clustered, temporary, remote, or external.

(3).  The table contains neither LONG nor nested columns.

 

            After flashback archiving is enabled for a table, you can disable it only if you either have the FLASHBACK ARCHIVE ADMINISTER system privilege or you are logged on as SYSDBA.

            --FDA 啟動以後,只有具有FLASHBACK ARCHIVE ADMINISTER 許可權的使用者或者用SYSDBA登陸的使用者才可以禁用FDA

 

            When choosing a Flashback Data Archive for a specific table, consider the data retention requirements for the table and the retention times of the Flashback Data Archives on which you have the FLASHBACK ARCHIVE object privilege.

 

給使用者賦:

SQL> create user dvd identified by dvd default tablespace users temporary tablespace temp;

User created.

 

SQL> grant resource,connect to dvd;

Grant succeeded.

 

SQL> grant flashback archive administer to dvd;

Grant succeeded.

 

SQL> select * from dba_sys_privs where grantee='DVD';

 

GRANTEE        PRIVILEGE                            ADM

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

DVD            FLASHBACK ARCHIVE ADMINISTER       NO

DVD            UNLIMITED TABLESPACE                NO

 

 

          Oracle 10g中的lashback 包括: flashback version queryflashback transaction queryflashback databaseflashback tableflashback drop等特性。

            在這些閃回技術當中,除了Flashback Database(依賴於閃回日誌)之外,其他的閃回技術都是依賴於Undo撤銷資料,都與資料庫初始化引數UNDO_RETENTION密切相關。

            它們是從撤銷資料中讀取資訊來構造舊資料的。這樣就有一個限制,就是undo中的資訊不能被覆蓋。而undo段是迴圈使用的,只要事務提交,之前的undo資訊就可能被覆蓋,雖然可以通過 undo_retention等引數來延長undo的存活期,但這個引數會影響所有的事務,設定過大,可能導致undo tablespace快速膨脹。

 

            Oracle 11gflashback增加了:Flashback Data Archive 特性。該技術與之前的Flashback的實現機制不同,通過將變化資料另外儲存到建立的閃迴歸檔區(Flashback Archive)中,以和undo區別開來,這樣就可以為閃迴歸檔區單獨設定儲存策略,使之可以閃回到指定時間之前的舊資料而不影響undo策略。並且可以根據需要指定哪些資料庫物件需要儲存歷史變化資料,而不是將資料庫中所有物件的變化資料都儲存下來,這樣可以極大地減少空間需求。

            Flashback Data Archive並不是記錄資料庫的所有變化,而只是記錄了指定表的資料變化。所以,Flashback Data Archive是針對物件的保護,是Flashback Database的有力補充。

 

            通過Flashback Data Archive,可以查詢指定物件的任何時間點(只要滿足保護策略)的資料,而且不需要用到undo,這在有審計需要的環境,或者是安全性特別重要的高可用資料庫中,是一個非常好的特性。缺點就是如果該表變化很頻繁,對空間的要求可能很高。

 

閃回資料歸檔區

            閃回資料歸檔區是閃回資料歸檔的歷史資料儲存區域,在一個系統中,可以有一個預設的閃回資料歸檔區,也可以建立其他許多的閃回資料歸檔區域。

            每一個閃回資料歸檔區都可以有一個唯一的名稱。同時,每一個閃回資料歸檔區都對應了一定的資料保留策略。

            例如可以配置歸檔區FLASHBACK_DATA_ARCHIVE_1中的資料保留期為1年,而歸檔區FLASHBACK_DATA_ARCHIVE_2的資料保留期為2天或者更短。 以後如果將表放到對應的閃回資料歸檔區,則就按照該歸檔區的保留策略來儲存歷史資料。

            閃回資料歸檔區是一個邏輯概念,是從一個或者多個表空間中拿出一定的空間,來儲存表的修改歷史,這樣就擺脫了對Undo撤銷資料的依賴,不利用undo就可以閃回到歸檔策略內的任何一個時間點上。

 

Flashback archive相關資料字典

*_FLASHBACK_ARCHIVE

Displays information about Flashback Data Archive files.

*_FLASHBACK_ARCHIVE_TS

Displays tablespaces of Flashback Data Archive files.

*_FLASHBACK_ARCHIVE_TABLES

Displays information about tables that are enabled for Data Flashback Archive files.

 

代表DBA 或者User

 

Flashback archive的後臺程式

            Oracle11gFlashback data archive特性專門引入了一個新的後臺程式FBDA,用於將追蹤表(traced table,也就是將指定使用flashback data archivetable)的歷史變化資料轉存到閃迴歸檔區。


SQL> select name,description from v$bgprocess where name='FBDA';

NAME  DESCRIPTION

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

FBDA  Flashback Data Archiver Process

 

 

 

Flashback archive 的限制條件

            1Flashback data archive只能在ASSMtablespace上建立
            
2Flashback data archive要求必須使用自動undo管理,

                         undo_management 引數為auto

 

.  Flashback Data Archive 的相關操作

2.1 Creating a Flashback Data Archive

            Create a Flashback Data Archive with the CREATE FLASHBACK ARCHIVE statement, specifying:

            1Name of the Flashback Data Archive

2Name of the first tablespace of the Flashback Data Archive

3(Optional) Maximum amount of space that the Flashback Data Archive can use in the first tablespaceThe default is unlimited. Unless your space quota on the first tablespace is also unlimited, you must specify this value; otherwise, error ORA-55621 occurs.

4Retention time (number of days that Flashback Data Archive data for the table is guaranteed to be stored)

 

--  建立FDA 時,可以指定以上4個引數,沒有沒有執行Flashback Archive 的配額,預設為 unlimited 這裡的配額,只的是使用者對錶空間的配額。

 

            If you are logged on as SYSDBA, you can also specify that this is the default Flashback Data Archive for the system. If you omit this option, you can still make this Flashback Data Archive the default later .

            -- 如果以SYSDBA 登陸,還可以指定default Flashback Data Archive 如果沒有指定,也可以通過alter flashback archive 命令來指定。

 

示例:

 

1先建立幾個測試的表空間

 

SQL> create tablespace FDA1 datafile 'D:/APP/ADMINISTRATOR/ORADATA/NEWCCS/FDA01.dbf' size 100M;

Tablespace created.

 

SQL> create tablespace FDA2 datafile 'D:/APP/ADMINISTRATOR/ORADATA/NEWCCS/FDA02.dbf' size 100M;

Tablespace created.

 

SQL> create tablespace FDA3 datafile 'D:/APP/ADMINISTRATOR/ORADATA/NEWCCS/FDA03.dbf' size 100M;

Tablespace created.

 

SQL> create tablespace FDA4 datafile 'D:/APP/ADMINISTRATOR/ORADATA/NEWCCS/FDA04.dbf' size 100M;

Tablespace created.

 

2建立一個預設的Flashback Archive, 配額為10M,資料保留期為1

 

SQL> create flashback archive default fla1 tablespace fda1 quota 10M retention 1 year;

 

預設的Flashback Archive 只能有一個:

SQL> create flashback archive default fla3 tablespace fda1 quota 10M retention 1 year;

create flashback archive default fla3 tablespace fda1 quota 10M retention 1 year

                                                 *

ERROR at line 1:

ORA-55609: Attempt to create duplicate default Flashback Archive

 

這裡報錯了,我們可以是使用alter flashback 來修改預設的Flashback Archive.

 

3  建立一個Flashback Archive fla2,使用預設配額unlimited retention 年。

 

SQL> create flashback archive fla2 tablespace fda2 retention 2 year;

Flashback archive created.

 

            根據官網的說法,這種情況下,使用者對該表空間的配額也必須為ulimited 否則就會報錯ORA-55621

 

測試一下:

 

SQL> conn / as sysdba;

Connected.

 

SQL> revoke unlimited tablespace from dvd;

Revoke succeeded.

 

SQL> alter user dvd quota 10m on fda4;

User altered.

 

SQL> conn dvd/dvd;

Connected.

 

SQL> create flashback archive fla5 tablespace fda4 retention 1 day;

create flashback archive fla5 tablespace fda4 retention 1 day

                                         *

ERROR at line 1:

ORA-55621: User quota on tablespace "FDA4" is not enough for Flashback Archive

 

報錯。

 

修改使用者的配合,在建立,成功:

 

SQL> conn / as sysdba;

Connected.

 

SQL> grant unlimited tablespace to dvd;

Grant succeeded.

 

SQL> conn dvd/dvd;

Connected.

 

SQL> create flashback archive fla5 tablespace fda4 retention 1 day;

Flashback archive created.

 

2.2  Altering a Flashback Data Archive

With the ALTER FLASHBACK ARCHIVE statement, you can:

-- 使用alter flashback archive 可以修改如下內容:

            1Change the retention time of a Flashback Data Archive

            2Purge some or all of its data

            3Add, modify, and remove tablespaces

 

Note:

            Removing all tablespaces of a Flashback Data Archive causes an error.

            If you are logged on as SYSDBA, you can also use the ALTER FLASHBACK ARCHIVE statement to make a specific file the default Flashback Data Archive for the system.

            -- 不能移除Flashback Data Archive裡的所有表空間。 否則報錯。 如果用sysdba 登陸,可以修改預設的Flashback archive

 

示例:

2.2.1  Flashback Data Archive 修改為default FA

 

先用我們具有flashback archive administer 許可權的使用者試試:

SQL> conn dvd/dvd;

Connected.

SQL> alter flashback archive fla1 set default;

alter flashback archive fla1 set default

*

ERROR at line 1:

ORA-55611: No privilege to manage default Flashback Archive

 

報錯,沒有許可權,用sysdba 測試成功:

SQL> conn / as sysdba;

Connected.

SQL> alter flashback archive fla1 set default;

 

Flashback archive altered.

 

注意一點,只能有一個預設的Flashback archive.

 

SQL> select flashback_archive_name name, status  from dba_flashback_archive;

NAME       STATUS

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

FLA1       DEFAULT

FLA2

 

當前預設的Flashback Archive FLA1,我們將預設改成FLA2,在檢視:

SQL> alter flashback archive fla2 set default;

Flashback archive altered.

 

SQL>  select flashback_archive_name name, status  from dba_flashback_archive;

NAME       STATUS

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

FLA1

FLA2       DEFAULT

 

2.2.2  為已經存在的Flashback Archive 新增表空間,並指定配額

 

SQL> alter flashback archive fla1 add tablespace fda3 quota 20M;

Flashback archive altered.

 

2.2.3 為已經存在的Flashback Archive 新增表空間,不指定配額,即需要多少用多少空間

 

SQL>  alter flashback archive fla1 add tablespace fda4;

Flashback archive altered.

 

2.2.4  修改已經存在的Flashback Archive的配額

SQL> alter flashback archive fla1 modify tablespace fda1 quota 20m;

Flashback archive altered.

 

2.2.5  修改配額不受限制

SQL> alter flashback archive fla1 modify tablespace fda1;

Flashback archive altered.

 

2.2.6 修改Flashback Archive retention time

SQL> alter flashback archive fla1 modify retention 2 year;

Flashback archive altered.

 

SQL> alter flashback archive fla1 modify retention 1 month;

Flashback archive altered.

 

SQL> alter flashback archive fla1 modify retention 2 month;

Flashback archive altered.

 

SQL> alter flashback archive fla1 modify retention 2 day;

Flashback archive altered.

 

SQL> alter flashback archive fla1 modify retention 1 day;

Flashback archive altered.

 

2.2.7  將表空間從Flashback Archive中移除

 

SQL> alter flashback archive fla1 remove tablespace fda4;

Flashback archive altered.

 

-- 注意,這裡移除的僅僅是Flashback Archive中的資訊,表空間不會被刪除。

 

2.2.8  清空Flashback Archive中的所有歷史記錄

 

SQL> alter flashback archive fla1 purge all;

Flashback archive altered.

 

2.2.9 清空Flashback Archive 中超過1天的歷史資料

SQL> alter flashback archive fla1 purge before timestamp (systimestamp - interval '1' day);

Flashback archive altered.

 

2.2.10  清空Flashback Archive 中指定SCN 之前的所有歷史資料

SQL> select current_scn from v$database;

CURRENT_SCN

-----------

 1315755078

 

SQL> alter flashback archive fla1 purge before scn  1315755078;

Flashback archive altered.

 

我這裡只是演示一個SCN。 具體要結合自己的情況。

 

2.3  Dropping a Flashback Data Archive

            Drop a Flashback Data Archive with the DROP FLASHBACK ARCHIVE statement. Dropping a Flashback Data Archive deletes its historical data, but does not drop its tablespaces.

--  刪除Flashback Archive 不會刪除相應的表空間

 

示例:

SQL> DROP FLASHBACK ARCHIVE fla2;

Flashback archive dropped.

 

SQL>  select flashback_archive_name name, status  from dba_flashback_archive;

NAME       STATUS

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

FLA1

 

2.4  Specifying the Default Flashback Data Archive

            By default, the system has no default Flashback Data Archive. If you are logged on as SYSDBA, you can specify default Flashback Data Archive in either of these ways:

            預設情況下,沒有default Flashback Data Archive. 當以sysdba 登陸之後,就可以指定它。

 

2.4.1 修改已經存在的Flashback Archive default

SQL> alter flashback archive fla1 set default;

Flashback archive altered.

 

SQL> alter flashback archive fla10 set default;

alter flashback archive fla10 set default

*

ERROR at line 1:

ORA-55605: Incorrect Flashback Archive is specified

 

如果指定的Flashback 不存在,就報錯。

 

2.4.2 在建立Flashback Data Archive 時,指定default

SQL>create flashback archive default fla2 tablespace tbs1 quota 10m retention 1 year;

 

            The default Flashback Data Archive for the system is the default Flashback Data Archive for every user who does not have his or her own default Flashback Data Archive.

 

 

2.5  Enabling and Disabling Flashback Data Archive

            By default, flashback archiving is disabled for any table. You can enable flashback archiving for a table if you have the FLASHBACK ARCHIVE object privilege on the Flashback Data Archive to use for that table.

            預設情況下,所有表都沒有啟動flashback archive

 

            To enable flashback archiving for a table, include the FLASHBACK ARCHIVE clause in either the CREATE TABLE or ALTER TABLE statement.

            In the FLASHBACK ARCHIVE clause, you can specify the Flashback Data Archive where the historical data for the table are stored. The default is the default Flashback Data Archive for the system. If you specify a nonexistent Flashback Data Archive, an error occurs.

 

            If you enable flashback archiving for a table, but AUM(automatic undo managed) is disabled, error ORA-55614 occurs when you try to modify the table.

 

            If a table has flashback archiving enabled, and you try to enable it again with a different Flashback Data Archive, an error occurs

 

            After flashback archiving is enabled for a table, you can disable it only if you either have the FLASHBACK ARCHIVE ADMINISTER system privilege or you are logged on as SYSDBA.

            To disable flashback archiving for a table, specify NO FLASHBACK ARCHIVE in the ALTER TABLE statement. (It is unnecessary to specify NO FLASHBACK ARCHIVE in the CREATE TABLE statement, because that is the default.)

 

 

示例:

2.5.1 建立table,使用預設的Flashback Data Archive      來儲存歷史資料

SQL> create table 安慶 (id number) flashback archive;

Table created.

 

2.5.2 建立table,使用指定的Flashback Data Archive 來儲存歷史資料

 

SQL> create table 懷寧 (id number) flashback archive fla1;

Table created.

 

2.5.3 對錶啟用Flashback archive,並使用預設的Flashback archive

 

SQL> alter table dave flashback archive;

Table altered.

 

2.5.4 禁用表的Flashback Archive

SQL> alter table dave no flashback archive;

Table altered.

 

2.5.5 table 啟用Flashback archive,並指定Flashaback Archive 區。

SQL> alter table dave flashback archive fla1;

Table altered.

 

 

2.6 DDL Statements on Tables Enabled for Flashback Data Archive

            Flashback Data Archive supports many DDL statements, including some that alter the table definition or move data. For example:

            --啟動Flashback Data Archive的表支援以下的DDL 操作

            1ALTER TABLE statement that does any of the following:

                                    1Adds, drops, renames, or modifies a column

                                    2Adds, drops, or renames a constraint

                                    3Drops or truncates a partition or subpartition operation

            2TRUNCATE TABLE statement

            3RENAME statement that renames a table

 

            Some DDL statements cause error ORA-55610 when used on a table enabled for Flashback Data Archive. For example:

            -- 啟動Flashback Data Archive的表上的一些DDL 操作可能觸發ORA-55610的錯誤,這些DDL 如下:

            1ALTER TABLE statement that includes an UPGRADE TABLE clause, with or without an INCLUDING DATA clause

            3ALTER TABLE statement that moves or exchanges a partition or subpartition operation

            3DROP TABLE statement

 

 

            If you must use unsupported DDL statements on a table enabled for Flashback Data Archive, use the DBMS_FLASHBACK_ARCHIVE.DISASSOCIATE_FBA procedure to disassociate the base table from its Flashback Data Archive.

            To reassociate the Flashback Data Archive with the base table afterward, use the DBMS_FLASHBACK_ARCHIVE.REASSOCIATE_FBA procedure.

 

            -- 如果必須在已經啟用Flashback Archive的表上執行這些不支援的DDL 操作,可以用DBMS_FLASHBACK_ARCHIVE 包將表從Flashback Data Archive 分離出來,待操作結束後在新增進去。

 

            The DBMS_FLASHBACK_ARCHIVE package contains two simple procedures for disassociation and reassociation of a Flashback Data Archive (FDA) enabled table from/with its underlying FDA respectively.

 

            Flashback Area中,會有一張歷史表記錄著我們啟動FA表的所有操作。 我們可以通過如下SQL 來檢視他們之間的對映關係。

 

SQL> SELECT table_name,archive_table_name,status from dba_flashback_archive_tables;

 

TABLE_NAME ARCHIVE_TABLE_NAME   STATUS

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

ANQING     SYS_FBA_HIST_78429   ENABLED

懷寧       SYS_FBA_HIST_78431   ENABLED

ORA        SYS_FBA_HIST_78448   ENABLED

DVD        SYS_FBA_HIST_78456   ENABLED

HUAINING   SYS_FBA_HIST_78464   ENABLED

QS         SYS_FBA_HIST_78472   ENABLED

FA         SYS_FBA_HIST_78484   ENABLED

 

7 rows selected.

 

            我們要執行那些不支援的DDL,就需要用dbms_flashback_archive禁用他們之間的對映關係,在操作,操作完在用該包啟用他們。

 

關於dbms_flashback_archive包的使用,參考官網:

          DBMS_FLASHBACK_ARCHIVE

            http://download.oracle.com/docs/cd/E11882_01/appdev.112/e16760/d_flashb_archive.htm#ARPLS72464

 

示例:

 

SQL> drop table 懷寧;

drop table 懷寧

           *

ERROR at line 1:

ORA-55610: Invalid DDL statement on history-tracked table

 

這個表使我們之前建立的,並啟用了Flashback Archive.

 

表的分離和重新結合:

SQL> exec dbms_flashback_archive.disassociate_fba('SYS','懷寧');

PL/SQL procedure successfully completed.

 

SQL> exec dbms_flashback_archive.reassociate_fba('SYS','懷寧');

PL/SQL procedure successfully completed.

 

最後我們在分離,在drop table

SQL> exec dbms_flashback_archive.disassociate_fba('SYS','懷寧');

PL/SQL procedure successfully completed.

 

SQL> drop table 懷寧;

drop table 懷寧

           *

ERROR at line 1:

ORA-55610: Invalid DDL statement on history-tracked table

 

drop 失敗。

 

表情: - -

猜測:靈異事件。

 

google 一下,說是bug9650074

 

9650074 ORA-55633 in Flashback data archive DDL support area

 

結果metalink 又不讀開,這個網路啊。 先不研究了。

 

 

.  一個用Flashback Data Archive 恢復資料的測試

 

這個測試使用之前的Flashback Archive: fla1.

 

建立測試表:

SQL> create table fa(id number) flashback archive;

Table created.

 

插入資料:

SQL> declare

  2  i number;

  3  begin

  4  for i in 1..100 loop

  5  insert into fa values(i);

  6  end loop;

  7  commit;

  8  end;

  9  /

PL/SQL procedure successfully completed.

 

SQL> select count(*) from fa;

  COUNT(*)

----------

  100

 

查詢時間:

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') tm from dual;

TM

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

2011-05-11 15:33:35

 

update 一次資料:

SQL> update fa set id=200 where id <50;

49 rows updated.

SQL>commit;

 

在查詢一次時間:

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') tm from dual;

 

TM

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

2011-05-11 15:35:23

 

 

 

使用Flashback Archive查詢1分鐘之前的資料:

SQL> select count(*) from fa as of timestamp (systimestamp - interval '1'minute);

 

  COUNT(*)

----------

       100

 

使用Flashback Archive查詢10分鐘之前的資料:

SQL> select count(*) from fa as of timestamp (systimestamp - interval '10'minute);

 

  COUNT(*)

----------

     0

 

這裡顯示為0. 因為我們還沒有做DML 操作。

 

或者使用時間來查:

SQL> select count(*) from fa as of timestamp to_timestamp('2011-05-11 15:35:23','yyyy-mm-dd hh24:mi:ss');

  COUNT(*)

----------

 100

 

SQL> delete from fa;

100 rows deleted

SQL> commit;

Commit complete.

 

 

SQL>  select count(*) from fa as of timestamp (systimestamp - interval '1'minute);

 

  COUNT(*)

----------

       100

 

根據時間的不同,查詢的結果也不一樣。 下面我們來確認下這個問題:

 

SQL> SELECT * from dba_flashback_archive_tables;

 

TABLE_NAME OWNER_NAME FLASHBACK_ARCHI ARCHIVE_TABLE_NAME   STATUS

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

FA         SYS        FLA1            SYS_FBA_HIST_78484   ENABLED

 

從這個結果,可以看出,在Flashback archive對應的FA錶的歷史表是SYS_FBA_HIST_78484

 

該表儲存了FA表的所有的操作記錄:

SQL> select count(*) from SYS_FBA_HIST_78484;

  COUNT(*)

----------

    149

 

SQL> desc SYS_FBA_HIST_78484

 Name                                      Null?    Type

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

 RID                                                VARCHAR2(4000)

 STARTSCN                                           NUMBER

 ENDSCN                                             NUMBER

 XID                                                RAW(8)

 OPERATION                                          VARCHAR2(1)

 ID                                                 NUMBER

 

注意一點:我們不能對這些歷史表做任何修改操作,只能查詢。

 

            如果想對這些歷史表進行相關的修改操作,和之前的操作一樣:使用dbms_flashback_archive分離2個表之間的關係。

 

如:

sql> exec dbms_flashback_archive.disassociate_fba('scott','emp_test');

sql> exec dbms_flashback_archive.reassociate_fba('scott','emp_test');

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

相關文章