Oracle 闪回查询

闪回查询

查看当前数据库中的3个参数,其中UNDO_MANAGEMENT:指定回滚段的管理方式,如果设置为AUTO。则采用撤销表空间自动管理回滚信息
UNDO_TABLESPACE:指定用于回滚信息自动管理的撤销表空间名
UNDO_RETENTION:指定回滚信息的最长保留时间

//查询参数

SQL> show parameter undo;

NAME                                 TYPE        VALUE
------------------------------------ ----------- -----------------------------
undo_management                      string      AUTO
undo_retention                       integer     900
undo_tablespace                      string      UNDOTBS1
可以通过使用ALTER SYSTEM命令改变各个参数的数值例如:
SQL> alter system set undo_retention = 1200;

系统已更改。
//查看修改后的各个参数的数值
SQL> show parameter undo;

NAME                                 TYPE        VALUE
------------------------------------ ----------- -----------------------------
undo_management                      string      AUTO
undo_retention                       integer     1200
undo_tablespace                      string      UNDOTBS1


1.基于as of timestamp的闪回查询及恢复操作示例。

SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';

会话已更改。

SQL> set time on;
14:23:12 SQL> select empno,sal from scott.emp where empno = 7844;

     EMPNO        SAL
---------- ----------
      7844       1500

14:25:24 SQL> update scott.emp set sal = 2000 where empno = 7844;

已更新 1 行。

14:26:04 SQL> commit;

提交完成。


14:26:38 SQL> update scott.emp set sal = 2500 where empno = 7844;

已更新 1 行。


14:26:56 SQL> update scott.emp set sal = 3000 where empno = 7844;

已更新 1 行。


14:27:41 SQL> commit;

提交完成。

14:27:45 SQL> update scott.emp set sal = 3500 where empno = 7844;

已更新 1 行。

14:28:16 SQL> commit;

提交完成。
(1)查询7844号员工的当前工资值。

14:28:20 SQL> select empno, sal from scott.emp where empno = 7844;

     EMPNO        SAL
---------- ----------
      7844       3500
(2)查看7844号员工前一个小时的工资值
14:29:07 SQL> select empno, sal from scott.emp as of timestamp sysdate-1/24 wh
e empno = 7844;

     EMPNO        SAL
---------- ----------
      7844       1500
(3)查看第一个事务提交,第二个事务还没有提交时7844号员工的工资、
14:30:48 SQL> select empno, sal from scott.emp as of timestamp to_timestamp ('
13-5-15 14:27:41','YYYY-MM-DD HH24:MI:SS') where empno = 7844;

     EMPNO        SAL
---------- ----------
      7844       2000
(4)查看第二个事务提交,第三个事务还没有提交时7844号员工的工资、
14:34:29 SQL> select empno, sal from scott.emp as of timestamp to_timestamp ('
13-5-15 14:28:16','YYYY-MM-DD HH24:MI:SS') where empno = 7844;

     EMPNO        SAL
---------- ----------
      7844       3000



(5)如果需要还可以将数据恢复到过去的某个时刻,下面是恢复到2013-05-15 14:28:16时刻的状态
14:39:14 SQL> update scott.emp set sal = ( select sal from scott.emp as of tim
tamp to_timestamp('2013-05-15 14:28:16','YYYY-MM-DD HH24:MI:SS') where empno =
7844) where empno = 7844;

已更新 1 行。

14:40:16 SQL> commit;

提交完成。

14:40:25 SQL> select empno, sal from scott.emp where empno = 7844;

     EMPNO        SAL
---------- ----------
      7844       3000
2.基于as of scn 的闪回查询
14:40:57 SQL> select current_scn from V$database;

CURRENT_SCN
-----------
     649161

14:44:11 SQL> select empno, sal from scott.emp where empno = 7844;

     EMPNO        SAL
---------- ----------
      7844       3000

15:00:07 SQL> update scott.emp set sal = 5000 where empno = 7844;

已更新 1 行。

15:00:40 SQL> commit;

提交完成。

15:00:48 SQL> update scott.emp set sal = 5500 where empno = 7844;

已更新 1 行。

15:01:18 SQL> commit;

提交完成。

15:01:22 SQL> select current_scn from V$database;

CURRENT_SCN
-----------
     649964

//查询scn = 649161时的7844号员工工资 
15:02:24 SQL> select empno, sal from scott.emp as of scn 649161 where empno
44;

     EMPNO        SAL
---------- ----------
      7844       3000
原文地址:https://www.cnblogs.com/lidaojian/p/3079885.html