具备了flashback version query查询的基础,我们就可以进行基于flashback version query的恢复.
这就是flashback transaction query。flashback transaction query可以从FLASHBACK_TRANSACTION_QUERY中获得指定事务的历史信息以及Undo_SQL,通过这个UNDO_SQL,我们就可以恢复特定的事务。
Flashback transaction query需要用到FLASHBACK_TRANSACTION_QUERY视图,我们先看一下视图

SQL> desc FLASHBACK_TRANSACTION_QUERY;
Name Type Nullable Default Comments
---------------- -------------- -------- ------- --------------------------
XID RAW(8) Y Transaction identifier
START_SCN NUMBER Y Transaction start SCN
START_TIMESTAMP DATE Y Transaction start timestamp
COMMIT_SCN NUMBER Y Transaction commit SCN
COMMIT_TIMESTAMP DATE Y Transaction commit timestamp
LOGON_USER VARCHAR2(30) Y Logon user for transaction
UNDO_CHANGE# NUMBER Y 1-based undo change number
OPERATION VARCHAR2(32) Y forward operation for this undo
TABLE_NAME VARCHAR2(256) Y table name to which this undo applies
TABLE_OWNER VARCHAR2(32) Y owner of table to which this undo applies
ROW_ID VARCHAR2(19) Y rowid to which this undo applies
UNDO_SQL VARCHAR2(4000) Y SQL corresponding to this undo
该视图的定义为:

select xid, start_scn, start_timestamp,
decode(commit_scn, 0, commit_scn, 281474976710655, NULL, commit_scn)
commit_scn, commit_timestamp,
logon_user, undo_change#, operation, table_name, table_owner,
row_id, undo_sql
from sys.x$ktuqqry

参考前面的文章,我们通过FLASHBACK_TRANSACTION_QUERY来恢复事务。 flashback version query参考:Oracle10g的Flashback version Query 1.通过flashback version query获得XID

EYGLE  on 30-MAR-05 >select versions_starttime, versions_endtime, versions_xid,
2 versions_operation, username,user_id
3 from t versions between timestamp minvalue and maxvalue
4 / VERSIONS_STARTTIME VERSIONS_ENDTIME VERSIONS_XID V USERNAME USER_ID
------------------------------ ------------------------------ ---------------- - ---------- ----------
30-MAR-05 09.34.49 AM 000A000B000000F1 D DBSNMP 22
30-MAR-05 09.34.49 AM 000A000B000000F1 D WMSYS 23
30-MAR-05 09.34.49 AM 000A000B000000F1 D OPERATOR 31
30-MAR-05 09.34.49 AM 000A000B000000F1 D TRANS 27
30-MAR-05 09.34.49 AM 000A000B000000F1 D DIP 19
30-MAR-05 09.34.49 AM 000A000B000000F1 D SCOTT 29
30-MAR-05 09.34.49 AM 000A000B000000F1 D TEST 25
30-MAR-05 09.34.15 AM 0001001900000F0F U EYGLE 1
30-MAR-05 09.33.51 AM 00080016000000EF D TEST1 28
30-MAR-05 09.33.23 AM 0004000A000005EF D OUTLN 11
SYSTEM 5 VERSIONS_STARTTIME VERSIONS_ENDTIME VERSIONS_XID V USERNAME USER_ID
------------------------------ ------------------------------ ---------------- - ---------- ----------
SYS 0
30-MAR-05 09.34.49 AM TEST 25
30-MAR-05 09.34.15 AM EYGLE 26
30-MAR-05 09.34.49 AM SCOTT 29
30-MAR-05 09.34.49 AM DIP 19
30-MAR-05 09.34.49 AM TRANS 27
30-MAR-05 09.33.51 AM TEST1 28
30-MAR-05 09.34.49 AM OPERATOR 31
30-MAR-05 09.34.49 AM WMSYS 23
30-MAR-05 09.34.49 AM DBSNMP 22
30-MAR-05 09.33.23 AM OUTLN 11 VERSIONS_STARTTIME VERSIONS_ENDTIME VERSIONS_XID V USERNAME USER_ID
------------------------------ ------------------------------ ---------------- - ---------- ----------
30-MAR-05 09.49.24 AM 00080006000000EF I PENNY 2 23 rows selected.

2.恢复XID=000A000B000000F1的事务 注意:观察到x$ktuqqry的查询非常耗时,所以请注意评估你的恢复成本。 由于x$ktuqqry表的xid字段上不存在索引(很奇怪,而且我们不能自己添加,估计后续版本中会有改进),查询该表会导致犬表扫描。 以下测试中,x$ktuqqry中存在大约19万记录,查询一次需要近6分钟。

SYS AS SYSDBA on 30-MAR-05 >select count(addr) from x$ktuqqry;  

COUNT(ADDR)
-----------
196015 SYS AS SYSDBA on 30-MAR-05 >set autotrace on
SYS AS SYSDBA on 30-MAR-05 >SELECT /*+ rule */ UNDO_SQL FROM FLASHBACK_TRANSACTION_QUERY
2 WHERE XID = '000A000B000000F1'; UNDO_SQL
--------------------------------------------------------------------------------
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('DBSNMP','22');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('WMSYS','23');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('OPERATOR','31');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('TRANS','27');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('DIP','19');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('SCOTT','29');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('TEST','25'); 8 rows selected. Elapsed: 00:05:53.44 Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=HINT: RULE
1 0 FIXED TABLE (FULL) OF 'X$KTUQQRY' (TABLE (FIXED)) Statistics
----------------------------------------------------------
393399 recursive calls
0 db block gets
1559016 consistent gets
4388 physical reads
0 redo size
1069 bytes sent via SQL*Net to client
664 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
23164 sorts (memory)
0 sorts (disk)
8 rows processed SYS AS SYSDBA on 30-MAR-05 >SELECT UNDO_SQL FROM FLASHBACK_TRANSACTION_QUERY
2 WHERE XID = '000A000B000000F1'; UNDO_SQL
--------------------------------------------------------------------------------
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('DBSNMP','22');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('WMSYS','23');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('OPERATOR','31');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('TRANS','27');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('DIP','19');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('SCOTT','29');
insert into "EYGLE"."T"("USERNAME","USER_ID") values ('TEST','25'); 8 rows selected. Elapsed: 00:05:55.30 Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=25 Card=1 Bytes=2008)
1 0 FIXED TABLE (FULL) OF 'X$KTUQQRY' (TABLE (FIXED)) (Cost=25 Card=1 Bytes=2008) Statistics
----------------------------------------------------------
393454 recursive calls
0 db block gets
1562425 consistent gets
4644 physical reads
0 redo size
1069 bytes sent via SQL*Net to client
664 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
23166 sorts (memory)
0 sorts (disk)
8 rows processed SYS AS SYSDBA on 30-MAR-05 >

3.通过UNDO语句我们可以撤销该事物。 4.注意,在Oracle Database 11g中,需要启用supplemental log data才能够得到UNDO SQL。 以下测试过程供参考:

SQL> connect / as sysdba
Connected.
SQL> alter database add supplemental log data; Database altered. SQL> connect eygle/eygle
Connected. SQL> create table t as select username from dba_users where rownum < 6; Table created. SQL> select * from t; USERNAME
------------------------------
SYS
SYSTEM
OUTLN
DIP
ORACLE_OCM 5 rows selected. SQL> delete from t where username='DIP'; 1 row deleted. SQL> commit; Commit complete. SQL> select versions_xid,versions_operation,username from t versions between timestamp minvalue and maxvalue; VERSIONS_XID V USERNAME
---------------- - ------------------------------
06001400D60C0000 D DIP
SYS
SYSTEM
OUTLN
DIP
ORACLE_OCM 6 rows selected. SQL> select xid,operation,undo_sql from FLASHBACK_TRANSACTION_QUERY where xid=hextoraw('06001400D60C0000'); XID OPERATION UNDO_SQL
---------------- -------------------------------- ------------------------------------------------------------
06001400D60C0000 DELETE insert into "EYGLE"."T"("USERNAME") values ('DIP');
06001400D60C0000 BEGIN 2 rows selected.

此处的UNDO_SQL显示为两行,其中一条为BEGIN,这是作为事务开始的标记。Oracle隐式的开始一个事务,在Reverse转换时,BEGIN被显示出来,而且在INSERT之后:

SQL> select xid,operation,undo_sql,undo_change# from FLASHBACK_TRANSACTION_QUERY where xid=hextoraw('06001400D60C0000');

XID		 OPERATION  UNDO_SQL							 UNDO_CHANGE#
---------------- ---------- ------------------------------------------------------------ ------------
06001400D60C0000 DELETE insert into "EYGLE"."T"("USERNAME") values ('DIP'); 1
06001400D60C0000 BEGIN

利用flashback transaction query新特性进行事务撤销的更多相关文章

  1. Flashback Version Query、Flashback Transaction Query快速闪回细粒度数据

    Flashback Version Query 闪回版本查询 使用Flashback Version Query  返回在指定时间间隔或SCN间隔内的所有版本,一次commit命令就会创建一个版本. ...

  2. Oracle Flashback Transaction Query with Oracle Flashback Version Query

    Oracle Flashback Transaction Query with Oracle Flashback Version Query In this example, a database a ...

  3. MongoDB 4.2新特性:分布式事务、字段级加密、通配符索引、物化视图

    MongoDB 4.2已经发布,我们来看看它增加了哪些新特性?分布式事务?数据库加密?通配符索引? 在2019年MongoDB World大会上,CTO Eliot Horowitz介绍了MongoD ...

  4. 利用Mysql5.7的新特性实现多机房高可用架构【转】

    再牛逼的架构也敌不过挖掘机,无论单机房内你的架构多么的高可用,多么的完善,当挖掘机挖下去那一瞬间,都是扯蛋,楼主所在的公司也被挖掘机挖断过光纤.电力线. 为什么大家都在谈论服务冗余,缓存击穿等高可用时 ...

  5. Flashback version/Transaction Query,FlashbackTable

    Flashback version Query相对于Flashback Query 只能看到某一点的对象状态, Oracle 10g引入的Flashback Version Query可以看到过去某个 ...

  6. Flashback Version/Transaction Query

    1.应用Flashback Version Query查询记修改版本 SQL> select dbms_flashback.get_system_change_number from dual; ...

  7. 11G新特性 -- flashback data archive(1)

    虽然可以依赖undo数据来查询row的旧版本数据,甚至可以执行逻辑恢复.但是你不能期待在undo中找到非常旧的数据.undo数据主要是用来提供读一致性. 在11G中,提供了Flashback Data ...

  8. PLSQL_闪回操作3_Fashback Transaction Query

    2014-12-09 Created By BaoXinjian

  9. 闪回之 Flashback Query (dml表、过程、函数、包等)、Flashback version Query

    Flashback Query 背景:Flashback 是 ORACLE 自 9i 就开始提供的一项特性,在 9i 中利用oracle 查询多版本一致的特点,实现从回滚段中读取表一定时间内操作过的数 ...

随机推荐

  1. A - Dubstep

    Problem description Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep mus ...

  2. Java 系列之spring学习--spring搭建(一)

    一.新建maven项目 二.引入spring jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...

  3. classname在JavaScript中的应用

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  4. 更改 AVD 默认存放位置

    AVD Manager 创建的 Android 模拟器(AVD)默认存放位置为C:\Users\<user>\.android\avd,我创建了2个AVD,一共用了近9G!是要挪挪地方了. ...

  5. 电商物流仓储WMS业务流程

    电商物流仓储WMS业务流程 SKU是什么意思?  一文详解电商仓储管理中SKU的含义 从货品角度看,SKU是指单独一种商品,其货品属性已经被确定.只要货品属性有所不同,那么就是不同的SKU. PO信息 ...

  6. MySQL 5.6 Reference Manual-14.5 InnoDB Tablespace Management

    14.5 InnoDB Tablespace Management   14.5.1 Resizing the InnoDB System Tablespace 14.5.2 Changing the ...

  7. [Intermediate Algorithm] - Smallest Common Multiple

    题目 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 范围是两个数字构成的数组,两个数字不一定按数字顺序排序. 例如对 1 和 3 —— 找出能被 1 和 3 和它们之间所有数字整除的最小 ...

  8. 初步使用vue中axios

    1.下载axios npm install axios --save 2.两种方式使用axios (1)在模块中引入axios 例如:我在用户登陆界面需要使用axios,就在login页面引入,不是全 ...

  9. Edit Distance FZU-1434

    题目大意: 给你两个字符串A,B,和以下三种操作: 1.删除一个字符 2.插入一个字符 3.把一个字符改变成另一个字符 求使A变成B所需要的最少的操作: 我刚开始的思路是以为求出最长公共子序列,然后对 ...

  10. 洛谷P1055 ISBN号码

    题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括 999 位数字. 111 位识别码和 333 位分隔符,其规定格式如x-xxx-xxxxx-x,其中符号-就是分隔符(键盘上 ...