Chinaunix首页 | 论坛 | 博客
  • 博客访问: 151250
  • 博文数量: 30
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 135
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-31 21:58
文章分类

全部博文(30)

文章存档

2015年(1)

2014年(18)

2013年(11)

我的朋友

分类: Oracle

2013-08-01 10:12:36

1、闪回数据查询:数据来源于UNDO表空间,数据库能保留多少撤销数据决定了闪回查询的时间窗口大小,撤销数据的保留策略取决于4个因素
闪回时间点查询:select * from emp as of timestamp[scn] to_timestamp('','') ;
             另有开启闪回模式:exec dbms_flashback.enbale_at_time()[enable_at_scn]
                关闭闪回模式: exec dbms_flashback.disable
闪回版本查询: select versions_xid,versions_startscn,versions_endscn,versions_starttime,versions_endtime from emp versions between
                timestamp(systimestamp - interval '15' minute)

2、闪回数据:利用UNDO表空间的撤销数据
     alter table emp row movement
     flashback table emp to timestamp(systimestamp - interval '10' minute) | to scn 1080318
     执行flashback table命令必须有flashback  any table的权限 或者对该对表的flashback 权限

3、闪回数据归档:将原来只保存在UNDO表空间的撤销数据额外的以一种历史表的形式保存在指定的普通表空间(permanent类型的表空间)中,且可以只为特定的表服务
    grant flahsback archive administer to hr; ----执行以下语句需要该权限
    create flashback archive [default] fda1 tablespace users retention 1 year;
    grant flashback archive on fda1 to scott;  ----执行下面语句需要该权限
    alter table emp flashback archive fda1; ---如果上面创建的是默认的闪回数据归档,这里就不用指定闪回数据归档的名称,即fda1
    或者在创建表时就指定: create table t (id number) flashback archive fda1;
    关闭表上的数据归档功能:alter table t no flashback archive;
    查询闪回数据归档:select * from t as of timestamp (systemstamp -  interval '7' month);
    清除归档:alter flashback archive fda purage before timestamp timestamp (systimestamp - interval '1' month);
         或清除全部:alter flashback archive fda purage all
4、闪回事务查询 (DML):
   区别于闪回查询:1)不但需要撤销数据,还需要事先启用最小补充日志;2)返回的是SQL语句;3)通过表flashback_transaction_query查询
    alter database add supplemental log data;
     select versions_xid,versions_starttime,versions_endtime,versions_operation,[versions_startscn,versions_endscn] from emp versions between
           timestamp minvalue and maxvalue;              -----查出XID,即事务号
      **** versions between timestamp minvalue and maxvalue 显示出所有值,因为versions between timestamp后必须有范围,就用minvalue and maxvalue 代替
           这里有多种组合,minvalue and maxvalue 、SCN、to_date这三种组合
     select undo_sql from flashback_transaction_query where xid='1234567890';
    下面是一个PL/SQL匿名块来执行UNDO_SQL:
      begin
        for rec in (select undo_sql from flashback_transaction_query where xid='1234567890') loop
          if rec.undo_sql is not null then
             execute immediate substr (rec.undo_sql,1,length(rec.undo_sql)-1);
          end if;
        end loop;
       commit;
       end;
        /
5、闪回事务:自动分析重做日志,挖掘出变更前的值用以构建撤销SQL,然后执行撤销。要启用主键补充日志和外键补充日志
      事务依赖性:WAW依赖、主键依赖、外键依赖三种。
            WAW依赖即Write After Write,事务1先修改了一行,事务紧接又修改了同一行。
            主键依赖即在事务1在一张拥有主键的表上删除一行,事务2又插入了具有相同主键值的另一行,即两次事务的主皱键值相同
            外键依赖即事务1修改(insert or update)了而产生了新的可被外键参考的字段值,之后事务2修改(insert or update)外键字段时利用了事务1所产生的字段值
       nocascade, 事务1不可被任何其它事务依赖,否则撤销操作报错
       cascade,将事务1同事务2一起撤销。
       nocascade_force,忽略事务2,直接执行事务1的撤销SQL,如果没有约束冲突,操作成功,否则约束报错导致撤销操作失败
       nonconfilict_only,在不影响事务2的前提下,撤销事务1的修改。与nocascade_force的不同在于首先过滤一下事务1的撤销SQL,确保他们不会作用在事务2的修改的行上
       eg: select distinct xid,commit_scn,undo_sql from flashback_transaction_query  where table_owner='SCOTT' and table_name='EMP'
                         and commit_timestamp > systimestamp - interval '15' minute order by commit_scn;
           declare
             xids sys.xid_array;
               begin
                  xids :=sys.xid_array('0A00160094020000');
                  dbms_flashback.transaction_backout(1,xids,options=>dbms_flashback_nocascade);
               end;
             /
       ********transaction_backout的第一参数是VARRAY内事务号的数量,第二个参数是容纳事务事号的VARRAY集合变量

注意四个权限:alter database add supplemental log data;
             alter database add supplemental log data (primary key) columns;
             grant execute on dbms_flashback to hr;
             grant select any transaction to hr;
             如果hr用户需要对其它用户下的对象的事务进行闪回,还必须具有对该用户下的所有表具有DML操作的权限

6、闪回删表(Flashback drop):当DROP时,表及索引并没有真正删除,其所占空间(称为段)只是分配给了另一种对象:回收站,并且这种分配使数据和数据块没有发生任何移动,还是待在原来的数据文件及表空间中。
     flashback table emp to before drop [rename to empold]
     flashback table "BIN$pseZ0Q66QY60DQn8jHaAxA==$0" to before drop [rename to  empold];

7、闪回数据库:利用闪回日志、重做日志。
   闪回数据库方式有两种:如当前时间点为TC,数据库要闪回到T1(TC>T1)。
   flashback database to    ----PIT可以是SCN、时间或还原点
   一、先用闪回日志回退到T2(T2    二、确定一个比T1更旧的某时刻T2,对于闪回日志无法恢复的对象或数据(比如删除了100W条记录,闪回日志不可能记录这些数据),数据库回退到创建些 对象的时间点T3(T3 -----------------------------------------------------------------------------------------------
   select oldest_flashback_scn,to_char(oldest_flashback_scn,'YYYY-MM-DD HH24:MI:SS') from v$flashback_database_log;
          -----查看最远的闪回点
   startup force mount
   flashback database to scn 1234567            ----在mount下执行
   alter database open read only;              ----查看数据是否闪回到想要的那个时间点
   .......                -----反复重复执行直到闪回到对应的时间点
   
   ******闪回日志的路径必须是快速恢复区的子目录,保存的期限则由参数db_flashback_retention_target控制
         必须开启闪回日志功能,
         闪回查询、闪回事务查询、闪回表均依赖ROWID定位“过去”和“现在”的行,如果执行过导致行移动的命令,如alter table ... move、alter table ... shrink space;
         dbms_flashback.transaction_backout时所需日志不存在,将报告类型ORA-00308:cannot open archived log;若撤销数据已不存在,将报告ORA-01555:snapshot too old错误
         由于默认情况下,闪回日志没有启用,闪回数据库功能没有关闭的;由于最小补充日志没有启用,闪回事务查询功能是关闭的;由于主键补充日志没有启用,闪回事务功能是关闭的,其它闪回功能默认可使用的
阅读(1773) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~