Chinaunix首页 | 论坛 | 博客
  • 博客访问: 649079
  • 博文数量: 109
  • 博客积分: 6081
  • 博客等级: 准将
  • 技术积分: 1318
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:28
文章分类
文章存档

2011年(8)

2010年(39)

2009年(62)

分类: Oracle

2009-12-20 21:39:21

oracle flashback 闪回技术实例

相关操作

1、DBA必须设定undo保持力足够大以能够重构需要闪回的数据

ALTER SYSTEM SET UNDO_RETENTION=; seconds值委undo数据保持的秒数。

Flashback view是由undo retention interval来限制的。

2、包DBMS_FLASHBACK提供了需求接口
call dbms_flashback.enable_at_time(‘9-nov-01:11:00:00’);
call dbms_flashback.disable();
-------------------------------------
enable_at_time:会话级的enable flashback,映像时间被设定为最接近指定时间戳的scn
enable_at_system_change_number:将数据库闪回到指定的scn号。
get_system_change_number:返回当前的scn。
disable:这个存储过程允许我们在整个会话内停止flashback并将你带回当前时间的数据状态。

----------
dbms_flashback.enable存储过程不可以在有活动事务的时候执行,并且,这个包不能用sys身份执行。
在使用DBMS_FLASHBACK.ENABLE_AT_TIME前,你必须设定你的NLS_DATE_FORMAT的精确程度,Oracle默认的是精确到天

3、timestamp 与scn(系统改变号) 的对应关系
事实上,Oracle在内部都是使用scn,即使你指定的是as of timestamp,oracle也会将其转换成scn,系统时间标记与scn之间存在一张表,即SYS下的SMON_SCN_TIME

SQL> desc sys.smon_scn_time;

THREAD NUMBER

TIME_MP NUMBER

TIME_DP DATE

SCN_WRP NUMBER

SCN_BAS NUMBER

NUM_MAPPINGS NUMBER

TIM_SCN_MAP RAW(1200)

SCN NUMBER

ORIG_THREAD NUMBER
SQL>alter session set nls_date_format='dd-mm-yy:hh24:mi:ss';

SQL>select scn,time_dp from smon_scn_time;

       SCN TIME_DP
---------- -----------------
   1596247 02-02-10:10:46:22
   1596397 02-02-10:10:50:07
   1596749 02-02-10:10:56:03
   1596969 02-02-10:11:00:07
   1597235 02-02-10:11:05:53
   1598045 02-02-10:11:10:01
   1598314 02-02-10:11:15:58
   1598541 02-02-10:11:19:59
   1598810 02-02-10:11:25:50
   1599296 02-02-10:11:29:55
每隔5分钟,系统产生一次系统时间标记与scn的匹配并存入sys.smon_scn_time表,该表中记录了最近1440个系统时间标记与scn的匹配记录,由于该表只维护了最近的1440条记录,因此如果使用as of timestamp的方式则只能flashback最近5天内的数据(假设系统是在持续不断运行并无中断或关机重启之类操作的话)。注意理解系统时间标记与scn的每5分钟匹配一次这句话,举个例子,比如scn:339988,339989分别匹配08-05-30 13:52:00和2008-13:57:00,则当你通过as of timestamp查询08-05-30 13:52:00或08-05-30 13:56:59这段时间点内的时间时,oracle都会将其匹配为scn:339988到undo表空间中查找,也就说在这个时间内,
不管你指定的时间点是什么,查询返回的都将是08-05-30 13:52:00这个时刻的数据。

当然,具体的情况,我想你亲自执行一下
SQL> select scn,to_char(time_dp,'yyyy-mm-dd hh24:mi:ss') from sys.smon_scn_time;  会理解的更深刻一些。


4、实例:
-------------------------------------------------

$sqlplus / as sysdba
SQL> set time on   
15:16:22 SQL>
16:21:31 SQL> variable scn number
16:21:47 SQL> exec :scn :=dbms_flashback.get_system_change_number

PL/SQL procedure successfully completed.

16:21:53 SQL> print scn

       SCN
----------
   1170495

16:29:10 SQL> print :scn

       SCN
----------
   1170495

可以通过一下方法获取SCN 号
SQL> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
                 1170495

SQL> select * from test.test;

        ID NAME
---------- ------------
         5 wgy
         1 laigq


16:00:54 SQL> delete from test.test;

2 rows deleted.

16:01:37 SQL> commit;

Commit complete.


16:02:40 SQL> select * from test.test;

no rows selected



1、通过 scn 来恢复
SQL> select * from test.test as of scn:scn;

        ID NAME
---------- ------------
         5 wgy
         1 laigq

SQL> select * from test.test as of scn 1170495;

        ID NAME
---------- ------------
         5 wgy
         1 laigq


2、用时间戳来查询
$ date
Thu Dec 17 16:17:55 CST 2009

SQL> select * from test.test as of timestamp to_date('2009-12-17 16:00','yyyy-mm-dd hh24:mi');

        ID NAME
---------- ------------
         5 wgy
         1 laigq


3、还原数据:

SQL> insert into test.test select * from test.test as of timestamp to_date('2009-12-17 16:00','yyyy-mm-dd hh24:mi');

2 rows created.

SQL> commit;

另外一个方法 ---------

这个test(非sys用户)用户自己也可以恢复自己删除的文件了--- 呵呵呵
SQL>flashback table test to timestamp  to_date('2009-12-17 16:00','yyyy-mm-dd hh24:mi');
ORA-08189: cannot flashback the table because row movement is not enabled

SQL>alter table test enable row movement;
Table altered.

SQL>flashback table test to timestamp  to_date('2009-12-17 16:00','yyyy-mm-dd hh24:mi');
Flashback complete.


或者
SQL>flashback table test to scn 1170495;

SQL> commit;
Commit complete.

对于drop table test 的操作flashback

SQL> drop table test;

SQL>COMMIT;

SQL> flashback table test to before drop;
SQL>commit;
4、验证还原成功


SQL> select * from test;

        ID NAME
---------- ------------
         5 wgy
         1 laigq

阅读(1384) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~