ORACLE 10g FLASHBACK (二)

关于ORACLE FLASHBACK特性
      从9i版本开始,Oracle通过DBMS_FLASHBACK包提供flashback query功能,Oracle 10g版本进一步扩展了flashback功能,利用flashabck我们可以做到flashback query,flashback table,flashback database等操作.

      flashback table与9i的flashback query相似,但是说明的是,flashback table不等于flashback query,所谓query,仅仅是查询以前的一个快照点而已,并不改变当前表的状态,而flashback table不一样,将改变当前表及附属对象一起回到以前的时间点。
  其实9i的flashback query在10g中也有了新的变化,10g中可以简单的利用以下语句实现flashback query,而不再需要象9i那样需要调用DBMS_FLASHBACK包。
  SELECT * FROM TABLENAME AS OF TIMESTAMP
  TO_TIMESTAMP('2010-08-04 09:30:00', 'YYYY-MM-DD HH:MI:SS')
  WHERE ……;

 

提示:如果需要flashback一个表,需要保证
    需要有flashback any table的系统权限或者是该表的flashback对象权限。
    需要有该表的SELECT, INSERT, DELETE, ALTER权限
    必须保证该表ROW MOVEMENT

      sys用户不能使用回收站闪回表工作

 

闪回恢复已经在上一节设置好,下面我们开始做闪回例子
--测试用例环境 DB:10.2.0.4 OS:rhel5.4

 

1.Flashback Query
1).原理:Flashback Query通过Undo数据,利用一致读的方法来查找用户需要的数据.
2).使用范围:查找指定时间点被删除(delete,被update)的数据
3).注意点:1).闪回时间受参数undo_retention影响 2).闪回时间受undo表空间大小影响 3).查询之前的快照点,并不改变当前表的状态
4).语法:1).select ... from table_name as of timestamp ... 2).select ... from table_name as of scn ...
5).举例:
SQL>create table test(id number,scn number,time varchar2(20));

Table created.
SQL>begin
  2  for i in 1..20
  3  loop
  4  insert into test values(i,dbms_flashback.get_system_change_number(),to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')); --插入scn和sysdate为了后面查询方便明了
  5  commit;   --如果不加commit 会造成scn都是一样的
  6  dbms_lock.sleep(3); --估计加大插入间隔
  7  end loop;
  8  end;
  9  /

PL/SQL procedure successfully completed.

SQL>select * from test;

        ID        SCN TIME
---------- ---------- ----------------------------------------
         1    1648243 2010-08-12 11:10:52
         2    1648245 2010-08-12 11:10:55
         3    1648247 2010-08-12 11:10:58
         4    1648249 2010-08-12 11:11:01
         5    1648251 2010-08-12 11:11:04
         6    1648253 2010-08-12 11:11:07
         7    1648255 2010-08-12 11:11:10
         8    1648257 2010-08-12 11:11:13
         9    1648259 2010-08-12 11:11:16
        10    1648261 2010-08-12 11:11:19
        11    1648263 2010-08-12 11:11:22
        12    1648265 2010-08-12 11:11:25
        13    1648267 2010-08-12 11:11:28
        14    1648269 2010-08-12 11:11:31
        15    1648271 2010-08-12 11:11:34
        16    1648274 2010-08-12 11:11:37
        17    1648276 2010-08-12 11:11:40
        18    1648278 2010-08-12 11:11:43
        19    1648280 2010-08-12 11:11:46
        20    1648282 2010-08-12 11:11:49

20 rows selected.

SQL>select * from test as of scn 1648247;  --根据自己实际数据,我选择的是第3条数据

        ID        SCN TIME
---------- ---------- ----------------------------------------
         1    1648243 2010-08-12 11:10:52
         2    1648245 2010-08-12 11:10:55

SQL>select * from test as of timestamp  to_timestamp('2010-08-12 11:10:58','yyyy-mm-dd hh24:mi:ss'); --根据自己实际数据,同样是第3条数据

        ID        SCN TIME
---------- ---------- ----------------------------------------
         1    1648243 2010-08-12 11:10:52
         2    1648245 2010-08-12 11:10:55
         3    1648247 2010-08-12 11:10:58

--从上面例子也可以看出scn和timestamp并不是完全对应的.在实际案例中我们往往忽略这些微小差别.

 

 


2.Flashback Table
1).原理:Flashback Table也是利用Undo数据来恢复对象到指定时间点
2).使用范围:恢复当前表及其相关对象到某个时间点
3).注意点:1).闪回时间受参数undo_retention影响 2).闪回时间受undo表空间大小影响 3).和flashback query有差别,恢复表到以前的时间点 4).flashback之前要先允许row movement
4).语法:1).flashback table table_name to timestamp .... 2).flash table table_name to scn ... 3).flashback table table_name to timestamp ... enable triggers.
5).举例
SQL>select * from test;

        ID        SCN TIME
---------- ---------- ----------------------------------------
         1    1648243 2010-08-12 11:10:52
         2    1648245 2010-08-12 11:10:55
         3    1648247 2010-08-12 11:10:58
         4    1648249 2010-08-12 11:11:01
         5    1648251 2010-08-12 11:11:04
         6    1648253 2010-08-12 11:11:07
         7    1648255 2010-08-12 11:11:10
         8    1648257 2010-08-12 11:11:13
         9    1648259 2010-08-12 11:11:16
        10    1648261 2010-08-12 11:11:19
        11    1648263 2010-08-12 11:11:22
        12    1648265 2010-08-12 11:11:25
        13    1648267 2010-08-12 11:11:28
        14    1648269 2010-08-12 11:11:31
        15    1648271 2010-08-12 11:11:34
        16    1648274 2010-08-12 11:11:37
        17    1648276 2010-08-12 11:11:40
        18    1648278 2010-08-12 11:11:43
        19    1648280 2010-08-12 11:11:46
        20    1648282 2010-08-12 11:11:49

20 rows selected.

SQL>alter table test enable row movement;  --允许数据库移动列

Table altered.

SQL>flashback table test to  timestamp  to_timestamp('2010-08-12 11:11:40','yyyy-mm-dd hh24:mi:ss');  --flashback到某一时间点

Flashback complete.

SQL>select * from test;  --表的数据已经改变

        ID        SCN TIME
---------- ---------- ----------------------------------------
         1    1648243 2010-08-12 11:10:52
         2    1648245 2010-08-12 11:10:55
         3    1648247 2010-08-12 11:10:58
         4    1648249 2010-08-12 11:11:01
         5    1648251 2010-08-12 11:11:04
         6    1648253 2010-08-12 11:11:07
         7    1648255 2010-08-12 11:11:10
         8    1648257 2010-08-12 11:11:13
         9    1648259 2010-08-12 11:11:16
        10    1648261 2010-08-12 11:11:19
        11    1648263 2010-08-12 11:11:22
        12    1648265 2010-08-12 11:11:25
        13    1648267 2010-08-12 11:11:28
        14    1648269 2010-08-12 11:11:31
        15    1648271 2010-08-12 11:11:34
        16    1648274 2010-08-12 11:11:37
        17    1648276 2010-08-12 11:11:40

17 rows selected.

SQL>flashback table test to  scn 1648274;

Flashback complete.

SQL>select * from test;

        ID        SCN TIME
---------- ---------- ----------------------------------------
         1    1648243 2010-08-12 11:10:52
         2    1648245 2010-08-12 11:10:55
         3    1648247 2010-08-12 11:10:58
         4    1648249 2010-08-12 11:11:01
         5    1648251 2010-08-12 11:11:04
         6    1648253 2010-08-12 11:11:07
         7    1648255 2010-08-12 11:11:10
         8    1648257 2010-08-12 11:11:13
         9    1648259 2010-08-12 11:11:16
        10    1648261 2010-08-12 11:11:19
        11    1648263 2010-08-12 11:11:22
        12    1648265 2010-08-12 11:11:25
        13    1648267 2010-08-12 11:11:28
        14    1648269 2010-08-12 11:11:31
        15    1648271 2010-08-12 11:11:34

15 rows selected.

 


3.Flashback Drop(RecycleBin)
1).原理:利用类似于windows的回收站来支持flashback drop(当表被删除时,数据库仅仅修改一下相关对象的名字,flashback过程相当于重新命名回来而已)
2).使用范围:恢复被误删除的表极其相关对象
3).注意点:1).闪回主要看回收站大小 2).truncate对象不能被flashback
4).语法:1).flashback table table_name to before drop 2).flashback table table_name to before drop rename to new_table_name;
5).举例
SQL>drop table test;  --首先drop表
Table dropped.

SQL>select * from test;  --表已经不存在
select * from test
              *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL>flashback table test to before drop;  --通过flashback drop恢复表

Flashback complete.

SQL>select * from test;    --表已经恢复

        ID        SCN TIME
---------- ---------- ----------------------------------------
         1    1648243 2010-08-12 11:10:52
         2    1648245 2010-08-12 11:10:55
         3    1648247 2010-08-12 11:10:58
         4    1648249 2010-08-12 11:11:01
         5    1648251 2010-08-12 11:11:04
         6    1648253 2010-08-12 11:11:07
         7    1648255 2010-08-12 11:11:10
         8    1648257 2010-08-12 11:11:13
         9    1648259 2010-08-12 11:11:16
        10    1648261 2010-08-12 11:11:19
        11    1648263 2010-08-12 11:11:22
        12    1648265 2010-08-12 11:11:25
        13    1648267 2010-08-12 11:11:28
        14    1648269 2010-08-12 11:11:31
        15    1648271 2010-08-12 11:11:34

15 rows selected.


4.Flashback Version Query
1).原理:利用Undo来查看多版本
2).使用范围:查看行改变情况
3).注意点:1).闪回时间受参数undo_retention影响 2).闪回时间受undo表空间大小影响 3).查询行改变情况,并不改变当前表的状态
4).语法:1).select * from table_name versions between timestamp  ... and ... 2).select * from table_name versions between scn ... and ...
5).举例
--我们可能用到的伪劣:versions_starttime,versions_startscn,versions_endscn,versions_endtime,versions_xid,versions_operation这些伪列性质和rownum等是一样的

SQL>delete from test where scn >1648261;

5 rows deleted.

SQL>commit;
SQL>column  VERSIONS_STARTTIME format a25;
SQL> select versions_starttime,versions_operation,id ,scn,time from jane.t versions between timestamp  to_timestamp('
2010-08-12 11:50:00','yyyy-mm-dd hh24:mi:ss') and maxvalue order by versions_starttime,id;

VERSIONS_STARTTIME        VE         ID        SCN TIME
------------------------- -- ---------- ---------- ----------------------------------------
12-AUG-10 11.50.12 AM     D          11    1648263 2010-08-12 11:11:22
12-AUG-10 11.50.12 AM     D          12    1648265 2010-08-12 11:11:25
12-AUG-10 11.50.12 AM     D          13    1648267 2010-08-12 11:11:28
12-AUG-10 11.50.12 AM     D          14    1648269 2010-08-12 11:11:31
12-AUG-10 11.50.12 AM     D          15    1648271 2010-08-12 11:11:34
                                      1    1648243 2010-08-12 11:10:52
                                      2    1648245 2010-08-12 11:10:55
                                      3    1648247 2010-08-12 11:10:58
                                      4    1648249 2010-08-12 11:11:01
                                      5    1648251 2010-08-12 11:11:04
                                      6    1648253 2010-08-12 11:11:07
                                      7    1648255 2010-08-12 11:11:10
                                      8    1648257 2010-08-12 11:11:13
                                      9    1648259 2010-08-12 11:11:16
                                     10    1648261 2010-08-12 11:11:19
                                     11    1648263 2010-08-12 11:11:22
                                     12    1648265 2010-08-12 11:11:25
                                     13    1648267 2010-08-12 11:11:28
                                     14    1648269 2010-08-12 11:11:31
                                     15    1648271 2010-08-12 11:11:34

20 rows selected.
--从上面信息可以看出我们在“12-AUG-09 11.50.12 AM ”时间做了D(删除)操作,删除的数据是id在11和15之间的.

 

 

5.Flashback Database
数据库可以通过flashback log来回到之前的某个时间点.

SQL>flashback database to timestamp to_timestamp('2010-08-12 23:33:34','yyyy-mm-dd hh24:mi:ss');

Flashback complete.

SQL>alter database open read only;

Database altered.

SQL> select count(*) from test;

  COUNT(*)
----------
      20

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area  171966464 bytes
Fixed Size                  2082496 bytes
Variable Size             125831488 bytes
Database Buffers           37748736 bytes
Redo Buffers                6303744 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open

--一旦resetlogs之后,就再也不能flashback database 到resetlogs之前的状态了,所以之前用open read only检查一下是否flashback 成功了。


SQL>alter database open resetlogs;

Database altered.

SQL> select count(*) from test;

  COUNT(*)
----------
      22

//通过v$flash_recovery_area_usage视图可以查到闪回区的使用情况。
//通过v$flashback_database_log视图可以查到flashback database能恢复到最早的时间和SCN号。

你可能感兴趣的:(oracle,数据库,table,database,query,2010)