Flashback Drop

来源:互联网 发布:a股数据购买 编辑:程序博客网 时间:2024/05/29 14:53
 

2.4.3 Flashback Drop

            Flashback Drop 是从Oracle 10g 开始出现的,用于恢复用户误删除的对象(包括表,索引等), 这个技术依赖于Tablespace Recycle Bin(表空间回收站),这个功能和windows的回收站非常类似。

 

            Flashback 不支持sys用户. system表空间下的对象,也不能从回收站里拿到。故使用SYS 或者SYSTEM用户登陆时, show recyclebin 为空。

 

            Flashback Drop 是基于Tablespace RecycleBin 来实现恢复的。 它只支持闪回与table 相关连的对象,比如表,索引,约束,触发器等。 如果是函数或者存储过程等,就需要使用Flashback Query来实现。

 

2.4.3.1. Tablespace Recycle Bin

            从Oracle 10g 开始, 每个表空间都会有一个叫作回收站的逻辑区域,当用户执行drop命令时, 被删除的表和表的关联对象( 包括索引, 约束,触发器,LOB段,LOB index 段) 不会被物理删除, 这些对象先转移到回收站中,这就给用户提供了一个恢复的可能。

 

            When you drop a table, the database does not immediately remove the space associated with the table. The database renames the table and places it and any associated objects in a recycle bin, where, in case the table was dropped in error, it can be recovered at a later time. This feature is calledFlashback Drop, and the FLASHBACK TABLE statement is used to restore the table. Before discussing the use of the FLASHBACK TABLE statement for this purpose, it is important to understand how the recycle bin works, and how you manage its contents.

 

            The recycle bin is actually a data dictionary table containing information about dropped objects. Dropped tables and any associated objects such as indexes, constraints, nested tables, and the likes are not removed and still occupy space. They continue to count against user space quotas, until specifically purged from the recycle bin or the unlikely situation where they must be purged by the database because of tablespace space constraints.

 

From:

http://download.oracle.com/docs/cd/E11882_01/server.112/e17120/tables011.htm#ADMIN11679

 

            初始化参数recyclebin 用于控制是否启用recyclebin功能,缺省是ON, 可以使用OFF关闭。

 

SQL> show parameter recycle

NAME                    TYPE        VALUE

------------------------------------ ----------- ------------------------------

buffer_pool_recycle            string

db_recycle_cache_size         big integer 0

recyclebin                    string      on

 

禁用该功能:

SQL> alter system set recyclebin=off;

SQL> alter system set recyclebin=on;

 

 

SQL> alter session set recyclebin=off;

SQL> alter session set recyclebin=on;

 

            禁用后删除的对象将直接删除,不会写到Recycle中,当然在删除时,指定purge 参数,表也将直接删除,不会写到recyclebin中。

SQL> drop table name purge;

 

查看recyclebin中的对象列表:

SQL> select * from A;

        ID

        ----------

         1

         2

         3

 

SQL> drop table A;

表已删除。

SQL> show recyclebin

ORIGINAL NAME    RECYCLEBIN NAME       OBJECT TYPE  DROP TIME

----------------      -----------------------------          ------------      

A   BIN$RWXQQcTPRde0ws4h9ewJcg==$0  TABLE     2009-10-15:12:44:33

 

查看recyclebin中对象:

SQL> select original_name,object_name from recyclebin;

ORIGINAL_NAME       OBJECT_NAME

-------------------------------- ------------------------------

A                      BIN$RWXQQcTPRde0ws4h9ewJcg==$0

 

 

 

查看recyblebin对象里的内容:

SQL> select * from "BIN$RWXQQcTPRde0ws4h9ewJcg==$0";

        ID

       ----------

         1

         2

         3

 

            表空间的Recycle Bin 区域只是一个逻辑区域,而不是从表空间上物理的划出一块区域固定用于回收站,因此Recycle Bin是和普通对象共用表空间的存储区域,或者说是Recycle Bin的对象要和普通对象抢夺存储空间。

            当发生空间不够时,Oracle会按照先入先出的顺序覆盖Recycle Bin中的对象。

 

也可以手动的删除Recycle Bin占用的空间:

            1). Purge tablespace tablespace_name : 用于清空表空间的Recycle Bin

            2). Purge tablespace tablespace_name user user_name: 清空指定表空间的Recycle Bin中指定用户的对象

            3). Purge recyclebin: 删除当前用户的Recycle Bin中的对象

            4). Purge dba_recyclebin: 删除所有用户的Recycle Bin中的对象,该命令要sysdba权限

            5). Drop table table_name purge:  删除对象并且不放在Recycle Bin中,即永久的删除,不能用Flashback恢复。

            6). Purge index recycle_bin_object_name: 当想释放Recycle bin的空间,又想能恢复表时,可以通过释放该对象的index所占用的空间来缓解空间压力。 因为索引是可以重建的。

 

2.4.3.2. Flashback Drop 实例操作

 

SQL> select original_name,object_name from recyclebin;

ORIGINAL_NAME         OBJECT_NAME

-------------------------------- ------------------------------

A                       BIN$RWXQQcTPRde0ws4h9ewJcg==$0

 

SQL> flashback table a to before drop;

闪回完成。

 

SQL> select * from a;

        ID

         ----------

         1

         2

         3

 

            当我们删除表A后,在新建表A,这时在恢复的时候就会报错,此时我们在闪回时,对表重命名就可以了:

SQL> drop table a;

表已删除。

 

SQL> create table a

  2  (id number(1));

表已创建。

 

SQL> flashback table a to before drop ;

flashback table a to before drop

*

第 1 行出现错误:

ORA-38312: 原始名称已被现有对象使用

 

SQL> flashback table a to before drop rename to B;

闪回完成。

 

SQL> select * from B;

        ID

        ----------

         1

         2

         3

 

            当我们删除表A,在新建表A,在删除它,这是在Recycle Bin中就会有2个相同的表明,此时恢复我们就要指定object_name才行.

 

SQL> select * from B;

        ID

        ----------

         1

         2

         3

 

SQL> drop table B;

表已删除。

SQL> create table B(name varchar(20));

表已创建。

SQL> drop table B;

表已删除。

 

SQL> select original_name,object_name from recyclebin;

 

ORIGINAL_NAME                    OBJECT_NAME

--------------------------------            ------------------------------

B                                BIN$vYuv+g9fTi2exYP9X2048Q==$0

B                                BIN$geQ9+NekSjuRvzG+TqDVWw==$0

 

SQL> flashback table "BIN$vYuv+g9fTi2exYP9X2048Q==$0" to before drop;

闪回完成。

 

SQL> select * from B;

        ID

       ----------

         1

         2

         3

 

            一旦完成闪回恢复,Recycle Bin中的对象就消失了.

 

            如果表上索引或者约束等信息,这些信息也会被恢复,但是这些对象会使用Oracle 自动的命名。 我们需要查看这些对象,然后对这些对象重新命名:如:

 

SQL>select index_name from user_indexes where table_name = 'job_history';

INDEX_NAME

------------------------------

BIN$DBo9UChwZSbgQFeMiAdCcQ==$0

BIN$DBo9UChtZSbgQFeMiAdCcQ==$0

BIN$DBo9UChuZSbgQFeMiAdCcQ==$0

BIN$DBo9UChvZSbgQFeMiAdCcQ==$0

 

重命名:

SQL>alter index "bin$dbo9uchtzsbgqfemiadccq==$0" rename to jhist_job_ix;

 

 

 

Flashback Drop 需要注意的地方:

1). 只能用于非系统表空间和本地管理的表空间

2). 对象的参考约束不会被恢复,指向该对象的外键约束需要重建。

3). 对象能否恢复成功,取决与对象空间是否被覆盖重用。

4). 当删除表时,信赖于该表的物化视图也会同时删除,但是由于物化视图并不会被放入recycle bin,因此当你执行flashback table to before drop 时,也不能恢复依赖其的物化视图,需要dba 手工介入重新创建。

5). 对于Recycle Bin中的对象,只支持查询.