萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> oracle教程 >> oracle閃回版本和閃回事務查詢詳解

oracle閃回版本和閃回事務查詢詳解

   --- 說明閃回數據庫

  --- 使用閃回表將表內容還原到過去的特定時間點

  --- 從刪除表中進行恢復

  --- 使用閃回查詢查看截止到任一時間點的數據庫內容

  --- 使用閃回版本查詢查看某一行在一段時間內的各個版本

  --- 使用閃回事務查詢查看事務處理歷史記錄或行

  優點:

  閃回技術由於只能處理更改數據,所以從根本上改變了恢復技術。使用這個技術時,從錯誤中恢復花費的時間等於制造錯誤所花費的時間。當閃回技術使用時,它與介質恢復相比,在易用性、可用性和還原時間方面有明顯的優勢。

  閃回數據庫使用閃回日志執行閃回。閃回刪除使用回收站。其他所有功能都使用還原數據。

oracle閃回版本和閃回事務查詢詳解    三聯

  閃回時間浏覽

  閃回技術提供的功能可用於查詢方案對象的過去版本、查詢歷史記錄數據以及執行更改分析。每個事務處理在邏輯上都會生成新版本數據庫。使用閃回技術,可通過浏覽這些版本來查找錯誤以及原因。

  · 閃回查詢:查詢特定時間點的所有數據。

  · 閃回版本查詢:查看兩個時間之間行的所有版本已經更改了行的事務處理。

  · 閃回事務處理查詢:查看事務處理做的所有更改。

  使用閃回查詢功能時,可以對自特定時間起的數據庫執行查詢。通過使用select語句的 as of 子句,可指定要查看其數據的時間戳。這有助於分析數據差異。

  實驗一:閃回查詢

  實驗一:閃回查詢:as of timestamp

  SYS@ORCL>conn tyger/tyger

  Connected.

  TYGER@ORCL>create table fb_query as select * from scott.dept;

  Table created.

  TYGER@ORCL>select * from fb_query;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  TYGER@ORCL>set time on;

  09:51:36 TYGER@ORCL>delete fb_query where deptno=10;

  1 row deleted.

  09:51:53 TYGER@ORCL>commit;

  Commit complete.

  09:51:57 TYGER@ORCL>select * from fb_query;

  DEPTNO DNAME LOC

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

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  09:52:06 TYGER@ORCL>select * from fb_query as of timestamp sysdate-1/1440;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  實驗二:閃回查詢應用

  10:25:04 TYGER@ORCL>drop table fb_tyger purge;

  Table dropped.

  10:25:10 TYGER@ORCL>create table fb_tyger as select * from scott.dept;

  Table created.

  10:25:33 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  10:25:44 TYGER@ORCL>select sysdate from dual;

  SYSDATE

  ---------

  14-MAR-14

  10:26:02 TYGER@ORCL>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

  Session altered.

  10:26:30 TYGER@ORCL>select sysdate from dual;

  SYSDATE

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

  2014-03-14 10:26:38

  10:26:38 TYGER@ORCL>update fb_tyger set dname='';

  4 rows updated.

  10:26:51 TYGER@ORCL>commit;

  Commit complete.

  10:26:54 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 NEW YORK

  20 DALLAS

  30 CHICAGO

  40 BOSTON

  10:27:12 TYGER@ORCL>select * from fb_tyger as of timestamp to_timestamp('2014-03-14 10:26:38','yyyy-mm-dd hh24:mi:ss');

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  此處遇到錯誤:

  ERROR at line 1:

  ORA-01466: unable to read data - table definition has changed

  參考文檔: http://blog.csdn.net/wanghui5767260/article/details/21227101

  10:29:21 TYGER@ORCL>select * from fb_tyger as of timestamp sysdate-3/1440;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  10:29:35 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 NEW YORK

  20 DALLAS

  30 CHICAGO

  40 BOSTON

  10:46:22 TYGER@ORCL>set time off

  TYGER@ORCL>update fb_tyger t

  2 set dname =

  3 (select dname from fb_tyger as of timestamp

  4 to_timestamp('2014-03-14 10:26:38','yyyy-mm-dd hh24:mi:ss')

  5 where t.deptno=fb_tyger.deptno);

  4 rows updated.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  實驗三:閃回查詢 as of scn

  TYGER@ORCL>conn / as sysdba

  Connected.

  SYS@ORCL>grant execute on dbms_flashback to tyger;

  Grant succeeded.

  TYGER@ORCL>select dbms_flashback.get_system_change_number from dual;

  GET_SYSTEM_CHANGE_NUMBER

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

  1107246

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  TYGER@ORCL>delete fb_tyger where deptno<=30;

  3 rows deleted.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

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

  40 OPERATIONS BOSTON

  TYGER@ORCL>select * from fb_tyger as of scn 1107246;

  DEPTNO DNAME LOC

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

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  實驗四:利用PL/SQL包dbms_flashback

  語法:

  · 會話啟用閃回指定時間:

  DBMS_FLASHBACK.ENABLE_AT_TIME(query_time IN TIMESTAMP);

  · 會話啟用閃回指定SCN:

  DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(query_scn IN NUMBER);

  · 關閉閃回:

  DBMS_FLASHBACK.DISABLE;

  TYGER@ORCL>conn / as sysdba

  Connected.

  SYS@ORCL>grant execute on dbms_flashback to tyger;

  Grant succeeded.

  SYS@ORCL>conn tyger/tyger

  Connected.

  TYGER@ORCL>

  TYGER@ORCL>

  TYGER@ORCL>

  TYGER@ORCL>create table fb_query1 as select * from scott.dept;

  Table created.

  TYGER@ORCL>create table fb_query2 as select * from scott.dept;

  Table created.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_query1;

  DEPTNO DNAME LOC

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

  10 ACCOUNTIN

copyright © 萬盛學電腦網 all rights reserved