oracle的閃回查詢、閃回表、閃回資料庫

來源:互聯網
上載者:User
/* 一、 要使用閃回查詢,資料庫必須開啟automatic undo management,必須有undo資料表空間,必須設定好復原段的保留時間 */-- 在sqlplus中查看undo_management參數值是否為AUTO,如果是“MANUAL”手動,需要修改為“AUTO”;-- 查看復原段的保留時間undo_retention,預設900秒(15分鐘)show parameter undo-- 查看所有的復原段select SEGMENT_ID ,SEGMENT_NAME  from dba_rollback_segs;-- 指定事務使用某個復原段,如果不人為的指定復原段,則資料庫會根據復原段中事務來權衡,以使得所有復原段中事務壓力儘可能平均。set transaction use rollback segment rbs6;-- 修改undo_management參數值為AUTO/*Oracle有個spfile動態參數檔案,裡面設定了Oracle的各種參數。所謂的動態,就是說你可以在不關閉資料庫的情況下,更改資料庫參數,記錄在spfile裡面。更改參數的時候,有4種scope選項,scope就是範圍。scope=spfile 僅僅更改spfile裡面的記載,不更改記憶體,也就是不立即生效,而是等下次資料庫啟動生效,有一些參數只允許用這種方法更改;scope=memory 僅僅更改記憶體,不改spfile,也就是下次啟動就失效了;scope=both 記憶體和spfile都更改;不指定scope參數,等同於scope=both。*/alter system set undo_management='AUTO' scope=spfile;-- 修改undo_retention為1小時alter system set undo_retention=3600 scope=both;-- 查看修改是否立即生效show parameter undoselect name,value from v$spparameter where name='undo_management';-- 重啟資料庫,使修改生效shutdown immediatestartupshow parameter undo/* 測試閃回查詢 */select * from t1 where id<10;delete from t1 where id<10;commit;-- 查詢15分鐘之前的表資料select * from t1 as of timestamp(sysdate - 15/1440) where id<10;-- 將資料恢複insert into t1  select * from t1 as of timestamp(sysdate - 15/1440) where id<10;commit;/* 根據時間的閃回本質上是基於scn的閃回 */-- 將dbms_flashback的執行許可權授權給scott使用者grant execute on dbms_flashback to scott;-- 查詢當前的系統改變號scn,並記錄下來,2363533select dbms_flashback.get_system_change_number from dual; -- 刪除資料delete from t1 where id<10;commit;-- 根據刪除資料時間點前的scn來查詢刪除前的資料select * from t1 as of scn(2363533) where id<10;-- 將資料恢複insert into t1  select * from t1 as of scn(2363533) where id<10;commit;-- 使用ora_rowscn偽列來查看與每一行關聯的scnselect ora_rowscn,t1.* from t1-- 查看scn映射的事務提交時間select scn_to_timestamp(2363533) from dual;-- 查看每行資料的最新事務提交時間select scn_to_timestamp(ora_rowscn), t1.* from t1;/* 二、閃回表 */drop table t1;select * from t1;-- 刪除t1表後,該表的block還在資料表空間中,查詢資源回收筒可以看到被刪除的對象select * from recyclebin;-- 閃回表到刪除之前flashback table t1 to before drop;/* 閃回表到某個時間點 */update t1 set contents='abc';commit;-- 必須啟用表的行移動功能alter table t1 enable row movement;flashback table t1 to timestamp(systimestamp - 5/1440);/* 三、閃回資料庫 */-- 需要有sysdba許可權,才能使用flashback database命令,必須以獨佔模式裝載資料庫,但不開啟資料庫。-- 同時將資料庫置於閃回模式,執行閃回。startup mount exclusive;alter database archivelog;alter database flashback on;alter database open;-- 查看閃回模式是否開啟select current_scn, flashback_on from v$database;shutdown;startup mount exclusive;-- 閃回資料庫到1小時之前flashback database to timestamp sysdate-1/24;-- 閃回成功後,開啟資料庫,同時resetlogs開啟對資料庫的寫存取權限alter database open resetlogs;startup mount exclusive;alter database flashback off;alter database open;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.