闪回版本查询

  • 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

来自为知笔记(Wiz)

时间: 2024-10-09 07:24:05

闪回版本查询的相关文章

[课]10.1闪回查询的三种方式:闪回查询/闪回版本查询/闪回事务查询

数据库版本 1.1闪回查询演示 1.2闪回版本查询演示 1.3闪回事务查询演示 在做闪回事务查询时候,我们需要使用ORACLE提供的一个系统视图FLASHBACK_TRANSACTION_QUERY.闪回事务查询与闪回版本查询之间有着密切的关系,从刚才我们实验的闪回版本查询中可以知道有一个伪列VERSIONS_XID,那么闪回事务查询就是通过这个伪列与闪回版本查询发生关联. 我们现在查看一下该表的表结构: 开始演示:

闪回事务查询

原理:基于undo 闪回事务查询可以看作一个在事务级别查询数据库改变的一个诊断工具 FLASHBACK_TRANSACTION_QUERY retrieve transaction information for all tables involved in a transaction. provides the SQL statements that you can use to undo the changes made by a particular transaction 需要打开最小附

Oracle 闪回技术

一.配置闪回数据库 1.数据库处于归档日志模式 SQL> select log_mode from v$database; LOG_MODE ------------ ARCHIVELOG 2.创建闪回恢复区 SQL> alter system set db_recovery_file_dest_size=10G; System altered. SQL> alter system set db_recovery_file_dest='D:\oracle\product\11.2.0\d

【读书笔记】oracle闪回技术

1.闪回技术有三种,分别是闪回数据库.闪回查询.闪回删除. 闪回数据库是把数据库恢复到某一个时间点:闪回查询是查询表的历史记录:闪回删除是把drop掉的表还原回来(表存在于recyclebin中). 2.三种闪回技术的限制: 闪回数据库受闪回日志大小的限制: 闪回查询受撤销保留时间限制: 闪回删除受表可用空间限制: 但可以通过配置Flashback Data Archive来无限期地存储行的前映像.(如何对表启用?) 3.闪回数据库的配置: 1)确保数据库处于归档模式中,如果不是归档模式则要进行

ORACLE 10G 闪回建议

Flashback Tips The following tips and restrictions apply to using flashback features. Flashback Tips – Performance For better performance, generate statistics on all tables involved in a Flashback Query by using the DBMS_STATS package, and keep the s

oracle闪回那些事

开启闪回数据库 alter database flashback on; alter database open; 修改日期时间显示格式: alter session  set nls_date_format='yyyy-mm-dd hh24:mi:ss'; 查看系统视图 v$flashback_database_log中查看闪回数据库日志信息: select * from v$flashback_database_log; 闪回数据库: flashback database to timest

Oracle OCP笔记(31)使用闪回功能

Oracle OCP笔记(31)使用闪回功能 Oracle的闪回功能受到3个数据库结构之一的支持: 撤消数据.闪回恢复区和回收站. 撤消表空间中的撤销数据不仅支持事务回滚,也支持大多数闪回表操作.Flashback Data Archives允许查询先前版本的表行,它在撤销表空间之外的一个或多个表空间中提供一个区域,支持的保留期比撤销表空间还长.闪回日志保存在闪回恢复区中,从而无需执行传统的还原和恢复操作,即可方便地将整个数据库回滚到过去的时间点.每个表空间中的回收站包含一个或多个版本的已删除的

Oracle学习(15)【DBA向】:闪回

闪回Flashback 什么是闪回 l在Oracle的操作工程中,会不可避免地出现操作失误或者用户失误,例如不小心删除了一个表等,这些失误和错误可能会造成重要数 据的丢失,最终导致Oracle数据库停止. l在传统意义上,当发生数据丢失.数据错误问题时,解决的主要办法是数据的导入导出.备份恢复技术,这些方法都需要在发生错误 前,有一个正确的备份才能进行恢复. l为了减少这方面的损失,Oracle提供了闪回技术.有了闪回技术,就可以实现数据的快速恢复,而且不需要数据备份. 闪回的好处 恢复中,闪回

闪回的用途与实战

闪回可以做的操作有如下几种类型: 1.当数据错误删除,并且提交时(flashback table) 2.当错误删除了一张表drop table(flashback drop) 3.通过闪回获取表的历史记录(flashback version query) 4.当错误地执行了一个事务,并且提交了(flashback transaction query) 5.将整个数据库进行恢复. 使用闪回条件: 1.切换到管理员上. 假设开始登录的用户名和密码是MISPS 如果是连接远程的,连接sqlplus的方