更新时间:2022-09-01 11:11:00
SQL> select * from v$version ;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
--建立表空间,并且flashback archive保留1年.
create tablespace fratable datafile '/u01/app/oracle11g/oradata/test/fratable01.dbf' size 100M;
create flashback archive fra_1year tablespace fratable retention 1 year;
2.建立测试表
create table nofra (id number, ins_date date, ins_scn number);
create table fra (id number, ins_date date, ins_scn number);
insert into nofra select 1,sysdate,current_scn from v$database ;
insert into fra select 101,sysdate,current_scn from v$database ;
commit;
SQL> select * from nofra ;
ID INS_DATE INS_SCN
---------- ------------------- ----------
1 2012-08-13 11:22:19 16006875
SQL> select * from fra ;
ID INS_DATE INS_SCN
---------- ------------------- ----------
101 2012-08-13 11:22:19 16006889
alter table fra flashback archive fra_1year;
SQL> column x new_value x format a30
SQL> select sysdate x from dual;
X
------------------------------
2012-08-13 11:28:45
--删除了nofra表记录
delete from nofra;
commit;
SQL> select * from nofra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from nofra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from nofra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss')
ID INS_DATE INS_SCN
---------- ------------------- ----------
1 2012-08-13 11:22:19 16006875
--删除了fra表记录
delete from fra;
commit;
SQL> select * from fra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from fra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from fra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss')
ID INS_DATE INS_SCN
---------- ------------------- ----------
101 2012-08-13 11:22:19 16006889
SQL> show parameter undo
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
4.看看执行计划如何!
SQL> select * from nofra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss');
ID INS_DATE INS_SCN
---------- ------------------- ----------
1 2012-08-13 11:22:19 16006875
SQL> @dpc
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID d93pfbuxp0n5c, child number 7
-------------------------------------
select * from nofra as of timestamp to_timestamp('2012-08-13
11:28:45','yyyy-mm-dd:hh24:mi:ss')
Plan hash value: 1697631366
---------------------------------------------------------
| Id | Operation | Name | E-Rows | Cost (%CPU)|
---------------------------------------------------------
| 0 | SELECT STATEMENT | | | 2 (100)|
| 1 | TABLE ACCESS FULL| NOFRA | 82 | 2 (0)|
---------------------------------------------------------
Note
-----
- Warning: basic plan statistics not available. These are only collected when:
* hint 'gather_plan_statistics' is used for the statement or
* parameter 'statistics_level' is set to 'ALL', at session or system level
20 rows selected.
SQL> select * from fra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss');
ID INS_DATE INS_SCN
---------- ------------------- ----------
101 2012-08-13 11:22:19 16006889
SQL> @dpc
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID dqqz4d9kzjmxj, child number 5
-------------------------------------
select * from fra as of timestamp to_timestamp('2012-08-13
11:28:45','yyyy-mm-dd:hh24:mi:ss')
Plan hash value: 3129699971
----------------------------------------------------------------------------------------------------------
| Id | Operation | Name | E-Rows | Cost (%CPU)| OMem | 1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | 13 (100)| | | |
| 1 | VIEW | | 21 | 13 (8)| | | |
| 2 | UNION-ALL | | | | | | |
|* 3 | FILTER | | | | | | |
| 4 | PARTITION RANGE SINGLE| | 1 | 3 (0)| | | |
|* 5 | TABLE ACCESS FULL | SYS_FBA_HIST_104229 | 1 | 3 (0)| | | |
|* 6 | FILTER | | | | | | |
|* 7 | HASH JOIN OUTER | | 20 | 10 (10)| 823K| 823K| 189K (0)|
|* 8 | TABLE ACCESS FULL | FRA | 20 | 6 (0)| | | |
|* 9 | TABLE ACCESS FULL | SYS_FBA_TCRV_104229 | 1 | 3 (0)| | | |
----------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("TIMESTAMP_TO_SCN"(TIMESTAMP' 2012-08-13 11:28:45.000000000')
5 - filter(("ENDSCN""TIMESTAMP_TO_SCN"(TIMESTAMP' 2012-08-13
11:28:45.000000000') AND ("STARTSCN" IS NULL OR "STARTSCN"
2012-08-13 11:28:45.000000000'))))
6 - filter(("STARTSCN"
"STARTSCN" IS NULL))
7 - access("T".ROWID=CHARTOROWID("RID"))
8 - filter("T"."VERSIONS_STARTSCN" IS NULL)
9 - filter((("ENDSCN" IS NULL OR "ENDSCN">16393765) AND ("STARTSCN" IS NULL OR
"STARTSCN"
Note
-----
- dynamic sampling used for this statement (level=2)
- Warning: basic plan statistics not available. These are only collected when:
* hint 'gather_plan_statistics' is used for the statement or
* parameter 'statistics_level' is set to 'ALL', at session or system level
43 rows selected.
select table_name from dba_tables where tablespace_name='FRATABLE';
TABLE_NAME
------------------------------
SYS_FBA_DDL_COLMAP_104229
SYS_FBA_TCRV_104229
以及一个分区表 SYS_FBA_HIST_104229.
SQL> insert into fra select 102,sysdate,current_scn from v$database ;
1 row created.
SQL> insert into nofra select 2,sysdate,current_scn from v$database ;
1 row created.
SQL> commit ;
Commit complete.
SQL> select * from nofra ;
ID INS_DATE INS_SCN
---------- ------------------- ----------
2 2012-08-13 15:34:26 16406037
SQL> select * from fra ;
ID INS_DATE INS_SCN
---------- ------------------- ----------
102 2012-08-13 15:34:18 16406033
alter table nofra add (pad varchar2(32));
alter table fra add (pad varchar2(32));
SQL> select * from nofra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from nofra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from nofra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss')
ID INS_DATE INS_SCN PAD
---------- ------------------- ---------- --------------------------------
1 2012-08-13 11:22:19 16006875
SQL> select * from fra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from fra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from fra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss')
ID INS_DATE INS_SCN PAD
---------- ------------------- ---------- --------------------------------
101 2012-08-13 11:22:19 16006889
SQL> alter table nofra drop column pad;
Table altered.
SQL> alter table fra drop column pad;
Table altered.
SQL> select * from nofra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from nofra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from nofra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss')
select * from nofra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss')
*
ERROR at line 1:
ORA-01466: unable to read data - table definition has changed
SQL> select * from fra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from fra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from fra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss')
ID INS_DATE INS_SCN PAD
---------- ------------------- ---------- --------------------------------
101 2012-08-13 11:22:19 16006889
SQL> column type format a30
SQL> column HISTORICAL_COLUMN_NAME format a30
SQL> column column_name format a30
SQL> select * from SYS_FBA_DDL_COLMAP_104229;
STARTSCN ENDSCN XID O COLUMN_NAME TYPE HISTORICAL_COLUMN_NAME
---------- ---------- ---------------- - ------------------------------ ------------------------------ ------------------------------
16406145 16406275 D_16406275_PAD VARCHAR2(32) PAD
16006837 ID NUMBER ID
16006837 INS_DATE DATE INS_DATE
16006837 INS_SCN NUMBER INS_SCN
SQL> select * from fra as of scn 880;
ID INS_DATE INS_SCN PAD
---------- ------------------- ---------- --------------------------------
101 2012-08-13 11:22:19 16006889
SQL> select * from fra as of timestamp to_timestamp('2012-08-01 11:22:19','yyyy-mm-dd:hh24:mi:ss');
ID INS_DATE INS_SCN PAD
---------- ------------------- ---------- --------------------------------
101 2012-08-13 11:22:19 16006889
SQL> select * from fra as of scn 16006837;
ID INS_DATE INS_SCN
---------- ------------------- ----------
101 2012-08-13 11:22:19 16006889
SQL> select * from fra as of scn 16006836;
select * from fra as of scn 16006836
*
ERROR at line 1:
ORA-01466: unable to read data - table definition has changed
SQL> column y new_value y format a30
SQL> select sysdate y,current_scn from v$database ;
Y CURRENT_SCN
------------------------------ -----------
2012-08-13 16:09:29 16407378
SQL> truncate table nofra;
Table truncated.
SQL> truncate table fra;
Table truncated.
--注:truncate 表fra有点慢!
SQL> select * from nofra as of timestamp to_timestamp('&y','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from nofra as of timestamp to_timestamp('&y','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from nofra as of timestamp to_timestamp('2012-08-13 16:09:29','yyyy-mm-dd:hh24:mi:ss')
select * from nofra as of timestamp to_timestamp('2012-08-13 16:09:29','yyyy-mm-dd:hh24:mi:ss')
*
ERROR at line 1:
ORA-01466: unable to read data - table definition has changed
SQL> select * from fra as of timestamp to_timestamp('&y','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from fra as of timestamp to_timestamp('&y','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from fra as of timestamp to_timestamp('2012-08-13 16:09:29','yyyy-mm-dd:hh24:mi:ss')
ID INS_DATE INS_SCN PAD
---------- ------------------- ---------- --------------------------------
102 2012-08-13 15:34:18 16406033
SQL> select * from fra as of scn 16407378;
ID INS_DATE INS_SCN
---------- ------------------- ----------
102 2012-08-13 15:34:18 16406033
SQL> select * from fra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss');
old 1: select * from fra as of timestamp to_timestamp('&x','yyyy-mm-dd:hh24:mi:ss')
new 1: select * from fra as of timestamp to_timestamp('2012-08-13 11:28:45','yyyy-mm-dd:hh24:mi:ss')
ID INS_DATE INS_SCN PAD
---------- ------------------- ---------- --------------------------------
101 2012-08-13 11:22:19 16006889--时间点X,Y都可以查询.不过truncate fra表确实很慢.
SQL> select * from fra;
no rows selected
SQL> select table_name from dba_tables where tablespace_name='FRATABLE';
TABLE_NAME
------------------------------
SYS_FBA_DDL_COLMAP_104229
SYS_FBA_TCRV_104229
SQL> select object_name,object_id,data_object_id from dba_objects where object_name='FRA';
OBJECT_NAME OBJECT_ID DATA_OBJECT_ID
------------- ---------- --------------
FRA 104229 104244
SQL> drop table fra ;
drop table fra
*
ERROR at line 1:
ORA-55610: Invalid DDL statement on history-tracked table
SQL> drop table nofra ;
Table dropped.
--取消flashback data archive特性.
SQL> alter table fra no flashback archive ;
Table altered.
SQL> drop table fra ;
Table dropped.