说明:
文章整理自2015-08-08我在ITPUB博客发布的《Oracle FLASHBACK》
https://blog.itpub.net/29785807/viewspace-1765524/
注意:文章发布时间较久,可能存在错误,仅供参考,请勿用于生产环境。
一 闪回数据(DELETE)
二 闪回表(Flashback Drop)
三 闪回数据库(Flashback Database)
四 闪回查询(Flashback Query)
五 闪回版本查询(Flashback Version Query)
六 闪回事务查询(Flashback Transaction)
七 闪回归档查询
八 常见错误
一 闪回数据(DELETE)
一:原理
UNDO
二:数据库基本信息
SQL> select name,log_mode,flashback_on from v$database;
NAME LOG_MODE FLASHBACK_ON
------------------ ------------------------ ------------------------------------
CHEN NOARCHIVELOG NO
三 方法
方法一:基于SCN
1 准备数据
SQL> create user chen identified by chen;
User created.
SQL> grant resource,connect to chen;
Grant succeeded.
SQL> create table chen.t1 as select level as id from dual connect by level select current_scn from v$database;
CURRENT_SCN
-----------
1726953
3 删除数据
SQL> conn chen/chen
Connected.
SQL> delete t1;
10 rows deleted.
SQL> commit;
Commit complete.
4 基于SCN闪回删除
SQL> flashback table t1 to scn 1726953;
flashback table t1 to scn 1726953
*
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled
被flashback回来rowid发生了变化,这也是为什么flashback table 需要enable row movement的原因,正常情况表中 数据的rowid是不可以改变的;
SQL> alter table t1 enable row movement;
Table altered.
SQL> flashback table t1 to scn 1726953;
Flashback complete.
SQL> select count(*) from t1;
COUNT(*)
----------
10
方法二:基于时间
1 查看当前时间
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as t_time from dual;
T_TIME
--------------------------------------
2015-08-06 15:17:48
2 删除数据
SQL> delete t1;
10 rows deleted.
SQL> commit;
Commit complete.
3 基于时间闪回删除
SQL> alter table t1 enable row movement;
Table altered.
SQL> flashback table t1 to timestamp to_timestamp('2015-08-06 15:17:22','yyyy-mm-dd hh24:mi:ss');
Flashback complete.
SQL> select count(*) from t1;
COUNT(*)
----------
10
方法三:基于闪存查询
1 查看当前SCN或当前时间
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
1727759
2 删除数据
SQL> conn chen/chen
Connected.
SQL> delete t1;
10 rows deleted.
SQL> commit;
Commit complete.
3 通过闪回查询创建表t2
SQL> create table t2 as select * from t1 as of scn 1727759;
Table created.
/*
或者
SQL> create table t2 as select * from t1 as of timestamp to_timestamp('2015-08-06 15:25:56','yyyy-mm-dd hh24:mi:ss');
Table created.
*/
4 重命名
SQL> rename t1 to t1_old;
Table renamed.
SQL> rename t2 to t1;
Table renamed.
SQL> select count(*) from t1;
COUNT(*)
----------
10
二 闪回表(Flashback Drop)
一:原理
回收站
闪回丢弃是将被丢弃的数据库对象及其相依对象的复制保存在回收站中,以便在必要时能够及时恢复这些对象。在回收站被清空以前,被丢弃的对象并没有从数据库中删除。
这就使数据库能够恢复被意外或者误操作而删除的表。
/*FLASHBACK语句会还原最后放入回收站的表,而PURGE语句会清除最早进入回收站的表*/
二:数据库基本信息
SQL> select name,log_mode,flashback_on from v$database;
NAME LOG_MODE FLASHBACK_ON
-------------------- ------------------------ ------------------------------------
CHEN NOARCHIVELOG NO
SQL> col name for a20
SQL> col type for a10
SQL> col value for a8
SQL> show parameter recyclebin
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
recyclebin string on
三:方法
不清空回收站的恢复
1 删除表
SQL> drop table t1;
Table dropped.
2 查看回收站
SQL> show recyclebi
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T1 BIN$HKCe4+VkDVXgU8UDqMAWvQ==$0 TABLE 2015-08-06:15:39:34
SQL> select object_name,ORIGINAL_NAME from user_recyclebin;
OBJECT_NAME ORIGI
------------------------------ -----
BIN$HKCe4+VkDVXgU8UDqMAWvQ==$0 T1
SQL> select original_name,operation,droptime from recyclebin;
ORIGI OPERATION DROPTIME
----- ------------------ --------------------------------------
T1 DROP 2015-08-06:15:39:34
SQL> select tname from tab;
TNAME
------------------------------------------------------------
BIN$HKCe4+VkDVXgU8UDqMAWvQ==$0
SYS_TEMP_FBT
T1_OLD
T2
SQL> select * from t1;
select * from t1
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from "BIN$HKCe4+VkDVXgU8UDqMAWvQ==$0";
ID
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.
3 恢复
恢复方法一:
SQL> create table t1 as select * from "BIN$HKCe4+VkDVXgU8UDqMAWvQ==$0";
Table created.
恢复方法二:
SQL> show recyclebin
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T1 BIN$HKC57EgIDWXgU8UDqMA0zA==$0 TABLE 2015-08-06:15:47:08
SQL> flashback table t1 to before drop;
或者
SQL> flashback table "BIN$HKC57EgJDWXgU8UDqMA0zA==$0" to before drop;
或者(闪回表并且重命名表)
SQL> flashback table t1 to before drop rename to t1_1;
2 清空回收站的恢复(PURE)
drop table t1 purge;
清空回收站后不能按照以上方法进行恢复,可以通过闪回数据库进行恢复。
三 闪回数据库(Flashback Database)
一:原理
闪回日志
Flashback Database整个架构包括一个进程Recover Writer(RVWR)后台进程,Flashback Database Log日志和Flash Recovery Area。
一旦数据库启用了Flashback Database,则RVWR进程会启动,该进程会向Flash Recovery Area中写入Flashback Database Log,这些日志包括的是数据块的前镜像(before image),这也是Flashback Database技术不完全恢复块的原因。
/*该功能不基于撤销数据(undodata),而是基于闪回日志*/
/*使用闪回数据库恢复,恢复时间是由恢复过程中需要备份的变化的数量决定的,而不是数据文件和归档日志的大小*/
/*闪回数据库的结构是由恢复写入器(RVWR)后台进程和闪回数据库日志组成的*/
配置闪回数据库
配置闪回恢复区以后,要启用闪回数据库功能,还需要进行进一步的配置,需要注意如下几点:
1 配置闪回恢复区。
2 数据库需要运行在归档模式下(Archivelog)。
3 通过数据库参数DB_FLASHBACK_RETENTION_TARGET,来指定可以在多长时间内闪回数据库。
该值以分钟为单位,默认值为1440(1天),更大的值对应更大的闪回恢复空间,类似于闪回数据库的基线。
4 需要在MOUNT状态下使用ALTER DATABASE FLASHBACK ON 命令启动闪回数据库功能。
二 查看数据库基本信息
1 查看是否启动闪回数据库功能
SQL> select name,log_mode,flashback_on from v$database;
NAME LOG_MODE FLASHBACK_ON
-------------------- ------------------------ ------------------------------------
CHEN NOARCHIVELOG NO
2 查看是否使用spfile文件
SQL> show parameter spfile
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
spfile string u01/app/oracle/product/11.2.4
/dbs/spfilechen.ora
3 查看闪回区大小及路径
SQL> show parameter db_recovery_file_dest
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
db_recovery_file_dest string u01/app/oracle/flash_recovery_area
db_recovery_file_dest_size big integer 3882M
4 查看保留时间
SQL> show parameter DB_FLASHBACK_RETENTION_TARGET
NAME TYPE VALUE
------------------------------------ ---------- ------------------------------
db_flashback_retention_target integer 1440
5 查看闪回日志文件
[oracle@cjc flashback]$ pwd
/u01/app/oracle/flash_recovery_area/CHEN/flashback
[oracle@cjc flashback]$ ls
o1_mf_bw69sdgk_.flb o1_mf_bw69sg6d_.flb
6 查看归档目录,格式
SQL> set linesize 100
SQL> show parameter log_archive_format
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
log_archive_format string %t_%s_%r.dbf
SQL> show parameter log_archive_dest_
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
log_archive_dest_1 string
log_archive_dest_2 string
...
将闪回恢复区的大小设置为4GB。
SQL> ALTER SYSTEM SET db_recovery_file_dest_size=4g SCOPE=BOTH;
要停用闪回恢复区,只需将参数db_recovery_file_dest置空就可以了。
SQL>ALTER SYSTEM SET db_recovery_file_dest='';
当闪回恢复区中的空间使用率超过85%的时候,数据库将会向alert文件中写入警告信息。而当超过97%的时候将会写入严重告警信息。
当闪回恢复区空间全部耗尽的时候,Oracle将报告如下类似的错误:
ORA-19809: limit exceeded for recovery files
ORA-19804: cannot reclaim 52428800 bytes disk space from 1258291200 limit
此时查询视图dba_outstanding_alerts,将会给出错误的原因及操作建议
三 启动归档和闪回功能
SQL> ho mkdir {chen_archive1,chen_archive2} -p
SQL> alter system set log_archive_dest_1='location=/home/oracle/chen_archive1' scope=both;
System altered.
SQL> alter system set log_archive_dest_2='location=/home/oracle/chen_archive2' scope=both;
System altered.
SQL> shutdown immediate
SQL> alter database archivelog;
Database altered.
SQL> alter database flashback on;
Database altered.
SQL> alter database open;
Database altered.
SQL> select name,log_mode,flashback_on from v$database;
NAME LOG_MODE FLASHBACK_ON
------------------ ------------------------ ------------------------------------
CHEN ARCHIVELOG YES
SQL> ho ps -ef|grep ora_
......
oracle 9059 1 0 17:21 ? 00:00:00 ora_rvwr_chen
oracle 9100 1 0 17:26 ? 00:00:00 ora_arc0_chen
oracle 9102 1 0 17:26 ? 00:00:00 ora_arc1_chen
oracle 9104 1 0 17:26 ? 00:00:00 ora_arc2_chen
oracle 9106 1 0 17:26 ? 00:00:00 ora_arc3_chen
......
四:闪回数据库示例
1 创建表,查看当前SCN和时间
SQL> conn chen/chen
Connected.
SQL> create table t1 as select level as id from dual connect by level conn as sysdba
Connected.
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
1733751
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as t_time from dual;
T_TIME
--------------------------------------
2015-08-06 17:40:57
2 删除表同时清空回收站
SQL> conn chen/chen
Connected.
SQL> drop table t1 purge;
Table dropped.
3 闪回数据库
SQL> shutdown immediate
SQL> startup mount
SQL> flashback database to scn 1733751;
Flashback complete.
或者
SQL> flashback database to timestamp to_timestamp('2014-06-25 10:57:48','yyyy-mm-dd hh24:mi:ss');
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
SQL> alter database open resetlogs;
Database altered.
SQL> select count(*) from chen.t1;
COUNT(*)
----------
10
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination home/oracle/chen_archive2
Oldest online log sequence 1
Next log sequence to archive 1
Current log sequence 1
四 闪回查询
一:原理
UNDO
二:查看
1 查询t1表2分钟之前的数据
SQL> select * from t1 as of timestamp (systimestamp - interval '2' minute);
SQL> select * from t1 as of timestamp (systimestamp - interval '2' minute) where ...;
2 查询t1表120秒之前的数据
SQL> select * from t1 as of timestamp (systimestamp - interval '120' second);
3 查询dept表12小时之前的数据
SQL> select * from dept as of timestamp (systimestamp - interval '12' hour);
4 查询dept表12天之前的数据
SQL> select * from dept as of timestamp (systimestamp - interval '12' day);
5 查询cjc表2天之前的数据
SQL> select * from cjc as of timestamp sysdate-2;
SQL> select * from cjc as of timestamp to_timestamp('2014-08-24 00:13:59', 'yyyy-mm-dd hh24:mi:ss');
6 时间和scn之间的转换
SQL> select timestamp_to_scn(to_timestamp('2014-08-24 05:15:22','yyyy-mm-dd hh24:mi:ss')) scn from dual;
SCN
----------
1081597
SQL> select scn_to_timestamp(1081597) scn from dual;
SCN
------------------------------------
24-AUG-14 05.15.21.000000000 AM
五 闪回版本查询(Flashback Version Query)
一:原理
UNDO
在 Oracle9i Database 中,我们看到它推出了以闪回查询形式表示的“时间机器”。
该特性允许 DBA 看到特定时间的列值,只要在还原段中提供该数据块此前镜像的拷贝即可。
但是,闪回查询只提供某时刻数据的固定快照,而不是在两个时间点之间被更改数据的运行状态表示。
某些应用程序,如涉及到外币管理的应用程序,可能需要了解一段时期内数值数据的变化,而不仅仅是两个时间点的数值。
由于闪回版本查询特性,Oracle Database 10g 能够更方便高效地执行该任务。
二:准备数据
SQL> create table t1 as select level as id from dual connect by level alter table t1 add(a number);
Table altered.
SQL> update t1 set a=id;
10 rows updated.
SQL> commit;
Commit complete.
SQL> delete t1 where id=7;
1 row deleted.
SQL> commit;
Commit complete.
SQL> insert into t1 values(7,7);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t1;
ID A
---------- ----------
1 1
2 2
3 3
4 4
5 5
6 6
8 8
9 9
10 10
7 7
10 rows selected.
四:闪回查看版本
其中:
U 代表UPDATE
I 代表INSERT
D 代表DELETE
SQL> select versions_xid,versions_startscn,versions_endscn,versions_operation from t1 versions between scn minvalue and maxvalue order by 2;
VERSIONS_XID VERSIONS_STARTSCN VERSIONS_ENDSCN VE
---------------- ----------------- --------------- --
08001800A3040000 1739604 U
08001800A3040000 1739604 U
08001800A3040000 1739604 U
08001800A3040000 1739604 U
08001800A3040000 1739604 U
08001800A3040000 1739604 U
08001800A3040000 1739604 U
08001800A3040000 1739604 1739615 U
08001800A3040000 1739604 U
08001800A3040000 1739604 U
09000D00A3040000 1739615 D
07000500C3030000 1739655 I
1739604
1739604
1739604
1739604
1739604
1739604
1739604
1739604
1739604
1739604
22 rows selected.
SQL> col versions_endtime for a25
SQL> col versions_starttime for a25
SQL> select versions_starttime, versions_endtime, versions_xid,versions_operation from t1 versions between timestamp minvalue and maxvalue order by VERSIONS_STARTTIME;
VERSIONS_STARTTIME VERSIONS_ENDTIME VERSIONS_XID VE
------------------------- ------------------------- ---------------- --
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 07-AUG-15 11.16.03 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.15.42 AM 08001800A3040000 U
07-AUG-15 11.16.03 AM 09000D00A3040000 D
07-AUG-15 11.16.24 AM 07000500C3030000 I
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
07-AUG-15 11.15.42 AM
22 rows selected.
SQL> select versions_starttime, versions_endtime, versions_xid,versions_operation from t1 versions between scn 1743474 and 1743668 order by 1;
VERSIONS_STARTTIME VERSIONS_ENDTIME VERSIONS_XID VE
------------------------- ------------------------- ---------------- --
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM 02000100F1040000 U
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
07-AUG-15 01.00.13 PM
20 rows selected.
SQL> select timestamp_to_scn(to_timestamp('2015-08-07 11:15:42','yyyy-mm-dd hh24:mi:ss')) scn from dual;
/*或者select timestamp_to_scn(to_date('2015-08-07 11:15:42','yyyy-mm-dd hh24:mi:ss')) scn from dual;*/
SCN
----------
1739603
SQL> select timestamp_to_scn(to_timestamp('2015-08-07 11:16:24','yyyy-mm-dd hh24:mi:ss')) scn from dual;
SCN
----------
1739654
SQL> select versions_starttime, versions_endtime, versions_xid,versions_operation from t1 versions between scn 1739603 and 1739654;
select versions_starttime, versions_endtime, versions_xid,versions_operation from t1 versions between scn 1739603 and 1739654
*
ERROR at line 1:
ORA-30052: invalid lower limit snapshot expression
SQL> show parameter undo
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
注意,此处显示了对该行所作的所有更改,甚至包括该行被删除和重新插入的情况。
VERSION_OPERATION 列显示对该行执行了什么操作 (Insert/Update/Delete)。
所做的这些工作不需要历史表或额外的列。
在上述查询中,列 versions_starttime、versions_endtime、versions_xid、versions_operation 是伪列,与 ROWNUM、LEVEL 等其他熟悉的伪列相类似。
其他伪列 — 如 VERSIONS_STARTSCN 和 VERSIONS_ENDSCN — 显示了该时刻的系统更改号。
列 versions_xid 显示了更改该行的事务标识符。有关该事务的更多详细信息可在视图FLASHBACK_TRANSACTION_QUERY 中找到,其中列 XID 显示事务 id。
例如,使用上述的 VERSIONS_XID 值 000A000D00000029,UNDO_SQL 值显示了实际的语句。
注:versions_starttime : 这个数据开始生效的时间
VERSIONS_ENDTIME :这个数据失效的时间--一般就是下面一条记录开始的时间
versions_xid : 显示了更改该行的事务标识符。
VERSION_OPERATION :这条记录执行的操作(Insert/Update/Delete)
六 闪回事务查询(Flashback Transaction)
一:原理
REDO LOG
闪回事务又称撤销事务(Backout Transation),能够撤销一个或多个事务的修改,其功能由一个名为DBMS_FLASHBACK.TRANSACTION_BACKOUT的存储过程实现。
该存储过程的工作原理是自动分析重做日志,挖掘出变更前的值用以构建撤销SQL(Undo SQL),然后执行撤销SQL最后达到撤销的目的。
为了该功能可以正常使用,至少需要事先启用主键补充日志。另外,为了能够跟踪外键依赖还需要启用外键补充日志。
闪回事务查询有别于闪回查询的特点有以下3个:
(1)其正常工作不但需要利用撤销数据,还需要事先启用最小补充日志。
(2)返回的结果不是以前的“旧”数据,而是能够将当前数据修改为以前的样子的撤销SQL(Undo SQL)语句。
(3)集中地在名为flashback_transaction_query表上查询,而不是在各个表上通过“as of”或“versions between”子句查询。
二:不启用补充闪回事务查询
SQL> insert into t1 values(10000,10000);
1 row created.
SQL> commit;
SQL> select versions_xid,versions_startscn from t1 versions between timestamp minvalue and maxvalue order by 2;
VERSIONS_XID VERSIONS_STARTSCN
---------------- -----------------
07001F00D5030000 1752034
SQL> conn as sysdba
Connected.
SQL> select undo_sql from flashback_transaction_query where xid='07001F00D5030000';
UNDO_SQL
--------------------------------------------------------------------------------
SQL> select supplemental_log_data_fk,supplemental_log_data_all,supplemental_log_data_min from v$database;
SUPPLE SUPPLE SUPPLEMENTAL_LOG
------ ------ ----------------
NO NO NO
三 启用补充闪回事务查询
SQL> alter database add supplemental log data;
Database altered.
/*SQL> alter database drop supplemental log data;*/
SQL> select supplemental_log_data_fk,supplemental_log_data_all,supplemental_log_data_min from v$database;
SUPPLE SUPPLE SUPPLEMENTAL_LOG
------ ------ ----------------
NO NO YES
SQL> delete t1 where id=10000;
1 row deleted.
SQL> commit;
Commit complete.
SQL> select versions_xid,versions_startscn from t1 versions between timestamp minvalue and maxvalue order by 2;
VERSIONS_XID VERSIONS_STARTSCN
---------------- -----------------
08000300B3040000 1784373
SQL> conn as sysdba
Connected.
SQL> select undo_sql from flashback_transaction_query where xid='08000300B3040000';
UNDO_SQL
--------------------------------------------------------------------------------
insert into "CHEN"."T1"("ID","A") values ('10000','10000');
七 闪回归档查询
一:原理
UNDO归档
从Oracle Database 11g开始,Oracle 提供了一个这样的功能:闪回数据归档(Flashback Data Archive)。
通过这一功能Oracle数据库可以将UNDO数据进行归档,从而提供全面的历史数据查询,也因此Oracle引入一个新的概念Oracle Total Recall,也即Oracle全面回忆功能。
闪回数据归档可以和我们一直熟悉的日志归档类比,日志归档记录的是Redo的历史状态,用于保证恢复的连续性;
而闪回归档记录的是UNDO的历史状态,可以用于对数据进行闪回追溯查询;
后台进程LGWR用于将Redo信息写出到日志文件,ARCH进程负责进行日志归档;
在Oracle 11g中,新增的后台进程FBDA(Flashback Data Archiver Process)则用于对闪回数据进行归档写出:
SQL> ho ps -ef|grep fb
oracle 9173 1 0 16:38 ? 00:00:00 ora_fbda_chen
闪回归档数据甚至可以以年为单位进行保存,Oracle可以通过内部分区和压缩算法减少空间耗用,这一特性对于需要审计以及历史数据分区的环境尤其有用,但是注意,对于繁忙的数据库环境,闪回数据存储显然要耗用更多的存储空间。
当然,用户可以根据需要,对部分表进行闪回数据归档,从而满足特定的业务需求。
二:创建UNDO归档表空间
SQL> create tablespace fbda datafile '/u01/app/oracle/oradata/chen/fbda01.dbf' size 50M autoextend on;
Tablespace created.
三:设置闪回归档数据保存一个月
SQL> create flashback archive fdba tablespace fbda retention 1 month;
Flashback archive created.
四:切换较小的回退段表空间
SQL> create undo tablespace undotbs2 datafile '/u01/app/oracle/oradata/chen/undotbs2_01.dbf' size 10M;
Tablespace created.
SQL> alter system set undo_tablespace=undotbs2;
System altered.
SQL> set linesize 200
SQL> show parameter undo
NAME TYPE VALUE
------------------------------------ ---------------------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS2
五:对表T1开启闪回归档
SQL> conn as sysdba
Connected.
SQL> grant flashback archive administer to chen;
Grant succeeded.
SQL> conn chen/cjc
Connected.
SQL> alter table t1 flashback archive fdba;
Table altered.
取消对于数据表的闪回归档可以使用如下命令
alter table table_name no flashback archive;
六:插入大量数据
SQL> conn chen/cjc
Connected.
SQL> select count(*) from t1;
COUNT(*)
----------
10
SQL> insert into t1 select level as id,level as a from dual connect by level commit;
Commit complete.
七:开启TRACE跟踪
SQL> conn as sysdba
Connected.
SQL> @$ORACLE_HOME/sqlplus/admin/plustrce.sql
SQL> grant plustrace to chen;
Grant succeeded.
八:执行一次较近的闪回查询
SQL> conn as sysdba
Connected.
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
1821700
SQL> conn chen/cjc
Connected.
SQL> set autotrace on
SQL> select count(*) from t1 as of scn 1821700;
COUNT(*)
----------
10010
Execution Plan
----------------------------------------------------------
Plan hash value: 3724264953
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 9 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| T1 | 654 | 9 (0)| 00:00:01 |
-------------------------------------------------------------------
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
23 consistent gets
0 physical reads
0 redo size
528 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
九:执行一次较久远的闪回查询(通过UDNO归档查询)
SQL> select count(*) from t1 as of scn 1820792;
COUNT(*)
----------
10
Execution Plan
----------------------------------------------------------
Plan hash value: 1563784122
------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 4250 (1)| 00:00:52 | | |
| 1 | SORT AGGREGATE | | 1 | | | | | | |
| 2 | VIEW | | 34 | | | 4250 (1)| 00:00:52 | | |
| 3 | UNION-ALL | | | | | | | | |
| 4 | PARTITION RANGE SINGLE| | 1 | 28 | | 2 (0)| 00:00:01 | 1 | 1 |
|* 5 | TABLE ACCESS FULL | SYS_FBA_HIST_75719 | 1 | 28 | | 2 (0)| 00:00:01 | 1 | 1 |
|* 6 | FILTER | | | | | | | | |
| 7 | MERGE JOIN OUTER | | 33 | 67320 | | 4248 (1)| 00:00:51 | | |
| 8 | SORT JOIN | | 33 | 396 | | 10 (10)| 00:00:01 | | |
|* 9 | TABLE ACCESS FULL | T1 | 33 | 396 | | 9 (0)| 00:00:01 | | |
|* 10 | SORT JOIN | | 9963 | 19M| 38M| 4238 (1)| 00:00:51 | | |
|* 11 | TABLE ACCESS FULL | SYS_FBA_TCRV_75719 | 9963 | 19M| | 19 (0)| 00:00:01 | | |
------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
5 - filter("ENDSCN">1820792 AND "ENDSCN" drop restore point chen_rollback;
Restore point dropped.
SQL> alter database noarchivelog;
Database altered.
2 数据库归档自定义路径必须有location参数,否则报ORA-16179错误
SQL> alter system set log_archive_dest_1='/home/oracle/chen_archive1' scope=both;
alter system set log_archive_dest_1='/home/oracle/chen_archive1' scope=both
*
ERROR at line 1:
ORA-32017: failure in updating SPFILE
ORA-16179: incremental changes to "log_archive_dest_1" not allowed with SPFILE
SQL> alter system set log_archive_dest_1='location=/home/oracle/chen_archive1' scope=both;
System altered.
3 开启数据库闪回功能之前,必须开启数据库归档
SQL> alter database flashback on;
alter database flashback on
*
ERROR at line 1:
ORA-38706: Cannot turn on FLASHBACK DATABASE logging.
ORA-38707: Media recovery is not enabled.
SQL> alter database archivelog;
Database altered.
SQL> alter database flashback on;
Database altered.
###chenjuchao 20240224###