闪回查询(SELECT AS OF)
2015-07-13 16:26
295 查看
使用Flashback Query的场景包括如下:
摘自官档Recovering lost data or undoing incorrect, committed changes.
For example, if you mistakenly delete or update rows, and then commit them, you can immediately undo the mistake.
Comparing current data with the corresponding data at an earlier time.
For example, you can run a daily report that shows the change in data from yesterday. You can compare individual rows of table data or find intersections or unions of sets of rows.
Checking the state of transactional data at a particular time.
For example, you can verify the account balance of a certain day.
Simplifying application design by removing the need to store some kinds of temporal data.
Oracle Flashback Query lets you retrieve past data directly from the database.
Applying packaged applications, such as report generation tools, to past data.
Providing self-service error correction for an application, thereby enabling users to undo and correct their errors.
需要授予 FLASHBACK ANY TABLE 权限
假设表
personnel在早上4点30的时候发现
Smith的记录被删,DBA确定在前天晚上7点30的时候数据是准确的,于是我们可以使用
Flashback Query技术找回丢失的数据
查纪录
SELECT * FROM personnel AS OF TIMESTAMP TO_TIMESTAMP('2012-03-21 07:30:00', 'YYYY-MM-DD HH:MI:SS') WHERE UPPER(last_name) = 'SMITH';
使用闪回查询恢复数据
INSERT INTO personnel ( SELECT * FROM personnel AS OF TIMESTAMP TO_TIMESTAMP('2012-03-21 07:30:00', 'YYYY-MM-DD HH:MI:SS') WHERE UPPER(last_name) = 'SMITH' );
创建过去表的一个视图
CREATE VIEW hour_ago AS SELECT * FROM employees AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '60' MINUTE);
可以在自连接中使用
AS OF子句,或者
INTERSECT和
MINUS提取或者比较两个不同时间点的数据
也可以使用下面的方法恢复丢失的数据
INSERT INTO employees (SELECT * FROM employees AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '60' MINUTE) MINUS SELECT * FROM employees;
场景
CREATE TABLE flashback_query_test ( id NUMBER(10) ); SELECT current_scn, TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') FROM v$database; CURRENT_SCN TO_CHAR(SYSTIMESTAM ----------- ------------------- 722452 2004-03-29 13:34:12 INSERT INTO flashback_query_test (id) VALUES (1); COMMIT; SELECT COUNT(*) FROM flashback_query_test; COUNT(*) ---------- 1 SELECT COUNT(*) FROM flashback_query_test AS OF TIMESTAMP TO_TIMESTAMP('2004-03-29 13:34:12', 'YYYY-MM-DD HH24:MI:SS'); COUNT(*) ---------- 0 SELECT COUNT(*) FROM flashback_query_test AS OF SCN 722452; COUNT(*) ---------- 0
使用DBMS_FLASHBACK
进程闪回查询
This can as well been done with SCN using ENABLE_AT_SYSTEM_CHANGE_NUMBERprocedure or timestamp using
ENABLE_AT_TIMEprocedure:
SQL> SELECT current_scn, SCN_TO_TIMESTAMP(current_scn),TO_CHAR(SYSDATE,'dd-mon-yyyy hh24:mi:ss') AS current_time FROM v$database;
CURRENT_SCN SCN_TO_TIMESTAMP(CURRENT_SCN) CURRENT_TIME
----------- --------------------------------------------------------------------------- -----------------------------
28954179 15-AUG-11 11.03.48.000000000 AM 15-aug-2011 11:03:48
SQL> EXEC dbms_flashback.enable_at_system_change_number(28954179);
PL/SQL PROCEDURE successfully completed.
SQL> SELECT * FROM test1;
ID DESCR
---------- ------------------------------
1 One
2 Two
3 Three
SQL> EXEC dbms_flashback.disable;
PL/SQL PROCEDURE successfully completed.
SQL> SELECT * FROM test1;
ID DESCR
---------- ------------------------------
1 TEMPORARY
2 TEMPORARY
3 TEMPORARY
[/code]最后,闪回表到过去,(效果和上面的insert into as select一样)
SQL> flashback TABLE test1 TO scn 28954179;
Flashback complete.
SQL> SELECT * FROM test1;
ID DESCR
---------- ------------------------------
1 One
2 Two
3 Three
[/code]
来自为知笔记(Wiz)
相关文章推荐
- 【C#】判断文件是否正在被其他进程占用
- 用了 CSDN 的 markdown 编辑器吐槽下~~
- 视频监控系统:C/S & B/S
- 深入理解Windows Phone 8.1 UI控件编程
- 解析XML三种方式(PULL、SAX、DOM)
- 条码扫描二维码扫描——ZXing android 源码简化
- DisplayIndex属性设置错乱的解决方案
- android 的unregisterReceiver报错处理 提示“Receiver not registered”
- GIT界的神探--bisect
- CentOS 6图形界面自定义开启终端快捷键
- truncate table
- Android网络编程之Http请求服务器数据(GET方式)
- genymotion报出unable to connect to virtual device的解决方法
- 1139_My Summary
- Android PullToRefresh 实战(ListView)
- Less
- 实现数字转换RMB大写~
- static类型指针使用中的问题(AVPicture 类型内存释放)
- linux SWIG C++转换为JAVA
- 如何提高程序效率