Flashback query(閃回查詢)原理
Oracle根據(jù)undo信息,利用undo數(shù)據(jù),類似一致性讀取方法,可以把表置于一個(gè)刪除前的時(shí)間點(diǎn)(或SCN),從而將數(shù)據(jù)找回。
Flashback query(閃回查詢)前提:
SQL> show parameter undo;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
其中undo_management = auto,設(shè)置自動(dòng)undo管理(AUM),該參數(shù)默認(rèn)設(shè)置為:auto;
Undo_retention = n(秒),設(shè)置決定undo最多的保存時(shí)間,其值越大,就需要越多的undo表空間的支持。修改undo_retention的命令如下:
SQL> alter system set undo_retention = 3600;
System altered
閃回實(shí)現(xiàn)方式
1. 獲取數(shù)據(jù)刪除前的一個(gè)時(shí)間點(diǎn)或scn,如下:
SQL>select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') time, to_char(dbms_flashback.get_system_change_number) scn from dual;
TIME SCN
------------------- ----------------------------------------
2010-06-29 23:03:14 1060499
2. 查詢?cè)摃r(shí)間點(diǎn)(或scn)的數(shù)據(jù),如下:
SQL> select * from t as of timestamp to_timestamp('2010-06-29 22:57:47', 'yyyy-mm-dd hh24:mi:ss');
SQL> select * from t as of scn 1060174;
3. 將查詢到的數(shù)據(jù),新增到表中。也可用更直接的方法,如:
SQL>create table tab_test as select * from t of timestamp to_timestamp('2010-06-29 22:57:47', 'yyyy-mm-dd hh24:mi:ss');
SQL>insert into tab_test select * from1060174;
示例:
Create table t(id number);
insertinto t values(1);
insert into t values(2);
insert into t values(3);
insert into t values(4);
insert into t values(5);
1.查看t表中的原始數(shù)據(jù)
SQL> select * from t;
ID
---------
1
2
3
4
5
2.獲取數(shù)據(jù)刪除前的一個(gè)時(shí)間點(diǎn)或scn
SQL> select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') time, to_char(dbms_flashback.get_system_change_number) scn from dual;
TIME SCN
------------------- ----------------------------------------
2010-06-29 23:23:33 1061279
3.刪除t表中的數(shù)據(jù),并提交事物
SQL> delete from t;
5 rows deleted
SQL> commit;
Commit complete
4.在查看t表,此時(shí)t表中數(shù)據(jù)以刪除
SQL> select * from t;
ID
----------
5.查看t表中scn為1061279(或時(shí)間點(diǎn)為2010-06-29 23:23:33)時(shí)的數(shù)據(jù)
SQL> select * from t as of scn 1061279;
ID
----------
1
2
3
4
5
6.確認(rèn)要恢復(fù)后,將t表中的數(shù)據(jù)還原到scn為1061279(或時(shí)間點(diǎn)為2010-06-29 23:23:33)時(shí)的數(shù)據(jù),并提交事物
SQL> insert into t select * from t as of scn 1061279;
5 rows inserted
SQL> commit;
Commit complete
7.確認(rèn)t表數(shù)據(jù)的還原情況
SQL> select * from t;
ID
----------
1
2
3
4
5
注:推薦使用scn,由于oracle9i中,因?yàn)?/span>scn與時(shí)間點(diǎn)的同步需要5分鐘,如果最近5分鐘之內(nèi)的數(shù)據(jù)需要Falshback query查詢,可能會(huì)查詢丟失,而scn則不存在這個(gè)問(wèn)題。Oracle10g中這個(gè)問(wèn)題已修正(scn與時(shí)間點(diǎn)的大致關(guān)系,可以通過(guò)logmnr分析歸檔日志獲得)。
Falshback query查詢的局限:
1. 不能Falshback到5天以前的數(shù)據(jù)。
2. 閃回查詢無(wú)法恢復(fù)到表結(jié)構(gòu)改變之前,因?yàn)殚W回查詢使用的是當(dāng)前的數(shù)據(jù)字典。
3. 受到undo_retention參數(shù)的影響,對(duì)于undo_retention之前的數(shù)據(jù),Flashback不保證能Flashback成功。
4. 對(duì)drop,truncate等不記錄回滾的操作,不能恢復(fù)。
5. 普通用戶使用dbms_flashback包,必須通過(guò)管理員授權(quán)。命令如下:
SQL>grant execute on dbms_flashback to scott;
posted on 2010-08-04 20:39
xzc 閱讀(27797)
評(píng)論(0) 編輯 收藏 所屬分類:
Oracle