Oracle Flashback-based Flashback Query

Source: Internet
Author: User

In Oracle 10 Gb, the Flash back family is divided into the following members:
Flashback Database
Flashback Drop
Flashback Table
Flashback Query (Flashback Query, Flashback Version Query, and Flashback Transaction Query)
The following describes Flashback Query.
 
Flashback is a feature provided by ORACLE since 9i. In 9i, we use oracle to query data that has been operated in a table for a certain period of time, this feature is also called Flashback Query.
I. Flashback Query
As mentioned in the preface, Flashback Query uses the multi-version read consistency feature to read the record data before the operation from the UNDO tablespace!
What is multi-version read consistency?
Oracle adopts a very good design. It uses undo data to ensure that writing is not blocked. In short, when writing data for different transactions, the pre-image of the data is written to the undo tablespace. If other transactions query the table data at the same time, you can use the pre-image of the data in the undo tablespace to construct the required complete record set, you do not need to wait for the written transaction to commit or roll back.
 
Flashback query can be used to construct a query record set in multiple ways. The record set range can be time-based or scn-based. It can even be used to query the front image of records in different transactions in the undo tablespace at the same time. The usage is very similar to the standard query. To use the flashback query to query undo data, the simplest method is to keep up with the as of timestamp (based on time) after the name of the standard query statement) or as of scn (based on scn. As of timestamp | the scn syntax is supported only after 9iR2.
 
This function has nothing to do with flashback on/off and recyclebin on/off.
1. Example of As of timestamp:
SQL> select * from test1;
ID NAME
--------------------
3764577 1
SQL> select to_char (sysdate, 'yy-mm-dd hh24: mi: ss') time from dual;
TIME
-----------------
12-01-13 16:59:29
 
Simulate user misoperations and delete data
SQL> alter table test1 enable row movement; -- to change the table to allow row movement.
SQL> delete from test1;
SQL> commit;
SQL> select * from test1;
No rows selected
 
View the status before deletion:
Assume that the data has been deleted for about 5 minutes:
SQL> select * from test1 as of timestamp sysdate-5/1440;
ID NAME
--------------------
3764577 1
 
Or:
SQL> select * from test1 as of timestamp to_timestamp ('12-01-13 16:59:29 ', 'yy-MM-DD hh24: mi: ss ');
ID NAME
--------------------
3764577 1
Use Flashback to restore the previous data:
SQL> insert into test1 select * from test1 as of timestamp to_timestamp ('12-01-13 16:59:29 ', 'yy-MM-DD hh24: mi: ss ');
1 row created.
SQL> commit;
SQL> select * from test1;
ID NAME
--------------------
3764577 1
As shown in the preceding example, the as of timestamp is indeed very easy to use. However, in some cases, we recommend that you use the as of scn method to execute the flashback query, for example, to restore multiple tables with primary and foreign key constraints, if the as of timestamp method is used, data selection or insertion may fail due to inconsistent time points, the scn method ensures the consistency of the constraints of the record.
 
2. As of scn example
View SCN:
SELECT dbms_flashback.get_system_change_number FROM dual;
SELECT CURRENT_SCN from v $ DATABASE;
SQL> SELECT CURRENT_SCN FROM V $ DATABASE;
CURRENT_SCN
-----------
3765554
 
Delete data:
SQL> delete from test1;
1 row deleted.
SQL> commit;
View the status before deletion:
SQL> select * from test1 as of scn 3765554;
ID NAME
--------------------
3764577 1
Use Flashback to restore the previous data:
SQL> insert into test1 select * from test1 as of scn 3765554;
1 row created.
SQL> commit;
Commit complete.
SQL> select * from test1;
ID NAME
--------------------
3764577 1
 
In fact, Oracle uses scn internally. Even if you specify as of timestamp, oracle converts it to scn. There is a table between the system time mark and scn, SMON_SCN_TIME in SYS
Every five minutes, the system generates a matching system time mark with scn and stores it in sys. smon_scn_time table, which records the matching records of the last 1440 system time tags and scn. Because this table only maintains the most recent 1440 records, therefore, if you use the as of timestamp method, you can only use flashback data in the last five days (assuming that the system is continuously running without interruption or shutdown or restarting ).
Note that the system time mark matches the scn every 5 minutes. For example, scn: 3764577,3765348 Match 12-01-12 13:52:00 and 12-01-12 13:57:00 respectively, then, when you query the time between 12-01-12 13:52:00 or 12-01-12 13:57:00 through the as of timestamp, oracle will match it with scn: 3764577 to the undo tablespace, that is, during this time, no matter what time point you specify, the query will return data at 12-01-12 13:52:00.
View the ing between SCN and timestamp:
Select scn, to_char (time_dp, 'yyyy-mm-dd hh24: mi: ss') from sys. smon_scn_time;
 
Flashback version Query
Compared with the Flashback Query, you can only view the object status at a certain point. The Flashback Version Query introduced by Oracle 10 Gb shows how the record changes over a certain period of time. Based on this history, the DBA can quickly determine when an error occurs in the data and then restore it to the previous state.
First look at a pseudo column ORA_ROWSCN. the so-called pseudo column is a false data column that does not exist. Although it is not specified when you create a table, some internal fields added to Oracle for maintenance are, these fields can be used as common files.
The most familiar pseudo-column is ROWID, which is equivalent to a pointer pointing to the position of the record on the disk. ORA_ROWSCN is newly added to Oracle 10 Gb. It is regarded as the SCN of the last modified record. The Flashback Version Query uses this pseudo column to track the change history of a record.
For example:
SQL> select * from test1;
ID NAME
--------------------
3764577 1
SQL> insert into test1 values (dbms_flashback.get_system_change_number, '2 ');
1 row created.
SQL> select * from test1;
ID NAME
--------------------
3764577 1
3765736 2
SQL> commit;
SQL> select ora_rowscn, id, name from test1;
ORA_ROWSCN ID NAME
------------------------------
3765611 3764577 1
3765611 3765736 2
Get more historical information
SQL> Select versions_xid, versions_startscn, versions_endscn,
2 DECODE (versions_operation, 'I', 'insert', 'U', 'update', 'D', 'delete', 'original') "Operation ",
3 id, name from test1 versions between scn minvalue and maxvalue;
VERSIONS_XID VERSIONS_STARTSCN VERSIONS_ENDSCN Operatio ID NAME
----------------------------------------------------------------------------
0400210080030000 3765611 Insert 3764577 1
0400210080030000 3765611 Delete 3764577 1
03002A0049040000 3765520 3765611 Insert 3764577 1
03002A0049040000 3765520 Delete 3765348
3765520 Original 3765348
Or
SQL> select xid, commit_scn, commit_timestamp, operation, undo_ SQL
From flashback_transaction_query q where q. xid in (select versions_xid from B versions between scn 3764577 and 3765348 );
Below we will talk about the pseudo columns. The Flashback Version Query Technology actually has many pseudo columns, but ORA_ROWSCN is the most important. It records the SCN of the last modification. Note that the modification is submitted. If not submitted, this pseudo column will not change.
ORA_ROWSCN is at the data block level by default, that is, all records in a data block are one ORA_ROWSCN, and any record in the data block is modified, the ORA_ROWSCN of all records in this database block will change at the same time. The query result of the preceding example is used to prove the problem.
However, we can use the keyword rowdependencies when creating a table to change this default behavior. With this keyword, each record has its own ORA_ROWSCN.
Example:
SQL> create table test1 (id int, seq int) rowdependencies;
Table created.
SQL> insert into test1 values (dbms_flashback.get_system_change_number, 1 );
1 row created.
SQL> insert into test1 values (dbms_flashback.get_system_change_number, 2 );
1 row created.
SQL> commit;
Commit complete.
SQL> insert into test1 values (dbms_flashback.get_system_change_number, 3 );
1 row created.
SQL> commit;
Commit complete.
SQL> select ora_rowscn, id, seq from test1;
ORA_ROWSCN ID SEQ
------------------------------
3769182 3769179 1
3769182 3769180 2
3769186 3769184 3
 
It indicates the SCN of the last modification. If it is not submitted, it will not change.
 
Flashback Transaction Query
Flashback Transaction Query is also implemented using UNDO information. This function allows you to view all the changes in the execution of a transaction. You need to access the flashback_transaction_query view. The XID column in this view represents the transaction ID, which can be used to identify all the data changes that occur in a specific transaction.
Example:
SQL> truncate table test1;
Table truncated.
SQL> select current_scn from v $ database;
CURRENT_SCN
-----------
3769319
SQL> insert into test1 values (dbms_flashback.get_system_change_number, 1 );
1 row created.
SQL> commit;
Commit complete.
SQL> select current_scn from v $ database;
CURRENT_SCN
-----------
3769333
 
View the view. Each transaction corresponds to the same XID.
SQL> Select xid, operation, commit_scn, undo_ SQL from flashback_transaction_query where xid in (
2 Select versions_xid from test1 versions between scn minvalue and maxvalue );
 
Xid operation COMMIT_SCN
----------------------------------------------------------
UNDO_ SQL
Bytes -----------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
02000E009C040000 INSERT 3769330
Delete from "YORKER". "TEST1" where ROWID = 'aaanloaaeaaaal2aaa ';
02000E009C040000 BEGIN 3769330
Or
Select xid, commit_scn, commit_timestamp, operation, undo_ SQL
From flashback_transaction_query q where q. xid in
(Select versions_xid from test1 versions between scn 3769319 and 3769333 );

Author kkdelta

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.