Home  >  Article  >  Database  >  oracle闪回版本和闪回事务查询详解

oracle闪回版本和闪回事务查询详解

WBOY
WBOYOriginal
2016-06-07 16:20:591197browse

--- 说明闪回数据库 --- 使用闪回表将表内容还原到过去的特定时间点 --- 从删除表中进行恢复 --- 使用闪回查询查看截止到任一时间点的数据库内容 --- 使用闪回版本查询查看某一行在一段时间内的各个版本 --- 使用闪回事务查询查看事务处理历史记录或行 优点

   --- 说明闪回数据库

  --- 使用闪回表将表内容还原到过去的特定时间点

  --- 从删除表中进行恢复

  --- 使用闪回查询查看截止到任一时间点的数据库内容

  --- 使用闪回版本查询查看某一行在一段时间内的各个版本

  --- 使用闪回事务查询查看事务处理历史记录或行

  优点:

  闪回技术由于只能处理更改数据,所以从根本上改变了恢复技术。使用这个技术时,从错误中恢复花费的时间等于制造错误所花费的时间。当闪回技术使用时,它与介质恢复相比,在易用性、可用性和还原时间方面有明显的优势。

  闪回数据库使用闪回日志执行闪回。闪回删除使用回收站。其他所有功能都使用还原数据。

oracle闪回版本和闪回事务查询详解    三联

  闪回时间浏览

  闪回技术提供的功能可用于查询方案对象的过去版本、查询历史记录数据以及执行更改分析。每个事务处理在逻辑上都会生成新版本数据库。使用闪回技术,可通过浏览这些版本来查找错误以及原因。

  · 闪回查询:查询特定时间点的所有数据。

  · 闪回版本查询:查看两个时间之间行的所有版本已经更改了行的事务处理。

  · 闪回事务处理查询:查看事务处理做的所有更改。

  使用闪回查询功能时,可以对自特定时间起的数据库执行查询。通过使用select语句的 as of 子句,可指定要查看其数据的时间戳。这有助于分析数据差异。

  实验一:闪回查询

  实验一:闪回查询:as of timestamp

  SYS@ORCL>conn tyger/tyger

  Connected.

  TYGER@ORCL>create table fb_query as select * from scott.dept;

  Table created.

  TYGER@ORCL>select * from fb_query;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  TYGER@ORCL>set time on;

  09:51:36 TYGER@ORCL>delete fb_query where deptno=10;

  1 row deleted.

  09:51:53 TYGER@ORCL>commit;

  Commit complete.

  09:51:57 TYGER@ORCL>select * from fb_query;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  09:52:06 TYGER@ORCL>select * from fb_query as of timestamp sysdate-1/1440;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  实验二:闪回查询应用

  10:25:04 TYGER@ORCL>drop table fb_tyger purge;

  Table dropped.

  10:25:10 TYGER@ORCL>create table fb_tyger as select * from scott.dept;

  Table created.

  10:25:33 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  10:25:44 TYGER@ORCL>select sysdate from dual;

  SYSDATE

  ---------

  14-MAR-14

  10:26:02 TYGER@ORCL>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

  Session altered.

  10:26:30 TYGER@ORCL>select sysdate from dual;

  SYSDATE

  -------------------

  2014-03-14 10:26:38

  10:26:38 TYGER@ORCL>update fb_tyger set dname='';

  4 rows updated.

  10:26:51 TYGER@ORCL>commit;

  Commit complete.

  10:26:54 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 NEW YORK

  20 DALLAS

  30 CHICAGO

  40 BOSTON

  10:27:12 TYGER@ORCL>select * from fb_tyger as of timestamp to_timestamp('2014-03-14 10:26:38','yyyy-mm-dd hh24:mi:ss');

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  此处遇到错误:

  ERROR at line 1:

  ORA-01466: unable to read data - table definition has changed

  参考文档:

  10:29:21 TYGER@ORCL>select * from fb_tyger as of timestamp sysdate-3/1440;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  10:29:35 TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 NEW YORK

  20 DALLAS

  30 CHICAGO

  40 BOSTON

  10:46:22 TYGER@ORCL>set time off

  TYGER@ORCL>update fb_tyger t

  2 set dname =

  3 (select dname from fb_tyger as of timestamp

  4 to_timestamp('2014-03-14 10:26:38','yyyy-mm-dd hh24:mi:ss')

  5 where t.deptno=fb_tyger.deptno);

  4 rows updated.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  实验三:闪回查询 as of scn

  TYGER@ORCL>conn / as sysdba

  Connected.

  SYS@ORCL>grant execute on dbms_flashback to tyger;

  Grant succeeded.

  TYGER@ORCL>select dbms_flashback.get_system_change_number from dual;

  GET_SYSTEM_CHANGE_NUMBER

  ------------------------

  1107246

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  TYGER@ORCL>delete fb_tyger where deptno

  3 rows deleted.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_tyger;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  40 OPERATIONS BOSTON

  TYGER@ORCL>select * from fb_tyger as of scn 1107246;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  实验四:利用PL/SQL包dbms_flashback

  语法:

  · 会话启用闪回指定时间:

  DBMS_FLASHBACK.ENABLE_AT_TIME(query_time IN TIMESTAMP);

  · 会话启用闪回指定SCN:

  DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(query_scn IN NUMBER);

  · 关闭闪回:

  DBMS_FLASHBACK.DISABLE;

  TYGER@ORCL>conn / as sysdba

  Connected.

  SYS@ORCL>grant execute on dbms_flashback to tyger;

  Grant succeeded.

  SYS@ORCL>conn tyger/tyger

  Connected.

  TYGER@ORCL>

  TYGER@ORCL>

  TYGER@ORCL>

  TYGER@ORCL>create table fb_query1 as select * from scott.dept;

  Table created.

  TYGER@ORCL>create table fb_query2 as select * from scott.dept;

  Table created.

  TYGER@ORCL>commit;

  Commit complete.

  TYGER@ORCL>select * from fb_query1;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  TYGER@ORCL>select * from fb_query2;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  TYGER@ORCL>set time on;

  11:03:38 TYGER@ORCL>update fb_query1 set loc='';

  4 rows updated.

  11:03:52 TYGER@ORCL>commit;

  Commit complete.

  11:03:54 TYGER@ORCL>update fb_query2 set dname='';

  4 rows updated.

  11:04:14 TYGER@ORCL>commit;

  Commit complete.

  11:04:15 TYGER@ORCL>

  11:04:15 TYGER@ORCL>select * from fb_query1;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING

  20 RESEARCH

  30 SALES

  40 OPERATIONS

  11:04:23 TYGER@ORCL>select * from fb_query2;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 NEW YORK

  20 DALLAS

  30 CHICAGO

  40 BOSTON

  // 闪回定位到5分钟前,此时若访问sysdate等时间函数,那么返回的是当前值而非5分钟之前。

  11:04:30 TYGER@ORCL>exec dbms_flashback.enable_at_time(sysdate-5/1440);

  PL/SQL procedure successfully completed.

  11:05:09 TYGER@ORCL>select * from fb_query1;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  11:05:29 TYGER@ORCL>select * from fb_query2;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING NEW YORK

  20 RESEARCH DALLAS

  30 SALES CHICAGO

  40 OPERATIONS BOSTON

  //处于闪回会话模式时,不允许执行DML 、 DDL 操作

  11:05:45 TYGER@ORCL>update fb_query1 set dname='';

  update fb_query1 set dname=''

  *

  ERROR at line 1:

  ORA-08182: operation not supported while in Flashback mode

  11:05:59 TYGER@ORCL>exec dbms_flashback.disable;

  PL/SQL procedure successfully completed.

  11:06:18 TYGER@ORCL>select * from fb_query1;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 ACCOUNTING

  20 RESEARCH

  30 SALES

  40 OPERATIONS

  11:06:30 TYGER@ORCL>select * from fb_query2;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10 NEW YORK

  20 DALLAS

  30 CHICAGO

  40 BOSTON

  11:06:37 TYGER@ORCL>update fb_query1 set dname='' where deptno=10;

  1 row updated.

  11:07:10 TYGER@ORCL>select * from fb_query1;

  DEPTNO DNAME LOC

  ---------- -------------- -------------

  10

  20 RESEARCH

  30 SALES

  40 OPERATIONS

  // sys 用户不允许使用dbms_flashback 包

  11:07:20 TYGER@ORCL>conn / as sysdba

  Connected.

  11:07:35 SYS@ORCL>set time off

  SYS@ORCL>exec dbms_flashback.enable_at_time(sysdate-5/1440);

  BEGIN dbms_flashback.enable_at_time(sysdate-5/1440); END;

  *

  ERROR at line 1:

  ORA-08185: Flashback not supported for user SYS

  ORA-06512: at "SYS.DBMS_FLASHBACK", line 3

  ORA-06512: at line 1

  实验二:闪回版本

  ---通过闪回版本可审计表行,检索影响行的事务处理的有关信息。然后可使用返回的事务处理标识符来执行事务处理挖掘(通过使用LogMiner)或执行闪回版本查询。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn