oracle 之flashback 深入研究。

今天是2013-08-24,开始进行oracle flashback 内部原理研究,记录一下笔记。

SQL> startup
ORACLE instance started.

Total System Global Area  405020672 bytes
Fixed Size                  2213816 bytes
Variable Size             260048968 bytes
Database Buffers          138412032 bytes
Redo Buffers                4345856 bytes
Database mounted.
Database opened.
SQL>
SQL> select * from t1;

ID        SAL JOB
---------- ---------- ----------
         1          9 z
         1          2 a
         2          3 b
         3          4 c
         1          3 b
         2          1 a
                      D

7 rows selected.

SQL> select xidusn,ubafil,ubablk from v$transaction;

no rows selected

SQL> delete from t1 where id=3;

1 row deleted.

SQL> select xidusn,ubafil,ubablk from v$transaction;

XIDUSN     UBAFIL     UBABLK
---------- ---------- ----------
        17          8       2583

SQL> alter system dump datafile 8 block 2583;

System altered.

SQL> set vlaue for a60
SP2-0158: unknown SET option "vlaue"
SQL> col value for a60
SQL> set linesize 200
SQL> select * from v$diag_info where name='Default Trace File';

INST_ID NAME                                                             VALUE
---------- ---------------------------------------------------------------- ------------------------------------------------------------
         1 Default Trace File                                               /opt/app/oracle/diag/rdbms/rhys/RHYS/trace/RHYS_ora_1801.trc

SQL> commit;

Commit complete.

UNDO BLK:
xid: 0x0011.019.000000b4  seq: 0x23c cnt: 0x46  irb: 0x46  icl: 0x0   flg: 0x0000

Rec Offset      Rec Offset      Rec Offset      Rec Offset      Rec Offset
---------------------------------------------------------------------------
0x01 0x1f90     0x02 0x1f4c     0x03 0x1ef4     0x04 0x1eb0     0x05 0x1e58
0x06 0x1e14     0x07 0x1dbc     0x08 0x1d78     0x09 0x1d20     0x0a 0x1cdc
0x0b 0x1c84     0x0c 0x1c40     0x0d 0x1be8     0x0e 0x1ba4     0x0f 0x1b4c
0x10 0x1b08     0x11 0x1ab0     0x12 0x19d0     0x13 0x18f0     0x14 0x1868
0x15 0x1800     0x16 0x1794     0x17 0x1620     0x18 0x1508     0x19 0x1480
0x1a 0x1414     0x1b 0x13a8     0x1c 0x1224     0x1d 0x119c     0x1e 0x1134
0x1f 0x10c8     0x20 0x0f4c     0x21 0x0edc     0x22 0x0e54     0x23 0x0de8
0x24 0x0d7c     0x25 0x0d38     0x26 0x0ca0     0x27 0x0c14     0x28 0x0bd0
0x29 0x0b74     0x2a 0x0b18     0x2b 0x0ad4     0x2c 0x0a78     0x2d 0x0a1c
0x2e 0x09d8     0x2f 0x097c     0x30 0x0920     0x31 0x08dc     0x32 0x0880
0x33 0x0824     0x34 0x07dc     0x35 0x0778     0x36 0x071c     0x37 0x06d8
0x38 0x067c     0x39 0x0620     0x3a 0x05dc     0x3b 0x0580     0x3c 0x0524
0x3d 0x04e0     0x3e 0x0484     0x3f 0x0428     0x40 0x0340     0x41 0x02b4
0x42 0x0250     0x43 0x01e8     0x44 0x016c     0x45 0x0114     0x46 0x00d0

*-----------------------------
* Rec #0x22  slt: 0x19  objn: 468(0x000001d4)  objd: 468  tblspc: 1(0x00000001)
*       Layer:  11 (Row)   opc: 1   rci 0x00
Undo type:  Regular undo    Begin trans    Last buffer split:  No
Temp Object:  No
Tablespace Undo:  No
rdba: 0x00000000Ext idx: 0
flg2: 0
*-----------------------------
uba: 0x02000a17.023c.21 ctl max scn: 0x0000.00a82f35 prv tx scn: 0x0000.00a82f6f
txn start scn: scn: 0x0000.00a83315 logon user: 0
 prev brb: 33580056 prev bcl: 0
KDO undo record:
KTB Redo
op: 0x04  ver: 0x01
compat bit: 4 (post-11) padding: 1
op: L  itl: xid:  0x0010.000.000000cb uba: 0x02006433.024f.39
                      flg: C---    lkc:  0     scn: 0x0000.00a832c7
KDO Op code: DRP row dependencies Disabled
  xtype: XA flags: 0x00000000  bdba: 0x0080f7a9  hdba: 0x0080039a
itli: 1  ispac: 0  maxfr: 4858
tabn: 0 slot: 27(0x1b)

SQL> select to_number('a82f35','xxxxxxxxxxxxxxxx') sc from dual;

SC
----------
  11022133

SQL> select current_scn from v$database;

CURRENT_SCN
-----------
   11054748

SQL>
注意:闪回查询和undo_retention有直接关系;
eg:
SQL> update t1 set job='ab' where id=1;

3 rows updated.

SQL>  select versions_starttime,versions_endtime,versions_xid,versions_operation,id,sal from t1 versions between timestamp minvalue and maxvalue
  2  ;

VERSIONS_STARTTIME                                                          VERSIONS_ENDTIME     VERSIONS_XID     V         ID        SAL
--------------------------------------------------------------------------- -------------------- ---------------- - ---------- ----------
                                                                                                                             1          9
                                                                                                                             1          2
                                                                                                                             2          3
                                                                                                                             1          3
                                                                                                                             2          1

6 rows selected.

SQL> show parameter undo

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
_optimizer_undo_cost_change          string      11.2.0.1
_undo_autotune                       boolean     FALSE
undo_management                      string      AUTO
undo_retention                       integer     60
undo_tablespace                      string      UNDOTBS3
SQL> alter system set undo_retention=1800;

System altered.

SQL> select versions_starttime,versions_endtime,versions_xid,versions_operation,id,sal from t1 versions between timestamp minvalue and maxvalue;

VERSIONS_STARTTIME                                                          VERSIONS_ENDTIME     VERSIONS_XID     V         ID        SAL
--------------------------------------------------------------------------- -------------------- ---------------- - ---------- ----------
                                                                                                                             1          9
24-AUG-13 09.49.38 PM                                                                            11000300BF000000 D          3          4
                                                                                                                             1          2
                                                                                                                             2          3
                                                                            24-AUG-13 09.49.38 P                             3          4
                                                                            M

1          3
                                                                                                                             2          1

8 rows selected.

1)闪回查询:

SQL> select * from t1 ;

ID        SAL JOB
---------- ---------- ----------
         1          9 z
         1          2 a
         2          3 b
         1          3 b
         2          1 a
                      D

6 rows selected.

SQL> select * from t1 as of scn 11022133;

ID        SAL JOB
---------- ---------- ----------
         1          2 a
         2          3 b
         3          4 c
         1          3 b
         2          1 a
                      D

6 rows selected.

SQL>
2)闪回版本查询:
SQL> r
  1* select * from t1

ID        SAL JOB
---------- ---------- ----------
         1          9 ab
         1          2 ab
         2          3 b
         1          3 ab
         2          1 a
                      D

6 rows selected.

SQL> delete from t1 where id=1;

3 rows deleted.

SQL> commit;

Commit complete.

SQL> select versions_startscn,versions_endscn,versions_xid,versions_operation,id,sal,job from t1 versions between scn minvalue and maxvalue;

VERSIONS_STARTSCN VERSIONS_ENDSCN VERSIONS_XID     V         ID        SAL JOB
----------------- --------------- ---------------- - ---------- ---------- ----------
         11144497                 0D001600D8000000 D          1          9 z
                         11144497                             1          9 z
         11144497                 0D001600D8000000 D          1          3 b
         11144497                 0D001600D8000000 D          1          2 a
         11054404                 11000300BF000000 D          3          4 c
                         11144497                             1          2 a
                                                              2          3 b
                         11054404                             3          4 c
                         11144497                             1          3 b
                                                              2          1 a
                                                                           D

11 rows selected.

SQL>
SQL>
然后我们更具versions_xid找到相应的事务
3)事务查询;
借助flashback_transaction_query这个视图;
首先看一下 这个视图结构;
SSQL> desc flashback_transaction_query;                 
 Name                                                                                                              Null?    Type
 ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
 XID                                                                                                                        RAW(8)
 START_SCN                                                                                                                  NUMBER
 START_TIMESTAMP                                                                                                            DATE
 COMMIT_SCN                                                                                                                 NUMBER
 COMMIT_TIMESTAMP                                                                                                           DATE
 LOGON_USER                                                                                                                 VARCHAR2(30)
 UNDO_CHANGE#                                                                                                               NUMBER
 OPERATION                                                                                                                  VARCHAR2(32)
 TABLE_NAME                                                                                                                 VARCHAR2(256)
 TABLE_OWNER                                                                                                                VARCHAR2(32)
 ROW_ID                                                                                                                     VARCHAR2(19)
 UNDO_SQL                                                                                                                   VARCHAR2(4000)

SQL>

然后我们进行一次查询:
eg:

SQL> conn rhys/root
Connected.
SQL> select * from t1;

ID        SAL JOB
---------- ---------- ----------
         2          3 b
         3          1 a
                      D
         1          1 ab

SQL> delete from t1 where id=1;

1 row deleted.

SQL> commit;

Commit complete.

SQL> select versions_startscn,versions_endscn,versions_xid,versions_operation,id,sal,job from t1 versions between scn minvalue and maxvalue;

VERSIONS_STARTSCN VERSIONS_ENDSCN VERSIONS_XID     V         ID        SAL JOB
----------------- --------------- ---------------- - ---------- ---------- ----------
         11166640                 13000A00D9000000 U          3          1 a
                                                              2          3 b
                         11166640                             2          1 a
                                                                           D
         11187698                 13001300DB000000 D          1          1 ab
         11166640        11187698 13000A00D9000000 I          1          1 ab

6 rows selected.

SQL> select xid,logon_user,start_scn,operation,table_name,row_id,undo_sql from flashback_transaction_query where xid='13000A00D9000000';
select xid,logon_user,start_scn,operation,table_name,row_id,undo_sql from flashback_transaction_query where xid='13000A00D9000000'
                                                                          *
ERROR at line 1:
ORA-01031: insufficient privileges

SQL> conn sys/root as sysdba
Connected.
SQL> select current_scn from v$database;

CURRENT_SCN
-----------
   11187761

SQL> select xid,logon_user,start_scn,operation,table_name,row_id,undo_sql from flashback_transaction_query where xid='13001300DB000000';

XID              LOGON_USER                      START_SCN OPERATION
---------------- ------------------------------ ---------- --------------------------------
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROW_ID
-------------------
UNDO_SQL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
13001300DB000000 RHYS                                    0 UNKNOWN
T1

XID              LOGON_USER                      START_SCN OPERATION
---------------- ------------------------------ ---------- --------------------------------
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROW_ID
-------------------
UNDO_SQL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
13001300DB000000 RHYS                                    0 BEGIN

SQL>

但是没有任何undo_sql,查看网络发现如下:

#这里执行这个语句的意思是追加日志

supplemental logging 的定义 :

redo log一般用于实例恢复及介质恢复。在redo log中这些数据被自动记录。不过一些

基于redo的application可能在redo log中记录额外的一些列。 这种记录额外列的过程

被称为supplemental logging

缺省情况下,数据库没有打开对supplemental logging的支持。

然后重新做实验如下:

SQL> alter database add supplemental log data;

Database altered.

SQL> select * from t1;

ID        SAL JOB
---------- ---------- ----------
         2          3 b
         2          1 a
                      D

SQL> update t1 set id=3 where job='D';

1 row updated.

SQL> commit;

Commit complete.

SQL> select versions_startscn,versions_endscn,versions_xid,versions_operation,id,sal,job from t1 versions between scn minvalue and maxvalue;

VERSIONS_STARTSCN VERSIONS_ENDSCN VERSIONS_XID     V         ID        SAL JOB
----------------- --------------- ---------------- - ---------- ---------- ----------
         11144497                 0D001600D8000000 D          1          9 z
                         11144497                             1          9 z
         11211146                 0B001600DF000000 U          3            D
         11144497                 0D001600D8000000 D          1          3 b
         11144497                 0D001600D8000000 D          1          2 a
                         11144497                             1          2 a
                                                              2          3 b
                         11144497                             1          3 b
                                                              2          1 a
                         11211146                                          D

10 rows selected.

SQL> select xid,logon_user,start_scn,operation,table_name,row_id,undo_sql from flashback_transaction_query where xid='0B001600DF000000';
^CERROR:
ORA-01013: user requested cancel of current operation

no rows selected

SQL> set timing on
SQL> r              
  1* select xid,logon_user,start_scn,operation,table_name,row_id,undo_sql from flashback_transaction_query where xid='0B001600DF000000'

XID              LOGON_USER                      START_SCN OPERATION
---------------- ------------------------------ ---------- --------------------------------
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROW_ID
-------------------
UNDO_SQL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0B001600DF000000 SYS                              11211144 UPDATE
T1
AAASCgAAFAAAACHAAF
update "SYS"."T1" set "ID" = NULL where ROWID = 'AAASCgAAFAAAACHAAF';

XID              LOGON_USER                      START_SCN OPERATION
---------------- ------------------------------ ---------- --------------------------------
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROW_ID
-------------------
UNDO_SQL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0B001600DF000000 SYS                              11211144 BEGIN

Elapsed: 00:03:40.77
SQL>

可以看到sql了。

那么之前的事务是不是也能自动再重新记录sql呢?答案是否定的如下:

SQL> select xid,logon_user,start_scn,operation,table_name,row_id,undo_sql from flashback_transaction_query where xid=hextoraw('13001300DB000000');
 
XID              LOGON_USER                      START_SCN OPERATION                        TABLE_NAME                                                                       ROW_ID              UNDO_SQL
---------------- ------------------------------ ---------- -------------------------------- -------------------------------------------------------------------------------- ------------------- --------------------------------------------------------------------------------
13001300DB000000 RHYS                                    0 UNKNOWN                          T1                                                                                                  
13001300DB000000 RHYS                                    0 BEGIN                                                                                                                                
 
Executed in 0.063 seconds
 
SQL>

另外一个问题就来了,这个查询过程也忒慢了吧。因为xid为raw类型。这时候为了提高查询速度使用hextoraw进行转换,使用内部索引来提高查询速度。

eg:

SQL> set autotrace on                                                                                                        
SQL> select xid,logon_user,start_scn,operation,table_name,row_id,undo_sql from flashback_transaction_query where xid=hextoraw('0B001600DF000000');

XID              LOGON_USER                      START_SCN OPERATION
---------------- ------------------------------ ---------- --------------------------------
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROW_ID
-------------------
UNDO_SQL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0B001600DF000000 SYS                              11211144 UPDATE
T1
AAASCgAAFAAAACHAAF
update "SYS"."T1" set "ID" = NULL where ROWID = 'AAASCgAAFAAAACHAAF';

XID              LOGON_USER                      START_SCN OPERATION
---------------- ------------------------------ ---------- --------------------------------
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROW_ID
-------------------
UNDO_SQL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0B001600DF000000 SYS                              11211144 BEGIN

Elapsed: 00:00:00.03

Execution Plan
----------------------------------------------------------
Plan hash value: 1747778896

---------------------------------------------------------------------------------------------
| Id  | Operation               | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |                   |     1 |  2197 |     0   (0)| 00:00:01 |
|*  1 |  FIXED TABLE FIXED INDEX| X$KTUQQRY (ind:1) |     1 |  2197 |     0   (0)| 00:00:01 |
---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

1 - filter("XID"=HEXTORAW('0B001600DF000000') )

Statistics
----------------------------------------------------------
         36  recursive calls
          1  db block gets
         51  consistent gets
          0  physical reads
          0  redo size
       1139  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          3  sorts (memory)
          0  sorts (disk)
          2  rows processed

SQL>

对比一下:

SQL> select xid,logon_user,start_scn,operation,table_name,row_id,undo_sql from flashback_transaction_query where xid='0B001600DF000000';

XID              LOGON_USER                      START_SCN OPERATION
---------------- ------------------------------ ---------- --------------------------------
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROW_ID
-------------------
UNDO_SQL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0B001600DF000000 SYS                              11211144 UPDATE
T1
AAASCgAAFAAAACHAAF
update "SYS"."T1" set "ID" = NULL where ROWID = 'AAASCgAAFAAAACHAAF';

XID              LOGON_USER                      START_SCN OPERATION
---------------- ------------------------------ ---------- --------------------------------
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ROW_ID
-------------------
UNDO_SQL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0B001600DF000000 SYS                              11211144 BEGIN

Elapsed: 00:03:51.14

Execution Plan
----------------------------------------------------------
Plan hash value: 1115820779

------------------------------------------------------------------------------
| Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |           |     1 |  2197 |     0   (0)| 00:00:01 |
|*  1 |  FIXED TABLE FULL| X$KTUQQRY |     1 |  2197 |     0   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

1 - filter(RAWTOHEX("XID")='0B001600DF000000')

Statistics
----------------------------------------------------------
    2936981  recursive calls
         10  db block gets
    3772792  consistent gets
      14256  physical reads
          0  redo size
       1139  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
     731820  sorts (memory)
          0  sorts (disk)
          2  rows processed

SQL>

这就是闪回查询,闪回版本查询,闪回事务查询。

oracle 之flashback 深入研究。的更多相关文章

  1. oracle 利用flashback将备库激活为read wirte(10g 及上)

    oracle 利用flashback将备库激活为read wirte(10g 及上) 环境: OS: CENTOS 6.5 X64 DB: ORACLE 10.2.0.5 主库操作: SQL> ...

  2. Oracle 12C In-Memory特性研究

    Oracle 12C In-Memory特性研究一.Oracle In-Memory1.1 In-Memory 开启方法1.2 开启与关闭IM column store1.3 inmemory优先级调 ...

  3. oracle的购买价格研究

    如果你是一个架构师,在数据库选型上除了技术选型,更重要的可能是性价比的选择,而oracle是现今排名第一的数据库,因此对oracle的价格有所了解是必须的. 几个要点: 1.oracle授权(也就是购 ...

  4. oracle使用flashback时,没有显示undosql

    这是因为oracle11g没有开启这个功能 用管理员用户sys(也就是sysdba)执行以下语句即可 alter databases add supplemental log data; 如果我们想恢 ...

  5. oracle数据库flashback系列--闪回数据库在dataguard中的使用

    很多人在学习flashback database这个oracle技术的时候,都会有一个疑问,就是如果我只有一个数据库作为生产库的话,是否有这样的业务需求导致我们要把数据库闪回到以前的时间点?以及这样做 ...

  6. Oracle安全之 Oracle 11g flashback技术详解

    Oracle11g提供的闪回技术用于对抗人为错误,主要有以下7种技术组成: 闪回查询-(闪回时间查询.闪回版本查询): 闪回数据归档: 闪回事务查询: 闪回事务: 闪回表: 闪回删表: 闪回数据库. ...

  7. Oracle Spatial GIS相关研究

    1.Oracle Spatial 概念相关 Oracle Spatial 是Oracle 数据库强大的核心特性,包含了用于存储矢量数据类型.栅格数据类型和持续拓扑数据类型的原生数据类型.Oracle ...

  8. 对oracle数字类型的研究

    Oracle的数字类型主要有number,binary_float,binary_double三类,其他的像int,number(p,s)等等大多数都是由这些引申出来的.这部分的理解主要来自oracl ...

  9. Oracle:sequence问题研究

    一直以来,以为sequence是不间断地持续增长的:但今天发现sequence是会跳号,这种情况发生在RAC环境下.在单实例环境下,应该不存在的. sequence截图如下: 数据库表中发生了跳号: ...

随机推荐

  1. SGU 403 Scientific Problem

    403. Scientific Problem Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: stan ...

  2. 移动端适配之REM

    随着手机等移动设备的普及,移动端带来的流量已经不可忽视,一个网站不只是只有pc的页面就足够了,移动端的适配已经势在必行.但是移动设备种类繁多,屏幕尺寸也千奇百怪,能不能找到一种方式可以适配所有的手机屏 ...

  3. .NET面试宝典-高级2

    http://blog.csdn.net/shanyongxu/article/category/6023593 对于 Web 性能优化,您有哪些了解和经验吗? 1.前端优化 (1)减少 HTTP 请 ...

  4. jquery 图片预加载

    /图片无序预加载 (function($){ function Preload(imgs,fns){ this.imgs=(typeof imgs==="string")?[img ...

  5. 邮件基本常识普及(to/cc/bcc)

    http://blog.sina.com.cn/s/blog_5572b4b5010009ul.html 前两天,某个同事发的一封邮件着实把我给郁闷了,他发的是图片形式的笑话,内容稍稍有点不太健康,这 ...

  6. SmartProg2 Universal, ISP capable programmer

    http://www.elnec.com/products/universal-programmers/smartprog2/ 40 powerful TTL pindrivers provide H ...

  7. maven打包出错: Failed to clean project: Failed to delete

    maven打包出错: Failed to clean project: Failed to delete 出现这种错误,通常是由于您已启动了另一个tomcat 进程,导致报错,关闭tomcat进程即可 ...

  8. Android之NDK开发环境r9

    需要的软件: android-ndk-r9-windows-x86_64.zip(我的系统是64位的,所以下载的是64的)下载地址:http://developer.android.com/tools ...

  9. 如何查看openshift Router的metrics

    先找到用户名和密码 [root@master ~]# oc describe pod router--8pjrb | grep STATS_ STATS_PASSWORD: mFj58DpaOQ ST ...

  10. Windows 7目录

    1. 用wubi安装的Ubuntu在重装Windows 7系统后,如何恢复(转) 2. Windows 7系统垃圾清理自写程序