Using Flashback Queries(使用闪回操作)

SCN的概念:

http://www.itpub.net/thread-1601493-1-1.html

查询当前的系统的时间戳:

1 select SysTimeStamp from DUAL;

NOTE--
Oracle uses undo to roll back transactions and support flashback
queries. Oracle uses redo (captured in the online redo log files) to
apply transactions during database recoveries.
NOTE--
To use some features of flashback queries, you must have the
EXECUTE privilege on the DBMS_FLASHBACK package. Most
users will not need privileges on this package.

1 GRANT ALL ON dbms_flashback TO scott;

1、基于时间戳的FLASHBACK(Time-Based Flashback Example)

1 INSERT INTO d VALUES('A');
2 INSERT INTO d VALUES('B');
3 INSERT INTO d VALUES('C');
4 SELECT * FROM d
5
6 DELETE FROM d

 

         delete后:             

查询两分钟的时间点的数据:

1 SELECT * FROM D AS OF TIMESTAMP(SYSDATE - 2 / 1140)

 

(英文解释:)
    The simplest method involves saving the data to a separate table. There are 1440 minutes in a
day, so “SysDate – 5/1440” will direct the database to a point in time five minutes ago. Note that
the query may return no rows if more than five minutes have passed since the commit.

--保存数据

1 CREATE TABLE OLD_DATA AS SELECT * FROM d AS OF TIMESTAMP(SYSDATE - 2/1140);

--插入之前的表中

1 INSERT INTO D SELECT * FROM OLD_DATA

2、基于SCN(SYSTEM CHANGE NUMBER)的FLASHBACK

     When you perform time-based flashbacks, you are really doing SCN-based flashbacks; you’re just
relying on Oracle to find an SCN near the time you specified. If you know the exact SCN, you
can perform a flashback with a great degree of precision.
To begin an SCN-based flashback, you must first know the SCN of your transaction. To get the latest change number, issue a commit and then use the as of scn clause of the select command.
You can find the current SCN by executing the GET_SYSTEM_CHANGE_NUMBER function of the DBMS_FLASHBACK package prior to executing your transaction. NOTE
Prior to executing the following example, you must have been granted
the EXECUTE privilege on the DBMS_FLASHBACK package.
注意:

DDL operations that alter the structure of a table invalidate the
old undo data for the table, and flashback capabilities are limited
to the time since the DDL was executed. Space-related changes
(such as changing pctfree) do not invalidate the old undo data. See
Chapter 30 for details on the flashback table and flashback database
commands.

查询系统当前的SCN(System Change Number):

1 SELECT  DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER FROM DUAL

使用SCN实现TIMESTAMP类似的功能:闪回:

1 SELECT * FROM D AS OF SCN(3040628)

--查询当前系统中最小的有效的SCN:

1 select min(SCN) min_scn from sys.smon_scn_time;

伪列ORA_ROWSCN的使用:代表每行数据自己的SCN号:

1 SELECT IDS,ORA_ROWSCN FROM D


--SCN 和 TIMESTAMP

The ORA_ROWSCN pseudocolumn value is not absolutely precise,
because Oracle tracks SCNs by transaction committed for the block in
which the row resides. A new transaction in the block may update the
ORA_ROWSCN values for all rows in the block.
What time does that SCN map to? You can use the SCN_TO_TIMESTAMP function to display
the date at which the change was made:

1 select SCN_TO_TIMESTAMP(555853) from DUAL;
2
3 select SCN_TO_TIMESTAMP(ORA_ROWSCN)
4 from D;
--查询系统当前的SCN号的两种方式
1 SELECT dbms_flashback.get_system_change_number,current_scn FROM v$database;
2
3 SELECT T.*,TO_CHAR(SCN_TO_TIMESTAMP(ORA_ROWSCN),'YYYY-MM-DD HH24:MM:SS') FROM D T;
4
5 SELECT * FROM d AS OF TIMESTAMP(SYSDATE-1);
6
7 SELECT * FROM d AS OF SCN(2383800);

为什么FLASHBACK QUERY会失败:
What If the Flashback Query Fails?
     If there is not enough space in the undo tablespace to maintain all the data needed for the
flashback query, the query will fail. Even if the DBA created a large undo tablespace, it is possible
for a series of large transactions to use all the space available. A portion of each failed query will
be written to the database’s alert log.
From the perspective of a user attempting to recover old data, you should attempt to recover
data that is as correct and timely as possible. Often, you may need to execute multiple flashback
queries to determine how far back you can successfully query the data, and then save the oldest
data you can access and the data closest to the point at which the problem occurred. Once the
oldest data is gone from the undo tablespace, you can no longer use flashback queries to retrieve it.
    If it is not possible to flash back to data that is old enough for your needs, you will need to
perform some sort of database recovery—either flashing back the entire database or recovering
specific tables and tablespaces via traditional database recovery methods. If this problem happens
routinely, the undo retention time should be increased, the space allotted for the undo tablespace
should be increased, and the application usage should be examined to determine why questionable
transactions are occurring on a persistent basis.
--注意
You can use the FLASHBACK_TRANSACTION_QUERY data dictionary view to track changes
made by a particular transaction. For a given transaction, FLASHBACK_TRANSACTION_QUERY
shows the name of the user who executed it, the operation performed, the table to which the
transaction was applied, the start and end SCNs and timestamps, and the SQL needed to undo
the transaction.
常见问题:
伪列:ORA_ROWSCN: Returns the conservative upper-bound SCN of the most recent change to
the row, showing approximately when the row was last updated (row-level
dependencies should be created for more fine-grained auditing

SCN:
ORA-08181

If the value supplied to the SCN_TO_TIMESTAMP function is either not a System Change Number or is considered by the database to be an SCN that is too old then an error message similar to the one in the following example will be returned:

DETERMINING THE OLDEST AVAILABLE SCN

In order to avoid an ORA-08181 error, it is possible to determine the oldest available SCN that the database can produce a timestamp from. The example below demonstates retreiving that number, and the effects of attempting to use an SCN just one before it.

SQL> col min_scn for 99999999999999999
SQL> select min(SCN) min_scn from sys.smon_scn_time;

MIN_SCN
------------------
3330756740492

SQL> select SCN_TO_TIMESTAMP(3330756740492) from dual;

SCN_TO_TIMESTAMP(3330756740492)
---------------------------------------------------------------------------
09-OCT-10 08.40.33.000000000

SQL> select SCN_TO_TIMESTAMP(3330756740491) from dual;
select SCN_TO_TIMESTAMP(3330756740491) from dual
*
ERROR at line 1:
ORA-08181: specified number is not a valid system change number
ORA-06512: at "SYS.SCN_TO_TIMESTAMP", line

 

posted on 2012-01-11 16:25  Coldest Winter  阅读(588)  评论(0编辑  收藏  举报