闪回版本查询

  • DBAs can use Oracle Flashback Version Query to retrieve the different versions of specific rows that existed during a given time interval. A row version is created whenever a COMMIT statement is executed.
    闪回版本的作用是查询指定行的不同的版本数据,也就是指定行在过去的不同值
  • 规则:(查询真实表)
    SELECT [pseudo_columns]...FROM table_name
    VERSION BETWEEN
    {SCN | TIMESTAMP {expr | MINVALUE} AND
    {expr | MAXVALUE}}
    [AS OF {SCN|TIMESTAMP expr}]
    WHERE [pseudo_column | column] . .
  • 虚列
    VERSIONS_STARTSCN The SCN at which this version of the row was created
    VERSIONS_STARTTIME The time stamp at which this version of the row was created
    VERSIONS_ENDSCN The SCN at which this row no longer existed (either changed or deleted)
    VERSIONS_ENDTIME The time stamp at which this row no longer existed (either changed or deleted)
    VERSIONS_XID The transaction ID of the transaction that created this version of the rows
    VERSIONS_OPERATION The operation done by this transaction: I=Insert, D=Delete, U=Update
  • MINVALUE and MAXVALUE resolve to the SCN or time stamp of the oldest and most recent data available, respectively

  • 下面是一个比较典型的闪回版本查询

    1. COLUMN versions_startscn FORMAT 99999999999999999
    2. COLUMN versions_starttime FORMAT A24
    3. COLUMN versions_endscn FORMAT 99999999999999999
    4. COLUMN versions_endtime FORMAT A24
    5. COLUMN versions_xid FORMAT A16
    6. COLUMN versions_operation FORMAT A1
    7. COLUMN description FORMAT A11
    8. SET LINESIZE 200
    9. SELECT versions_startscn,
    10. versions_starttime,
    11. versions_endscn,
    12. versions_endtime,
    13. versions_xid,
    14. versions_operation,
    15. last_name, salary
    16. FROM personnel
    17. VERSIONS BETWEEN TIMESTAMP
    18. TO_TIMESTAMP('2007-03-21 19:30:00', 'YYYY-MM-DD HH24:MI:SS')
    19. AND TO_TIMESTAMP('2007-03-22 04:30:00', 'YYYY-MM-DD HH24:MI:SS')
    20. WHERE first_name = 'Anthony';

    也可以使用MINVALUE和MAXVALUE定界

    1. SQL> SELECT versions_xid AS XID, versions_startscn AS START_SCN, versions_endscn AS END_SCN, versions_operation AS OPERATION, first_name FROM employees VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE AS OF SCN 5525300 WHERE employee_id = 111;
    1. SQL> select versions_startscn startscn,versions_endscn endscn, versions_xid xid, versions_operation oper, employee_id empid, last_name name, salary sal from hr.employees versions between timestamp trunc(systimestamp) and systimestamp where employee_id = 124;

Example

  1. SQL> SELECT * FROM test1;
  2. ID DESCR
  3. ---------- ------------------------------
  4. 1 One
  5. 2 Two
  6. 3 Three
  7. SQL> SELECT current_scn, SCN_TO_TIMESTAMP(current_scn) FROM v$database;
  8. CURRENT_SCN SCN_TO_TIMESTAMP(CURRENT_SCN)
  9. ----------- ---------------------------------------------------------------------------
  10. 29006118 15-AUG-11 12.51.21.000000000 PM
  11. SQL> UPDATE test1 SET descr='The one' WHERE id=1;
  12. 1 ROW updated.
  13. SQL> COMMIT;
  14. COMMIT complete.
  15. SQL> UPDATE test1 SET descr='The only one' WHERE id=1;
  16. 1 ROW updated.
  17. SQL> COMMIT;
  18. COMMIT complete.
  19. SQL> SELECT current_scn, SCN_TO_TIMESTAMP(current_scn) FROM v$database;
  20. CURRENT_SCN SCN_TO_TIMESTAMP(CURRENT_SCN)
  21. ----------- ---------------------------------------------------------------------------
  22. 29006142 15-AUG-11 12.52.03.000000000 PM

Remark:
Please note the commit after each update to generate multiple transactions.

You can now see past figures:

  1. SQL> SELECT versions_startscn, versions_starttime, versions_endscn, versions_endtime, versions_xid, versions_operation, id, descr
  2. FROM test1
  3. VERSIONS BETWEEN SCN 29006118 AND 29006142
  4. WHERE id = 1;
  5. VERSIONS_STARTSCN VERSIONS_STARTTIME VERSIONS_ENDSCN VERSIONS_ENDTIME VERSIONS_XID V ID DESCR
  6. ----------------- ------------------------ --------------- ------------------------ ---------------- - ---------- ------------------------------
  7. 29006133 15-AUG-11 12.51.45 PM 0A001500B99F0000 U 1 The only one
  8. 29006128 15-AUG-11 12.51.36 PM 29006133 15-AUG-11 12.51.45 PM 060017000A100000 U 1 The one
  9. 29006128 15-AUG-11 12.51.36 PM 1 One
  10. SQL> SELECT * FROM test1 AS OF scn 29006128;
  11. ID DESCR
  12. ---------- ------------------------------
  13. 1 The one
  14. 2 Two
  15. 3 Three
  16. SQL> SELECT * FROM test1 AS OF scn 29006118;
  17. ID DESCR
  18. ---------- ------------------------------
  19. 1 One
  20. 2 Two
  21. 3 Three
  22. SQL> SELECT * FROM test1;
  23. ID DESCR
  24. ---------- ------------------------------
  25. 1 The only one
  26. 2 Two
  27. 3 Three

Remark:
Same as DBMS_FLASHBACK package you may work with timestamp or SCN.

http://blog.yannickjaquier.com/oracle/flashback-technologies-by-examples.html#Flashback_Versions_Query
https://oracle-base.com/articles/10g/flashback-10g#flashback_version_query
http://www.oracle-dba-online.com/flash_back_features.htm





原文地址:https://www.cnblogs.com/haoxiaoyu/p/e6813e6a8d096dbb8822daf4c8dd3535.html