---
title: "oracle 11g新特性Flashback data archive"
author: "daizj"
date: "2013-01-09T22:52:32Z"
tags: ["Oracle"]
categories: ["数据库"]
slug: "oracle-11g-flashback-data-archive"
draft: false
lastmod: "2013-01-09T22:52:32Z"
id: 1767171
url: "https://daizj.iteye.com/blog/1767171"
---

1. 什么是flashback data archive

Flashback data archive是oracle 11g中引入的一个新特性。Flashback archive是一个新的数据库对象，用于存储一个或多表的历史数据。Flashback archive是一个逻辑对象，概念上类似于表空间。实际上flashback archive可以看作是存储一个或多个表的所有事务变化的逻辑空间。

 

2. flashback data archive的实现原理

从9i开始，Oracle就提供了表的flashback query功能，使用户可以查询表上过去某一时刻的数据或一段时间内数据的变化情况。flashback query实际上是利用UNDO信息来获得过去的数据。由于UNDO空间是循环使用的，事务的前映像不可能永久保存，因此flashback query只能支持过去一段时间内的数据查询，这个时间与UNDO_RETENTION相关。

Flashback data archive相比flashback query则更进了一步。Flashback data archive不再直接使用UNDO信息获得历史数据，而是把历史数据保存在一个track table里。Oracle新增加了一个后台进程fbda，此进程每隔一段时间（通常是5分钟）把UNDO中的历史数据信息加到track table里。如果表上开启了flashback archive功能，则Oracle先给事务产生的UNDO信息做标记，直到被标记的UNDO信息加到track table后，此UNDO空间才能被重用。

Flashback data archive采用压缩方式存储历史数据，以减小空间占用。同时flashback data archive采用range分区方式对track table表进行分区。track table表上没有索引，用户可根据需要对track table增加索引。

2.2 Altering a Flashback Data Archive
With the ALTER FLASHBACK ARCHIVE statement, you can:
-- 使用alter flashback archive 可以修改如下内容：
 （1）Change the retention time of a Flashback Data Archive
 （2）Purge some or all of its data
 （3）Add, modify, and remove tablespaces

Note:
 Removing all tablespaces of a Flashback Data Archive causes an error.
 If you are logged on as SYSDBA, you can also use the ALTER FLASHBACK ARCHIVE statement to make a specific file the default Flashback Data Archive for the system.
 -- 不能移除Flashback Data Archive里的所有表空间。 否则报错。 如果用sysdba 登陆，可以修改默认的Flashback archive。

示例：
2.2.1 将Flashback Data Archive 修改为default FA

先用我们具有flashback archive administer 权限的用户试试：
SQL&gt; conn dvd/dvd;
Connected.
SQL&gt; alter flashback archive fla1 set default;
alter flashback archive fla1 set default
*
ERROR at line 1:
ORA-55611: No privilege to manage default Flashback Archive

报错，没有权限，用sysdba 测试成功：
SQL&gt; conn / as sysdba;
Connected.
SQL&gt; alter flashback archive fla1 set default;

Flashback archive altered.

注意一点，只能有一个默认的Flashback archive. 

SQL&gt; select flashback_archive_name name, status from dba_flashback_archive;
NAME STATUS
---------- -------
FLA1 DEFAULT
FLA2

当前默认的Flashback Archive 是FLA1，我们将默认改成FLA2，在查看：
SQL&gt; alter flashback archive fla2 set default;
Flashback archive altered.

SQL&gt; select flashback_archive_name name, status from dba_flashback_archive;
NAME STATUS
---------- -------
FLA1
FLA2 DEFAULT

2.2.2 为已经存在的Flashback Archive 添加表空间，并指定配额

SQL&gt; alter flashback archive fla1 add tablespace fda3 quota 20M;
Flashback archive altered.

2.2.3 为已经存在的Flashback Archive 添加表空间，不指定配额，即需要多少用多少空间

SQL&gt; alter flashback archive fla1 add tablespace fda4;
Flashback archive altered.

2.2.4 修改已经存在的Flashback Archive的配额
SQL&gt; alter flashback archive fla1 modify tablespace fda1 quota 20m;
Flashback archive altered.

2.2.5 修改配额不受限制
SQL&gt; alter flashback archive fla1 modify tablespace fda1;
Flashback archive altered.

2.2.6 修改Flashback Archive 的retention time
SQL&gt; alter flashback archive fla1 modify retention 2 year;
Flashback archive altered.

SQL&gt; alter flashback archive fla1 modify retention 1 month;
Flashback archive altered.

SQL&gt; alter flashback archive fla1 modify retention 2 month;
Flashback archive altered.

SQL&gt; alter flashback archive fla1 modify retention 2 day;
Flashback archive altered.

SQL&gt; alter flashback archive fla1 modify retention 1 day;
Flashback archive altered.

2.2.7 将表空间从Flashback Archive中移除

SQL&gt; alter flashback archive fla1 remove tablespace fda4;
Flashback archive altered.

-- 注意，这里移除的仅仅是Flashback Archive中的信息，表空间不会被删除。

2.2.8 清空Flashback Archive中的所有历史记录

SQL&gt; alter flashback archive fla1 purge all;
Flashback archive altered.

2.2.9 清空Flashback Archive 中超过1天的历史数据
SQL&gt; alter flashback archive fla1 purge before timestamp (systimestamp - interval '1' day);
Flashback archive altered.

2.2.10 清空Flashback Archive 中指定SCN 之前的所有历史数据
SQL&gt; select current_scn from v$database;
CURRENT_SCN
-----------
1315755078

SQL&gt; alter flashback archive fla1 purge before scn 1315755078;
Flashback archive altered.

2.5.1 创建table，使用默认的Flashback Data Archive 来存储历史数据
SQL&gt; create table 安庆 (id number) flashback archive;
Table created.

2.5.2 创建table，使用指定的Flashback Data Archive 来存储历史数据

SQL&gt; create table 怀宁 (id number) flashback archive fla1;
Table created.

2.5.3 对表启用Flashback archive，并使用默认的Flashback archive。

SQL&gt; alter table dave flashback archive;
Table altered.

2.5.4 禁用表的Flashback Archive
SQL&gt; alter table dave no flashback archive;
Table altered.

2.5.5 对table 启用Flashback archive，并指定Flashaback Archive 区。
SQL&gt; alter table dave flashback archive fla1;
Table altered.


DDL Statements on Tables Enabled for Flashback Data Archive
 Flashback Data Archive supports many DDL statements, including some that alter the table definition or move data. For example:
 --启动Flashback Data Archive的表支持以下的DDL 操作
 （1）ALTER TABLE statement that does any of the following:
 1）Adds, drops, renames, or modifies a column
 2）Adds, drops, or renames a constraint
 3）Drops or truncates a partition or subpartition operation
 （2）TRUNCATE TABLE statement
 （3）RENAME statement that renames a table

 Some DDL statements cause error ORA-55610 when used on a table enabled for Flashback Data Archive. For example:
 -- 启动Flashback Data Archive的表上的一些DDL 操作可能触发ORA-55610的错误，这些DDL 如下：
 （1）ALTER TABLE statement that includes an UPGRADE TABLE clause, with or without an INCLUDING DATA clause
 （3）ALTER TABLE statement that moves or exchanges a partition or subpartition operation
 （3）DROP TABLE statement
