Flashback Query 是利用多版本读一致性的特性从UNDO 表空间读取操作前的记录数据!
由于例子完整性,演示As of timestamp和As of scn 的示例
个人推荐就使用as of scn,精准性高
1,As of timestamp的例子
******************************个人操作**************************************
SQL> alter session set nls_date_format='YYYY-MM-DD hh24:mi:ss';
会话已更改。
SQL> select sysdate from dual;
SYSDATE
-------------------
2011-11-25 10:24:18
SQL> delete from emp;
已删除14行。
SQL> select * from emp;
未选定行
SQL> select count(*) from emp as of timestamp sysdate-5/1440;(通过as of timestamp查找五分钟之前的数据)
COUNT(*)
----------
14
SQL> select count(*) from emp as of timestamp to_timestamp('2011-11-25 10:24:18'
,'YYYY-MM-DD hh24:mi:ss');(按系统时间查找)
COUNT(*)
----------
14
SQL> insert into emp select * from emp as of timestamp sysdate-5/1440;(插入以前时间点的数据)
已创建14行。
SQL> select count(*) from emp;
COUNT(*)
----------
14
恢复操作成功!!!!
2,As of scn的例子
********************************个人操作**************************************
SQL> SELECT dbms_flashback.get_system_change_number FROM dual;
SELECT dbms_flashback.get_system_change_number FROM dual
*
第 1 行出现错误:
ORA-00904: : 标识符无效
(它说我有ora-00904的错误,原因是我现在还在scott用户下,所以要转回sys用户下)
SQL> connect sys/root as sysdba;
已连接。
SQL> SELECT dbms_flashback.get_system_change_number FROM dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
3361877
SQL> SELECT CURRENT_SCN FROM V$DATABASE;(查看scn)
CURRENT_SCN
-----------
3361949
SQL> delete from scott.emp;
已删除14行。
SQL> commit;
提交完成。
SQL> select count(*) from scott.emp as of scn 3361949;(通过scn查找历史数据)
COUNT(*)
----------
14
SQL> insert into scott.emp select * from scott.emp as of scn 3361949;(插入历史数据)
已创建14行。
SQL> select count(*) from scott.emp;
COUNT(*)
----------
14
恢复操作成功
flash_query总结:
查看SCN 和 timestamp 之间的对应关系(如下),
SQL> select * from(select rownum no,scn,to_char(time_dp,'yyyy-mm-dd hh24:mi:ss')
from sys.smon_scn_time)
2 where no>=1 and no<=10;
NO SCN TO_CHAR(TIME_DP,'YY
---------- ---------- -------------------
1 3289317 2011-11-23 16:42:00
2 3289725 2011-11-23 16:52:00
3 3290621 2011-11-23 17:02:01
4 3214377 2011-11-22 14:16:15
5 3291105 2011-11-23 17:12:01
6 3179460 2011-11-19 16:29:43
7 2019804 2011-10-21 14:40:51
8 2020849 2011-10-21 15:00:36
9 3202079 2011-11-22 09:43:36
10 3216623 2011-11-22 15:26:16
已选择10行。
说明scn和时间有一定的对应关系,对应规则是:每隔5 分钟,系统产生一次系统时间标记与scn 的匹配
5分钟的概念就是,在这个时间段点,就有一个scn对应
举个例子,比如scn:339988,339989 分别匹配11-11-11 13:52:00 和11-11-11 13:57:00,在这个时间段里面查
找的数据都是11-11-11 13:52:00 时间点的数据。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26446098/viewspace-712049/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26446098/viewspace-712049/