聊聊数据库闪回技术
??提到閃回技術(shù),工作這么久了我也很少用到, 以至于我都快忘記閃回技術(shù)都有哪些東西了。今天得空,就來復(fù)習(xí)一下數(shù)據(jù)庫中的閃回技術(shù)。
??即使不看書,我印象中的閃回技術(shù)分這么幾種,閃回數(shù)據(jù)庫、閃回刪除、閃回查詢。閃回技術(shù)相比于數(shù)據(jù)庫不完全恢復(fù),其特點里速度快,影響層面小。
一、閃回數(shù)據(jù)庫
??閃回數(shù)據(jù)庫是把數(shù)據(jù)庫整體狀態(tài)恢復(fù)到過去某一時間點或者某一系統(tǒng)更改號(SCN),是實施數(shù)據(jù)庫不完全恢復(fù)的一種快速方式。
1.1使用要求:
1)必須有SYSDBA的權(quán)限
2)啟用了recovery area
3)數(shù)據(jù)庫處于FLASHBACK模式
4)數(shù)據(jù)庫處于mount狀態(tài)
此外,
5)數(shù)據(jù)庫必須處理歸檔模式;
6)控制文件不能是備份的控制文件或者重建的控制文件;
7)數(shù)據(jù)庫不包含處于FLASHBACK OFF的表空間。
1.2 語法
說明:
FLASHBACK DATABASE:當(dāng)使用flashback database命令時,數(shù)據(jù)庫驗證所要求的歸檔日志和聯(lián)機(jī)重做日志是否可用。如果它們可用,那么它將數(shù)據(jù)庫中的所有當(dāng)前聯(lián)機(jī)數(shù)據(jù)文件恢復(fù)為SCN或此語句中指定的時間。
數(shù)據(jù)庫中保留的閃回數(shù)據(jù)量由DB_FLASHBACK_RETENTION_TARGET初始化參數(shù)和快速恢復(fù)區(qū)的大小控制。可以通過查詢V$FLASHBACK_DATABASE_LOG視圖來確定多長時間后可以閃回數(shù)據(jù)庫。
STANDBY:指定STANDBY以將備用數(shù)據(jù)庫還原到較早的SCN或時間。如果數(shù)據(jù)庫不是備用數(shù)據(jù)庫,則數(shù)據(jù)庫返回錯誤。如果省略此子句,則數(shù)據(jù)庫可以是主數(shù)據(jù)庫或備用數(shù)據(jù)庫。
TO SCN語句:
?TO SCN將數(shù)據(jù)庫恢復(fù)為其在指定SCN的狀態(tài)。
?TO BEFORE SCN將數(shù)據(jù)庫恢復(fù)到緊靠指定SCN之前的系統(tǒng)更改號的狀態(tài)。
TO TIMESTAMP語句:
?TO TIMESTAMP:將數(shù)據(jù)庫恢復(fù)為其在指定時間戳的狀態(tài)。
?TO BEFORE TIMESTAMP:將數(shù)據(jù)庫恢復(fù)到指定時間戳之前一秒的狀態(tài)。
TO RESTORE POINT語句:指定此子句以將數(shù)據(jù)庫閃回到指定的還原點。 是未啟用閃回數(shù)據(jù)庫唯一可以使用的語句。
RESETLOGS:
將數(shù)據(jù)庫閃回到剛好在最后一次resetlogs操作(ALTER DATABASE OPEN RESETLOGS)之前。
知道了閃回數(shù)據(jù)庫的要求以及語法之后,就可以操作數(shù)據(jù)庫的閃回模式了。(測試環(huán)境:ORACLE 11GR2)
1)使用數(shù)據(jù)庫管理員登錄數(shù)據(jù)庫
SQL>?conn?/as?sysdba2)查看數(shù)據(jù)庫是否啟用閃回模式
?
SQL>??select?flashback_on?from?v$database;FLASHBACK_ON ------------------ NO數(shù)據(jù)庫未啟用,則使用如下命令:
ALTER DATABASE FLASHBACK ON; #實際測試過程中,執(zhí)行該命令時沒有重啟數(shù)據(jù)庫。
3) 查看是否啟用數(shù)據(jù)庫閃回區(qū)
SQL>?show?parameter?recovery NAME??????????????????????TYPE?????VALUE -----------------------------------?-----------?------------------------------ db_recovery_file_dest??????????string????/u01/app/oracle/flash_recovery_area db_recovery_file_dest_size???????big?integer??3882M recovery_parallelism???????????integer?????0#修改方式:
#調(diào)整閃回區(qū)的大小及位置
#SQL> alter system set db_recovery_file_dest_size=5g scope=spfile;
#設(shè)置閃回區(qū)位置:
#SQL> alter system set db_recovery_file_dest='/app/flash_recovery_area' scope=spfile;
4) 查看 DB_FLASHBACK_RETENTION_TARGET參數(shù)(單位:分鐘)
SQL>?show?parameter?db_flashback_retention_targetNAME??????????????????????TYPE??????VALUE ------------------------------------?-----------?------------------------------ db_flashback_retention_target??????integer????1440修改方式:
#SQL>?alter?system?set?db_flashback_retention_target=7200?scope=spfile;5)查看是否處于歸檔模式
SQL>?archive?log?list; Database?log?mode???????Archive?Mode Automatic?archival???????Enabled Archive?destination???????USE_DB_RECOVERY_FILE_DEST Oldest?online?log?sequence?????10 Next?log?sequence?to?archive???12 Current?log?sequence???????12已啟用歸檔模式
3)和4)中的參數(shù)可以根據(jù)實際需要修改
6) 查看V$FLASHBACK_DATABASE_LOG,查看是否有數(shù)據(jù)生成
SQL>?select?*?from?V$FLASHBACK_DATABASE_LOG;OLDEST_FLASHBACK_SCN?OLDEST_FLASHBACK_TIME?RETENTION_TARGET?FLASHBACK_SIZE?ESTIMATED_FLASHBACK_SIZE --------------------?---------------------?----------------?--------------?------------------------ 1025623??????????????2017/1/13?11:11:05????1440????????8192000?????07)閃回數(shù)據(jù)庫
創(chuàng)建測試表:
SQL>?create?table?scott.test_1113_1?as?select?*?from?v$logfile;Table?createdSQL>?create?table?scott.test_1113_2?as?select?*?from?v$logfile;Table?createdSQL>?select?dbms_flashback.get_system_change_number,SCN_TO_TIMESTAMP(dbms_flashback.get_system_change_number)??from?dual;??GET_SYSTEM_CHANGE_NUMBER?SCN_TO_TIMESTAMP(DBMS_FLASHBAC ------------------------?-------------------------------------------------------------------------------- 1026334??????????????????13-1月?-17?11.21.50.000000000?上午此時的系統(tǒng)更改點是1026334,之后數(shù)據(jù)庫會恢復(fù)到這個點。表scott.test_1113_1和scott.test_1113_2都應(yīng)該存在。
SQL>?drop?table?scott.test_1113_1;Table?droppedSQL>?drop?table?scott.test_1113_2;Table?droppedSQL>?shutdown?abort; ORACLE?instance?shut?down. SQL>?startup?mount; ORACLE?instance?started. Total?System?Global?Area??776646656?bytes Fixed?Size????2217384?bytes Variable?Size??511707736?bytes Database?Buffers??260046848?bytes Redo?Buffers????2674688?bytes Database?mounted. SQL>?flashback?database?to?scn?1026334; Flashback?complete. SQL>?alter?database?open?resetlogs; Database?altered. SQL>?select?count(1)?from?scott.test_1113_1;COUNT(1) ----------3SQL>?select?count(1)?from?scott.test_1113_2;COUNT(1) ----------3閃回數(shù)據(jù)庫后,scott.test_1113_1和scott.test_1113_2都存在。
二、閃回表
??使用flashback table 命令,可以將數(shù)據(jù)庫的表恢復(fù)到之前某一個時刻的狀態(tài),至于能恢復(fù)到什么時間點,則依賴于回滾段的數(shù)據(jù)量。flashback table 操作不可回滾。
2.1 使用要求
??具有該表的FLASHBACK權(quán)限或者有 FLASHBACK ANY TABLE的權(quán)限,此外,還必須有SELECT, INSERT, DELETE, and ALTER的權(quán)限。
??使用flashback table的表必須開啟row movement(從回收站閃回的表例外)。
??要將表閃回到還原點,必須具有SELECT ANY DICTIONARY或FLASHBACK ANY TABLE的系統(tǒng)特權(quán)或SELECT_CATALOG_ROLE角色。????
2.2 語法
??在Oracle閃回表操作期間,Oracle數(shù)據(jù)庫會在閃回列表中指定的所有表上獲取獨占DML鎖。當(dāng)這些表恢復(fù)到其早期狀態(tài)時,這些鎖阻止對表的任何操作。
??閃回表操作在單個事務(wù)中執(zhí)行,而與閃回列表中指定的表數(shù)無關(guān)。 所有表都恢復(fù)到早期狀態(tài),或者它們都不恢復(fù)。如果閃回表操作在任何表上失敗,則整個語句將失敗。
??在完成閃回表操作時,表中的數(shù)據(jù)與早期的表一致。 但是,FLASHBACK TABLE TO SCN或TIMESTAMP不保留rowid,而FLASHBACK TABLE TO BEFORE DROP不會恢復(fù)之前引用的約束。
??Oracle數(shù)據(jù)庫不會將與表關(guān)聯(lián)的統(tǒng)計信息還原到之前的表單。當(dāng)前存在的表上的索引將被還原,并反映閃回點處的表的狀態(tài)。 如果索引現(xiàn)在存在,但在閃回點尚不存在,則數(shù)據(jù)庫更新索引以反映閃回點處的表的狀態(tài)。 但是,在閃回點和當(dāng)前時間之間的間隔期間丟棄的索引不會恢復(fù)。
schema:表的擁有者
table:指定包含要還原到早期版本的數(shù)據(jù)的一個或多個表的名稱。
??限制:
??1)閃回表操作對以下類型對象無效:作為集群一部分的表,物化視圖,高級排隊(AQ)表,靜態(tài)數(shù)據(jù)字典表,系統(tǒng)表,遠(yuǎn)程表,對象表,嵌套表或單個表 分區(qū)或子分區(qū)。
??2)以下DDL操作更改表的結(jié)構(gòu),以便以后無法使用TO SCN或TO TIMESTAMP子句將表閃回到操作之前的某個時間:升級,移動或截斷表; 向表添加約束,向集群添加表; 修改或丟棄柱; 改變列加密密鑰; 添加,刪除,合并,拆分,合并或截斷分區(qū)或子分區(qū)(除了添加范圍分區(qū))。(upgrading, moving, or truncating a table; adding a constraint to a table, adding a table to a cluster; modifying or dropping a column; changing a column encryption key; adding, dropping, merging, splitting, coalescing, or truncating a partition or subpartition (with the exception of adding a range partition).
TO RESTORE POINT:指定要將表閃回的恢復(fù)點。 還原點必須已創(chuàng)建。
ENABLE | DISABLE TRIGGERS:默認(rèn)關(guān)閉觸發(fā)器
TO BEFORE DROP:從回收站中還原表。
可以指定表的原始名稱或分配給該對象的系統(tǒng)名稱;
如果存在多個同名表,將還原最晚刪除的表,即后進(jìn)先出。
RENAME TO:重命名。
2.3 例子
1)創(chuàng)建新的測試表
SQL>?create?table?scott.test_1114_1?as?select?*?from?v$logfile; SQL>?select?*?from?scott.test_1114_1;GROUP#?????STATUS??TYPE??MEMBER?????????????????????IS_RECOVERY_DEST_FILE ----------?-------?-------?-------------------------------------------------?--------------------- 3??????ONLINE??????????/u01/app/oracle/oradata/orcl/redo03.log??NO 2??????ONLINE??????????/u01/app/oracle/oradata/orcl/redo02.log??NO 1??????ONLINE??????????/u01/app/oracle/oradata/orcl/redo01.log??NO2)獲取當(dāng)前SCN和時間戳,最后該表數(shù)據(jù)會恢復(fù)到當(dāng)前的時間點。
SQL>?select?dbms_flashback.get_system_change_number,SCN_TO_TIMESTAMP(dbms_flashback.get_system_change_number)?timestamp??from?dual;GET_SYSTEM_CHANGE_NUMBER?TIMESTAMP ------------------------?-------------------------------------------------------------------------------- 1031289??????????????????14-1月?-17?09.13.59.000000000?上午3)刪除該表中的記錄
SQL>?delete?from?scott.test_1114_1;? SQL>?select?count(1)?from?scott.test_1114_1;COUNT(1) ----------04)閃回表
SQL>?alter?table?scott.test_1114_1?enable?row?movement;Table?altered SQL>?flashback?table?scott.test_1114_1?to?scn?1031289;DoneSQL>?select?*?from?scott.test_1114_1;GROUP#???STATUS??TYPE??MEMBER??????????????????????????????IS_RECOVERY_DEST_FILE ----------?-------?-------?---------------------------------------?-------------------- 3???????ONLINE???????/u01/app/oracle/oradata/orcl/redo03.log?NO 2??????ONLINE???????????/u01/app/oracle/oradata/orcl/redo02.log?NO 1??????ONLINE???????????/u01/app/oracle/oradata/orcl/redo01.log?NO表已恢復(fù)
5)多次刪除與創(chuàng)建該表
SQL>?drop?table??scott.test_1114_1;Table?droppedSQL>?create?table?scott.test_1114_1?as?select?*?from?v$logfile;Table?createdSQL>?drop?table??scott.test_1114_1;Table?droppedSQL>?create?table?scott.test_1114_1?as?select?*?from?v$logfile;Table?createdSQL>?drop?table??scott.test_1114_1;Table?dropped6)查看回收站
SQL>?select?object_name,original_name,droptime?from?dba_recyclebin;OBJECT_NAME?????????????ORIGINAL_NAME??????????????DROPTIME ------------------------------?--------------------------------?------------------- BIN$RgTFmsLNhcHgUKjAyX44MA==$0?TEST_1114_1???????????????2017-01-14:09:21:37 BIN$RgTFmsLOhcHgUKjAyX44MA==$0?TEST_1114_1???????????????2017-01-14:09:21:58 BIN$RgTFmsLPhcHgUKjAyX44MA==$0?TEST_1114_1???????????????2017-01-14:09:22:017)恢復(fù)表
SQL>?flashback?table?scott.test_1114_1?to?before?drop;DoneSQL>?select?object_name,original_name,droptime?from?dba_recyclebin;OBJECT_NAME??????????????ORIGINAL_NAME??????????????DROPTIME ------------------------------?--------------------------------?------------------- BIN$RgTFmsLNhcHgUKjAyX44MA==$0?TEST_1114_1???????????????2017-01-14:09:21:37 BIN$RgTFmsLOhcHgUKjAyX44MA==$0?TEST_1114_1???????????????2017-01-14:09:21:58最晚被刪除的表被還原。
SQL>?flashback?table?scott.test_1114_1?to?before?drop?rename?to?test_1114_2;DoneSQL>?select?object_name,original_name,droptime?from?dba_recyclebin;OBJECT_NAME?????????????ORIGINAL_NAME??????????????DROPTIME ------------------------------?--------------------------------?------------------- BIN$RgTFmsLNhcHgUKjAyX44MA==$0?TEST_1114_1???????????????2017-01-14:09:21:37三、閃回查詢
??要使用閃回查詢,必須要有表的查詢權(quán)限,以及該表的FLASHBACK 權(quán)限或FLASHBACK ANY TABLE的系統(tǒng)權(quán)限。
??閃回查詢有兩種,一種是查詢某一時間點的的數(shù)據(jù)(as of),另一種查詢某一時間段內(nèi)數(shù)據(jù)的操作(versions between)。閃回查詢并不會影響到當(dāng)前表中的數(shù)據(jù)。
1)閃回時間點查詢
select?*?from?<table_name>??as?of??timestamp?to_timestamp(timestamp,'yyyy-mm-dd?hh24:mi:ss'); select?*?from?<table_name>??as?of??scn?scn_number;2) 閃回版本查詢
SELECT?versions_startscn,?versions_starttime,?versions_endscn,?versions_endtime,?versions_xid,?versions_operation,?t.* FROM?<table_name>?t VERSIONS?BETWEEN?TIMESTAMP?BeginTimestamp?and?EndTimestamp; SELECT?versions_startscn,?versions_starttime,?versions_endscn,?versions_endtime,?versions_xid,?versions_operation,?t.* FROM?<table_name>?t VERSIONS?BETWEEN?scn?begin_scn?and?end_scn;例子:
1)獲取當(dāng)前的SCN
SQL>?select?dbms_flashback.get_system_change_number,SCN_TO_TIMESTAMP(dbms_flashback.get_system_change_number)?timestamp??from?dual;GET_SYSTEM_CHANGE_NUMBER?TIMESTAMP ------------------------?-------------------------------------------------------------------------------- 1032595??????????????????14-1月?-17?09.47.57.000000000?上午2)增加test_1114_1表中的數(shù)據(jù)
SQL>?insert?into?scott.test_1114_1?select?*?from?scott.test_1114_1; 3?rows?inserted3)閃回時間點查詢
SQL>?select?*?from?scott.test_1114_1?as?of?scn?1032595;GROUP#?????STATUS??TYPE????MEMBER??????????????????????IS_RECOVERY_DEST_FILE ----------?-------?-------?-----------------------------------------?--------------------- 3???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo03.log??NO 2???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo02.log??NO 1???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo01.log??NOSQL>?select?*?from?scott.test_1114_1;GROUP#?????STATUS??TYPE????MEMBER??????????????????????IS_RECOVERY_DEST_FILE ----------?-------?-------?-----------------------------------------?--------------------- 3???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo03.log??NO 2???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo02.log??NO 1???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo01.log??NO 3???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo03.log??NO 2???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo02.log??NO 1???????ONLINE??????????/u01/app/oracle/oradata/orcl/redo01.log??NO6?rows?selected4)閃回版本查詢
SQL>?select?dbms_flashback.get_system_change_number,SCN_TO_TIMESTAMP(dbms_flashback.get_system_change_number)?timestamp??from?dual;GET_SYSTEM_CHANGE_NUMBER??TIMESTAMP ------------------------??------------------------------------------------------------------------------- 1032939???????????????????14-1月?-17?09.52.42.000000000?上午 SQL>?SELECT?versions_startscn,?versions_starttime,?versions_endscn,?versions_endtime,?versions_xid,?versions_operation,?group#,status,type,member?,is_recovery_dest_file2??FROM?scott.test_1114_13??VERSIONS?BETWEEN?scn?1032595?and??1032939;VERSIONS_STARTSCN?VERSIONS_STARTTIME??????VERSIONS_ENDSCN?VERSIONS_ENDTIME??VERSIONS_XID???VERSIONS_OPERATION??GROUP#?????STATUS??TYPE???MEMBER?????????????????????IS_RECOVERY_DEST_FILE -----------------?-----------------------?---------------?-----------------?----------------?------------------?----------?-------?-------?-------------------------------------?---------------------3???????ONLINE?????????/u01/app/oracle/oradata/orcl/redo03.log????NO2???????ONLINE?????????/u01/app/oracle/oradata/orcl/redo02.log????NO1???????ONLINE?????????/u01/app/oracle/oradata/orcl/redo01.log????NO 1032882??????????14-1月?-17?09.50.06?上午???????????????????????030001002F030000?I?????????????3???????ONLINE?????????/u01/app/oracle/oradata/orcl/redo03.log????NO 1032882??????????14-1月?-17?09.50.06?上午???????????????????????030001002F030000?I?????????????2???????ONLINE?????????/u01/app/oracle/oradata/orcl/redo02.log????NO 1032882??????????14-1月?-17?09.50.06?上午???????????????????????030001002F030000?I?????????????1???????ONLINE?????????/u01/app/oracle/oradata/orcl/redo01.log????NO6?rows?selected閃回版本查詢查到了SCN為1032595表的數(shù)據(jù)狀態(tài),又查到了3條insert的記錄。
四、總結(jié)
??到目前為止,所接觸到的關(guān)于閃回的技術(shù)就是這些。分別是閃回數(shù)據(jù)庫[1種],閃回表[2種方式],閃回查詢[2種方式]。
參考資料:
1.《Database SQL Language Reference》.
轉(zhuǎn)載于:https://blog.51cto.com/myhwj/1891947
總結(jié)
- 上一篇: 让“王码五笔输入法”成为你的专用输入法!
- 下一篇: 【NanoPi T2】 8.uboot