Flashback Version Query

 

09:23:45 SCOTT@db252>select * from dept;sql

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON數據庫

Elapsed: 00:00:00.04
09:23:57 SCOTT@db252>insert into dept values(50,'WHAT\'S THAT','BEIJING');
ERROR:
ORA-01756: quoted string not properly terminatedoracle


Elapsed: 00:00:00.06
09:24:38 SCOTT@db252>insert into dept values(50,q'[WHAT'S THAT]','BEIJING');ui

1 row created.spa

Elapsed: 00:00:00.04
09:25:13 SCOTT@db252>commit;.net

Commit complete.blog

Elapsed: 00:00:00.00
09:25:16 SCOTT@db252>select * from dept;事務

DEPTNO DNAME LOC
---------- -------------- -------------
50 WHAT'S THAT BEIJING
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTONip

Elapsed: 00:00:00.02ci

09:29:10 SCOTT@db252>select * from dept versions between timestamp to_timestamp('2014-06-06 09:10:48','yyyy-mm-dd hh24:mi:ss') and to_timestamp('2014-
06-06 09:26:48','yyyy-mm-dd hh24:mi:ss');

DEPTNO DNAME LOC
---------- -------------- -------------
50 WHAT'S THAT BEIJING
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

Elapsed: 00:00:00.05
09:32:26 SCOTT@db252>select * from dept versions between timestamp to_timestamp('2014-06-06 09:10:48','yyyy-mm-dd hh24:mi:ss') and to_timestamp('2014-
06-06 09:12:48','yyyy-mm-dd hh24:mi:ss');

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

Elapsed: 00:00:00.02
09:32:39 SCOTT@db252>

 

 

flashback六大技術之flashback version query

環境:

[sql]  view plain copy print ?
 
  1. 23:47:03 hr@ORCL (^ω^) select * from v$version where rownum=1;  
  2.   
  3. BANNER  
  4. --------------------------------------------------------------------------------  
  5. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod  


    1 原理介紹

        flashback query只能「穿越」到過去的某個時間點上的數據庫版本,可是在當前時間和過去的某個時間點上,一個表中的數據可能已經被變動屢次,單一版本可能沒法知足恢復的需求。經過flashback version query可以查看指定時間段內undo表空間中被提交的記錄的不一樣版本。

                                flashback version query的僞列說明

 

 

versions_startscn

versions_starttime

記錄了操做時的SCN或時間,若是爲空,表示在查詢範圍外建立的

versions_endscn

versions_endtime

記錄了失效時的SCN或時間,配合version_operation列查看,若是爲空,或者被刪除、或者

該記錄當前時間在當前表不存在

versions_operation I:insert     D:delete    U:update
versions_operation 事務ID

 

 

    2 實驗

[sql]  view plain copy print ?
 
    1. 23:38:26 hr@ORCL (^ω^) select dbms_flashback.get_system_change_number from dual;  
    2.   
    3. GET_SYSTEM_CHANGE_NUMBER  
    4. ------------------------  
    5.                  3930808  
    6.   
    7. 23:38:48 hr@ORCL (^ω^) update t1 set empno=122 where ename='Water';  
    8.   
    9. 已更新 1 行。  
    10.   
    11. 23:39:42 hr@ORCL (^ω^) commit;  
    12.   
    13. 提交完成。  
    14.   
    15. 23:40:06 hr@ORCL (^ω^) delete t1 where rownum<5;  
    16.   
    17. 已刪除4行。  
    18.   
    19. 23:40:25 hr@ORCL (^ω^) commit;  
    20.   
    21. 提交完成。  
    22.   
    23. 23:40:29 hr@ORCL (^ω^) insert into t1 values(155,'qao');  
    24.   
    25. 已建立 1 行。  
    26.   
    27. 23:40:56 hr@ORCL (^ω^) insert into t1 values(156,'qinqin');  
    28.   
    29. 已建立 1 行。  
    30.   
    31. 23:41:19 hr@ORCL (^ω^) commit;  
    32.   
    33. 提交完成。  
    34.   
    35. 23:41:22 hr@ORCL (^ω^) select dbms_flashback.get_system_change_number from dual;  
    36.   
    37. GET_SYSTEM_CHANGE_NUMBER  
    38. ------------------------  
    39.                  3931066  
    40.   
    41. 23:46:34 hr@ORCL (^ω^) select empno,ename,versions_startscn,versions_endscn,versions_operation,versions_xid  
    42. 23:47:00   2              from t1 versions between scn 3930808 and 3931066;  
    43.   
    44.      EMPNO ENAME      VERSIONS_STARTSCN VERSIONS_ENDSCN VE VERSIONS_XID  
    45. ---------- ---------- ----------------- --------------- -- ----------------  
    46.        155 qao                  3930985                 I  04002000D4030000  
    47.        111 Linshuibin           3930897                 D  06002F00B6040000  
    48.        122 Water                3930897                 D  06002F00B6040000  
    49.        122 Water                3930883         3930897 U  02001100BD040000  
    50.        111 Water                                3930883  
    51.        111 Linshuibin                           3930897  
    52.       7566 JONES                3930897                 D  06002F00B6040000  
    53.       7521 WARD                 3930897                 D  06002F00B6040000  
    54.       7521 WARD                                 3930897  
    55.       7566 JONES                                3930897  
    56.       7788 SCOTT  
    57.       7844 TURNER  
    58.       7900 JAMES  
    59.       7902 FORD  
    60.        156 qinqin               3930985                 I  04002000D4030000  
    61.        110 Think  
    62.   
    63. 已選擇16行。  

 

 

關於flashback versions query 和flashback transaction query 特性

前面提到了oracle 自9i以來的flashback query 特性,藉助該特性,能夠看到某個過去某個時間點的某張表的數據,這爲咱們修正dml 誤操做等提供了很大的方便。
可是對於該特性來講,若是隻是想回退某個時間點以來的部分數據,那麼純粹依靠先後的數據對比來判斷,就會顯的很是吃力,或許根本就是沒法辦到的。好比,你某時刻同時跑起了多個job做業,
若是一個job有問題,而其餘的job正常完成的,此時,若是你僅僅是想回復錯誤job處理過的數據,而要保留正常job的處理結果,可是或許你沒法判斷後面變化的數據哪些是正常job的,哪些是錯誤job的,所以,很難進行部分數據的修復工做。


   10g提供了flashback versions query 和 flashback transaction query 特性,它強化了9i 的flashback query 特性,正是解決上面相似問題的利器。
   
1.flashback versions query
   flashback versions query 可以獲得某個時間段內,某些數據行的全部不一樣版本。這裏的版本以事務爲單位,事務中的每次數據變化就是一個版本。
舉例說明,下面的會話2次update id=1 時的 name 值,所以分別提交過2次,所以是2個不一樣的事務,在flashback versions query 的結果中,關於
id=1 ,咱們看到了3個不一樣的版本,一個是原來的值,一個是第一次修改的值,一個是第二次修改的值;經過flashback versions query 中的versions_startscn,versions_starttime
versions_endscn, versions_endtime,versions_xid, versions_operation 等僞列,咱們也得到了相關版本的開始scn,結束scn,開始時間和結束時間,以及作dml操做的具體transaction id (xid),以及操做類型等;
同時,咱們也看到對於id=1的數據行,三個版本的VERSIONS_ENDSCN,VERSIONS_ENDtime 和 VERSIONS_startscn,VERSIONS_starttime 是承上啓下的,一個版本的結束,也就是下一個版本的開始。


15:37:22 SQL> select *  from test;

        ID NAME
---------- --------------------
         1 kkk
         2 fff
         4 kkk

Executed in 0.031 seconds

15:37:34 SQL> update   test set name='TEST' where id=1;

1 row updated

Executed in 0.015 seconds

15:37:54 SQL> commit;

Commit complete

Executed in 0 seconds

15:37:56 SQL> update   test set name='TEST_new' where id=1;

1 row updated

Executed in 0.015 seconds

15:38:04 SQL> commit;

Commit complete

Executed in 0 seconds

15:38:06 SQL> 
15:39:12 SQL> SELECT versions_startscn, versions_starttime,
           2                      versions_endscn, versions_endtime,
           3                      versions_xid, versions_operation,
           4                      id,name
           5                 FROM test
           6               VERSIONS BETWEEN TIMESTAMP
           7                     TO_TIMESTAMP('2008-04-27 15:37:30', 'YYYY-MM-DD HH24:MI:SS')
           8               AND TO_TIMESTAMP('2008-04-27 15:38:30', 'YYYY-MM-DD HH24:MI:SS')
           9  ;

VERSIONS_STARTSCN VERSIONS_STARTTIME                                                               VERSIONS_ENDSCN VERSIONS_ENDTIME                                                                 VERSIONS_XID     VERSIONS_OPERATION         ID NAME
----------------- -------------------------------------------------------------------------------- --------------- -------------------------------------------------------------------------------- ---------------- ------------------ ---------- --------------------
           819393 27-4月 -08 03.38.06 下午                                                                                                                                                          040015006F010000 U                           1 TEST_new
           819387 27-4月 -08 03.37.54 下午                                                                  819393 27-4月 -08 03.38.06 下午                                                         0500150088010000 U                           1 TEST
                                                                                                            819387 27-4月 -08 03.37.54 下午                                                                                                      1 kkk
                                                                                                                                                                                                                                                 2 fff
                                                                                                                                                                                                                                                 4 kkk

Executed in 0.031 seconds

15:39:13 SQL> select *  from test;

        ID NAME
---------- --------------------
         1 TEST_new
         2 fff
         4 kkk

Executed in 0 seconds

15:43:52 SQL> 


2. flashback transaction query
    flashback transaction query 其實就是查詢歷史的事務信息,經過查詢,咱們能夠獲得過去某個事務操做信息,包括改變的數據行rowid,事務開始和結束時間,事務對應的loggon user,以及用來撤銷某個數據行改變的undo sql等。
flashback transaction query 其實就是查詢 FLASHBACK_TRANSACTION_QUERY 視圖。

好比,經過上面 versions query 獲得的xid,咱們能夠獲得這些xid 對應的信息。


15:43:52 SQL> SELECT xid, operation, start_scn,commit_scn, logon_user, undo_sql  FROM flashback_transaction_query  WHERE xid = HEXTORAW('&1');

XID              OPERATION                         START_SCN COMMIT_SCN LOGON_USER                     UNDO_SQL
---------------- -------------------------------- ---------- ---------- ------------------------------ --------------------------------------------------------------------------------
040015006F010000 UPDATE                               819387     819393 TEST1                          update "TEST1"."TEST" set "NAME" = 'TEST' where ROWID = 'AAAM1/AAEAAAAGsAAA';
040015006F010000 BEGIN                                819387     819393 TEST1                          

Executed in 0.297 seconds

15:57:34 SQL> SELECT xid, operation, start_scn,commit_scn, logon_user, undo_sql  FROM flashback_transaction_query  WHERE xid = HEXTORAW('&1');

XID              OPERATION                         START_SCN COMMIT_SCN LOGON_USER                     UNDO_SQL
---------------- -------------------------------- ---------- ---------- ------------------------------ --------------------------------------------------------------------------------
0500150088010000 UPDATE                               819184     819387 TEST1                          update "TEST1"."TEST" set "NAME" = 'kkk' where ROWID = 'AAAM1/AAEAAAAGsAAA';
0500150088010000 BEGIN                                819184     819387 TEST1                          

Executed in 0.032 seconds

15:58:07 SQL> 


3.一個例子
flashback versions query 和flashback transaction query 通常都是結合起來用的,經過前者獲得某個時間段的某些數據的多個版本的xid,而後經過後者獲得具體的xid的操做信息及undo_sql,最後根據須要作適當的數據修復操做。


SQL> create table  test  (id number ,name varchar2(20));

Table created

SQL> set time on
14:55:14 SQL> set timing on
14:55:18 SQL> insert into test values ( 1,'kkk');

1 row inserted

Executed in 0.016 seconds

14:55:27 SQL> insert into test values ( 2,'fff');

1 row inserted

Executed in 0.016 seconds

14:55:34 SQL> commit;

Commit complete

Executed in 0 seconds

14:55:39 SQL> 


此時打開第二個會話窗口,執行以下:
SQL> insert into test1.test values ( 4,'kkk');

1 row inserted

SQL> commit;

Commit complete

SQL>

--再回到第一個窗口,進行 flashback versions query 

15:00:20 SQL> SELECT versions_startscn, versions_starttime,
           2         versions_endscn, versions_endtime,
           3         versions_xid, versions_operation,
           4         id,name
           5    FROM test
           6  VERSIONS BETWEEN TIMESTAMP
           7        TO_TIMESTAMP('2008-04-27 14:55:20', 'YYYY-MM-DD HH24:MI:SS')
           8  AND TO_TIMESTAMP('2008-04-27 14:57:00', 'YYYY-MM-DD HH24:MI:SS')
           9  ;

VERSIONS_STARTSCN VERSIONS_STARTTIME                                                               VERSIONS_ENDSCN VERSIONS_ENDTIME                                                                 VERSIONS_XID     VERSIONS_OPERATION         ID NAME
----------------- -------------------------------------------------------------------------------- --------------- -------------------------------------------------------------------------------- ---------------- ------------------ ---------- --------------------
           817371 27-4月 -08 02.55.39 下午                                                                                                                                                          0900070081010000 I                           2 fff
           817371 27-4月 -08 02.55.39 下午                                                                                                                                                          0900070081010000 I                           1 kkk
           817386 27-4月 -08 02.56.03 下午                                                                                                                                                          0700280072010000 I                           4 kkk

Executed in 0.016 seconds

15:00:21 SQL> 


-- flashback  transaction query 
15:00:21 SQL> SELECT xid, operation, start_scn,commit_scn, logon_user, undo_sql  FROM flashback_transaction_query  WHERE xid = HEXTORAW('&1');

XID              OPERATION                         START_SCN COMMIT_SCN LOGON_USER                     UNDO_SQL
---------------- -------------------------------- ---------- ---------- ------------------------------ --------------------------------------------------------------------------------
0900070081010000 INSERT                               817359     817371 TEST1                          delete from "TEST1"."TEST" where ROWID = 'AAAM1/AAEAAAAGsAAB';
0900070081010000 INSERT                               817359     817371 TEST1                          delete from "TEST1"."TEST" where ROWID = 'AAAM1/AAEAAAAGsAAA';
0900070081010000 BEGIN                                817359     817371 TEST1                          

Executed in 0.593 seconds

15:03:54 SQL> 
15:11:27 SQL> 
15:11:27 SQL> 
15:11:27 SQL> SELECT xid, operation, start_scn,commit_scn, logon_user, undo_sql  FROM flashback_transaction_query  WHERE xid = HEXTORAW('&1');

XID              OPERATION                         START_SCN COMMIT_SCN LOGON_USER                     UNDO_SQL
---------------- -------------------------------- ---------- ---------- ------------------------------ --------------------------------------------------------------------------------
0700280072010000 INSERT                                    0     817386 TEST2                          delete from "TEST1"."TEST" where ROWID = 'AAAM1/AAEAAAAGwAAA';
0700280072010000 BEGIN                                     0     817386 TEST2                          

Executed in 0.031 seconds

15:11:47 SQL> 

4.undo_rentention

和9i flashback query 同樣,flashback  versions query和flashback  transaction query 也都是依賴於undo tablespace,依賴於undo_retention 的。換句話說,對於已經不存在於undo tablespace 的數據版本信息,
你固然是沒法經過 這些特性把它查出來的。所以設置好undo tablespace 的大小,設置好undo_rentention 的大小就尤其重要(10g 雖然有自動tuning undo_retention 的功能,不過考量並設置合適的undo_retention 最小值也是頗有必要的)

相關文章
相關標籤/搜索