ORACLE數據庫flashback使用手冊
- 概述
對於oracle數據庫flashback技術,主要包含閃回刪除(flashback drop),閃回表(flashback dml)和閃回數據庫(flashback database),本文主要針對閃回刪除,和閃回查詢進行分析,以及闡述操作步驟,在應對生產環境造成的誤操作進行恢復提供應急恢復方案。
如果使用drop table指定刪除表,該表不會從數據庫中立刻刪除,而是保持在原表位置,但是將刪除的表重命名,並將刪除的表存儲到回收站中,回收站中記錄被刪除表新名字和原名字,顯然此時被刪除表所佔用的空間沒有被立即釋放,變成數據庫中可用的潛在空間,保留一段時間,知道回收站空間不足或使用purge指定刪除回收站內容。
回收站是一個邏輯結構,刪除表後,物理上該表沒有被刪除,但是它所佔用的空間回收空閒列表上,也就是可用空間。
-
- 閃回刪除限制
1 對於sys和system用戶下的表無法進行flashback。
2 對於刪除的表如果不存在與回收站則無法進行flashback。
-
- 閃回刪除開啓
show parameter recyclebin
如果爲on則開啓閃回刪除。默認爲on (system和sys賬戶下的對象刪除後不到回收站不能進行flashback)
C:\Users\Administrator>sqlplus / as sysdba SQL*Plus: Release 12.2.0.1.0 Production on 星期一 9月 23 17:53:30 2019 Copyright (c) 1982, 2016, Oracle. All rights reserved. 連接到: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production SQL> show parameter recyclebin NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ recyclebin string on SQL> create table fla01 as select * from dba_objects; 表已創建。 SQL> drop table fla01; 表已刪除。 SQL> flashback table fla01 to before drop; flashback table fla01 to before drop * 第 1 行出現錯誤: ORA-38305: 對象不在回收站中 |
-
- 閃回刪除命令
flashback table t to before drop;
flashback table &recyclebin_name to before drop rename to t_test;
FLASHBACK TABLE my_table TO BEFORE DROP RENAME TO my_table2
. One of the tables is recovered from the Recycle Bin using a Last In First Out (LIFO) approach
-
- 回收站相關命令
Purge tablespace users user scott;
Purge dba_recyclebin
Purge table test;
Purge table &recyclebin_name
相關問題:
1 DROP TABLE T1 ;
CREATE TABLE T1 AS SELECT 1 NAME FROM DUAL;
SELECT *FROM USER_RECYCLEBIN;
PURGE TABLE T1; purge只清楚oldest scn記錄。
-
- 恢復案例
SQL> create user nmosdb identified by "123"; 用戶已創建。 SQL> grant dba,connect,resource to nmosdb; 授權成功。 SQL> connect nmosdb/123 已連接。 SQL> show user USER 爲 "NMOSDB" SQL> SQL> SQL> create table fla01 as select * from dba_objects; SQL> create table fla01 as select * from dba_objects where rownum<10; 表已創建。 SQL> drop table fla01; 表已刪除。 SQL> select count(1) from dba_recyclebin where ORIGINAL_NAME='FLA01';
COUNT(1) ---------- 1 SQL> flashback table fla01 to before drop; 閃回完成。 SQL> select count(1) from fla01;
COUNT(1) ---------- 9 |
- 閃回表(flashback dml)
閃回表利用undo表空間裏面記錄的數據舊映像,可以指定undo表空間的retention guarantee屬性保證undo_retention參數範圍內閃回。
-
- 閃回表限制
閃回點到閃回時間點之間,表不能發生結構變化,例如truncate等ddl操作。
-
- 閃回表命令
先查詢數據庫是否存在
Select * from table as of scn xxx;
Select * from table as of timestamp sysdate-1;
進行閃回(開啓表的行遷移功能)
Flashback table to timestamp to_date();
Flashback table to scn xxxxx;
-
- 添加列閃回
SELECT CURRENT_SCN FROM V$DATABASE;
DELETE FROM T1;
COMMIT;
ALTER TABLE T1 ADD ITEM_DATE1 DATE NOT NULL; 不可以
ALTER TABLE T1 ADD ITEM_DATE1 DATE ; 可以
ALTER TABLE T1 ENABLE ROW MOVEMENT;
flashback table t1 to scn 2220504;
-- 初始化參數undo_retention undo_retention=1800 (1800s=0.5h),
若條件允許,可以適當調大此參數。 -- 是否強制保留undo_retention的時間
select tablespace_name, retention from dba_tablespaces where tablespace_name like 'UNDO%'; alter tablespace undotbs1 retention guarantee;
-
- 閃回查詢
select * from at as of timestamp to_date() where object_name=’123’
select * from at as of scn 1748494 where object_name='123'—空
select * from at as of scn 1748894 where object_name='123'—20行
select count(1) from t1 as of timestamp systimestamp - interval '5' minute;
select * from nmosdb.aoi_db_config as of timestamp systimestamp - interval '2' minute;
select * from nmosdb.aoi_db_config as of timestamp systimestamp - interval '2' second;
select * from nmosdb.aoi_db_config as of timestamp systimestamp - interval '1' day;
select * from nmosdb.aoi_db_config as of timestamp systimestamp - interval '1' hour;
select * from nmosdb.aoi_db_config as of timestamp systimestamp - interval '1' month;
select * from nmosdb.aoi_db_config as of timestamp systimestamp - interval '1' year;
在人爲操作或應用程序錯誤時,使用FLASHBACK TABLE語句恢復表到一個早期狀態。表可以閃回到過去的時間點,依賴於系統中撤銷數據的數據量。此外,Oracle數據庫不能恢復到通過任何DDL操作改變了表結構的早期狀態。
(注意:Oracle強烈建議數據庫運行在自動撤銷模式下,通過設置UNDO_MANAGEMENT初始參數值爲AUTO,默認是自動模式。另外,設置UNDO_RETENTION初始化參數的間隔足夠大,包括你預計需要的最早數據。)
你不能回滾FLASHBACK TABLE語句。但是,你可以執行另一個FLASHBACK TABLE語句,指定一個早於當前時間的時間點。
-
- 恢復案例
SQL> select count(1) from fla01;
COUNT(1) ---------- 9
SQL> delete from fla01;
已刪除 9 行。
SQL> commit;
提交完成。
SQL> select count(1) from fla01 as of timestamp sysdate-1/1440;
COUNT(1) ---------- 0
SQL> select count(1) from fla01 as of timestamp sysdate-2/1440;
COUNT(1) ---------- 9
SQL>alter table fla01 enable row movement; SQL> flashback table fla01 to timestamp sysdate-2/1440;
閃回完成。 SQL> select count(1) from fla01 2 ;
COUNT(1) ---------- 9 |
FLASHBACK TABLE語句能恢復多長時間之前的數據呢?即撤銷數據能保留多長時間。
Oracle數據庫基於撤銷表空間是如何配置的,自動調整撤銷保留期限。
撤銷表空間是自動擴展的,數據庫動態調整撤銷保留期限,略長於系統中運行時間最常的主動查詢。
撤銷表空間是固定大小,UNDO_RETENTION參數值會被忽略。數據庫動態調整一個最佳的保留期限。
UNDO_MANAGEMENT初始化參數,默認值爲AUTO,即自動撤銷管理。UNDO_RETENTION初始化參數,默認值爲900(相當於15分鐘)。
通過動態數據自定v$undostat,查看最近4天數據庫動態動態調整撤銷保留期限的值:
select begin_time, end_time, tuned_undoretention
from v$undostat
order by desc end_time;