Oracle11g新特性:Flashback Data Archive

来源:互联网 发布:java精彩编程200例 编辑:程序博客网 时间:2024/05/16 10:36

Oracle9i引入flashback query,使得数据库第一次可以查询到之前的数据,而不再需要利用log和备份进行时间点恢复。Oracle10g更是引入flashback version query,flashback transaction query,flashback database,flashback table,flashback drop等特性,并且大大简化了flashback query的使用。Oracle11g则为flashback家族又带来一个新的成员:flashback data archive

初看起来,flashback data archive和flashback query没有太大的不同,都是通过as of能够查询之前的数据,但是他们的实现机制是不一样的。Flashback query是通过直接从undo中读取信息来构造旧数据,这样就有一个限制,就是undo中的信息不能被覆盖。而undo段是循环使用的,只要事务提交,之前的undo信息就可能被覆盖,虽然可以通过undo_retention等参数来延长undo的存活期,但这个参数会影响所有的事务,设置过大,可能导致undo tablespace快速膨胀。

Falshback data archive特性则通过将变化数据另外存储到创建的flashback archive中,以和undo区别开来,这样就可以通过为flashback archive单独设置存活策略,使得可以闪回到指定时间之前的旧数据而不影响undo策略。并且可以根据需要指定哪些数据库对象需要保存历史变化数据,而不是将数据库中所有对象的变化数据都保存下来,这样可以极大的减少空间需求。

flashback_data_archive

1.授予用户创建flashback archive的权限

SYS@11g>grant flashback archive administer to ning;

Grant succeeded.

SYS@11g>conn ning/ning@11g
Connected.

2.创建flashback archive

NING@11g>create flashback archive test_archive1
2 tablespace users
3 quota 10m
4 retention 1 day;
tablespace users
*
ERROR at line 2:
ORA-55603: Invalid Flashback Archive command

这里比较奇怪,语法和官方文档以及一些例子上的一样,但是总是报ora-55603错误,除非将tablespace的名字用双引号引起来:

NING@11g>create flashback archive test_archive1
2 tablespace “USERS”
3 quota 10m
4 retention 1 day;

Operation 218 succeeded.

这样我们就创建了一个闪回归档区(flashback archive),数据存放在users表空间中,上面创建的归档区大小不能超过10m,如果达到10m的限制,放到该归档区的table将不能执行DML操作按照Oracle文档的说法,DML操作会失败,但是Piner在实验中发现实际上不会影响DML操作,归档区满了以后会自动覆盖。从原理和需求来说,这两种方式应该都是可以接受的,这样子Oracle应该通过设置参数来让用户来选择以何种方式对待归档区空间不足的情况,等有时间再多做几个实验看看。历史数据保留时间为1天,单位可以是day/month/year。根据不同的需求,我们可以创建不同策略的闪回归档区。并且可以创建一个默认的闪回归档区,这样在为table指定flashback archive的时候没有指定名字的话,就会放置到默认的闪回归档区。


NING@11g>create flashback archive default test_archive2
2 tablespace “USERS”
3 quota 10m
4 retention 1 month;
tablespace “USERS”
*
ERROR at line 2:
ORA-55611: No privilege to manage default Flashback Archive

拥有flashback archive administer权限只能创建一般的闪回归档区,默认的flashback archive只能sysdba用户才能创建和修改。


NING@11g>conn sys/password@11g as sysdba
Connected.
SYS@11g>create flashback archive default test_archive2
2 tablespace “USERS”
3 quota 10m
4 retention 1 month;

Operation 218 succeeded.

系统中已有默认flashback archive的情况下不能再创建第二个默认flashback archive


SYS@11g>create flashback archive default test_archive3
2 tablespace “USERS”
3 quota 10m
4 retention 1 month;
tablespace “USERS”
*
ERROR at line 2:
ORA-55609: Attempt to create duplicate default Flashback Archive

但是可以可以设置其他已经存在的flashback archive为新的默认flashback archive

SYS@11g>alter flashback archive test_archive1 set default;

Operation 219 succeeded.

3.创建测试表

SYS@11g>conn ning/ning@11g

Connected.

NING@11g>create table test(i int)
2 flashback archive test_archive1;

Table created.

4.插入测试数据


NING@11g>insert into test values(1);

1 row created.

NING@11g>commit;

Commit complete.

NING@11g>select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’) tm from dual;

TM
————————————–
2007-09-01 18:33:12

NING@11g>insert into test values(2);

1 row created.

NING@11g>commit;

Commit complete.

NING@11g>select to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’) tm from dual;

TM
————————————–
2007-09-01 18:34:10

NING@11g>update test set i=3 where i=2;

1 row updated.

NING@11g>commit;

Commit complete.

NING@11g>select * from test;

I
———-
1
3

5.查询过去某个时刻的数据

NING@11g>select * from test
2 as of timestamp
3 to_timestamp(’2007-09-01 18:34:10′,’yyyy-mm-ff hh24:mi:ss’);

I
———-
1
2

NING@11g>select * from test
2 as of timestamp
3 to_timestamp(’2007-09-01 18:33:12′,’yyyy-mm-ff hh24:mi:ss’);

I
———-
1

当然,我们不可能查到启用flashback archive之前的数据。比如10分钟前,我们还没有开始该实验:


NING@11g>select * from test
2 as of timestamp
3 (systimestamp – interval ’10′minute);
select * from test
*
ERROR at line 1:
ORA-01466: unable to read data – table definition has changed

发生ora-01466错误,尝试缩小时间间隔

NING@11g>select * from test
2 as of timestamp
3 (systimestamp – interval ’5′ minute);

I
———-
2
1

6.Flashback archive相关数据字典

  • DBA_FLASHBACK_ARCHIVE
  • DBA_FLASHBACK_ARCHIVE_TS
  • DBA_FLASHBACK_ARCHIVE_TABLES
  • USER_FLASHBACK_ARCHIVE
  • USER_FLASHBACK_ARCHIVE_TS
  • USER_FLASHBACK_ARCHIVE_TABLES

本文网址:http://www.ningoo.net/html/2007/oracle_11g_new_feature_flashback_data_archive.html