標籤

4GL (1) 人才發展 (10) 人物 (3) 太陽能 (4) 心理 (3) 心靈 (10) 文學 (31) 生活常識 (14) 光學 (1) 名句 (10) 即時通訊軟體 (2) 奇狐 (2) 爬蟲 (1) 音樂 (2) 產業 (5) 郭語錄 (3) 無聊 (3) 統計 (4) 新聞 (1) 經濟學 (1) 經營管理 (42) 解析度 (1) 遊戲 (5) 電學 (1) 網管 (10) 廣告 (1) 數學 (1) 機率 (1) 雜趣 (1) 證券 (4) 證券期貨 (1) ABAP (15) AD (1) agentflow (4) AJAX (1) Android (1) AnyChart (1) Apache (14) BASIS (4) BDL (1) C# (1) Church (1) CIE (1) CO (38) Converter (1) cron (1) CSS (23) DMS (1) DVD (1) Eclipse (1) English (1) excel (5) Exchange (4) Failover (1) Fedora (1) FI (57) File Transfer (1) Firefox (3) FM (2) fourjs (1) Genero (1) gladiatus (1) google (1) Google Maps API (2) grep (1) Grub (1) HR (2) html (23) HTS (8) IE (1) IE 8 (1) IIS (1) IMAP (3) Internet Explorer (1) java (4) JavaScript (22) jQuery (6) JSON (1) K3b (1) ldd (1) LED (3) Linux (117) Linux Mint (4) Load Balance (1) Microsoft (2) MIS (2) MM (51) MSSQL (1) MySQL (27) Network (1) NFS (1) Office (1) OpenSSL (1) Oracle (126) Outlook (3) PDF (6) Perl (60) PHP (33) PL/SQL (1) PL/SQL Developer (1) PM (3) Postfix (2) postfwd (1) PostgreSQL (1) PP (50) python (5) QM (1) Red Hat (4) Reporting Service (28) ruby (11) SAP (234) scp (1) SD (16) sed (1) Selenium (3) Selenium-WebDriver (5) shell (5) SQL (4) SQL server (8) sqlplus (1) SQuirreL SQL Client (1) SSH (2) SWOT (3) Symantec (2) T-SQL (7) Tera Term (2) tip (1) tiptop (24) Tomcat (6) Trouble Shooting (1) Tuning (5) Ubuntu (37) ufw (1) utf-8 (1) VIM (11) Virtual Machine (2) VirtualBox (1) vnc (3) Web Service (2) wget (1) Windows (19) Windows (1) WM (6) Xvfb (2) youtube (1) yum (2)

2011年9月20日 星期二

Oracle flashback

http://blog.roodo.com/mywork/archives/1684525.html

Oracle Flashback

Oracle Flashback Database特性允許通過SQL語句Flashback Database語句,讓數據庫前滾到當前的前一個時間點或者SCN,而不需要做時間點的恢復。flashback可以迅速將數據庫回到誤操作或人為錯誤 的前一個時間點,可減少解決問題所花費的時間。

OS:windows 2000
Oracle:10g
跟flashback有關的參數:

開起flashback語法
SQL>alter database flashback on;


查看放flash空間的地方和容量
SQL> show parameter recover;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string D:\oracle\product\10.2.0/flash_recovery_area
db_recovery_file_dest_size big integer 2G
note:(修改大小值,容量大小跟flahsback的時間有關係)
SQL> alter system set db_recovery_file_dest_size=3G;
System altered

SQL> show parameter db_recovery_file_dest_size;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest_size big integer 3G


建立測試帳號
SQL> create user flashtest identified by flashtest;
已建立使用者.

SQL> grant connect,resource to flashtest;
順利授權.

SQL> grant execute on dbms_flashback to flashtest;
順利授權.

SQL> grant dba to flashtest;
順利授權.

SQL> conn flashtest/flashtest;
已連線.



flashback drop

建立測試table
SQL> create table testdrop7(id number);
Table created

建立幾筆資料
SQL> insert into testdrop7 values (1);
1 row inserted

SQL> insert into testdrop7 values (2);
1 row inserted

SQL> commit;
Commit complete

SQL> drop table testdrop7;
Table dropped

查尋回收區是否有剛被刪除的資料
SQL> select object_name,original_name,operation,type,droptime from recyclebin;

OBJECT_NAME ORIGINAL_NAME OPERATION TYPE DROPTIME
------------------------------ ---------------------- ------------------------- ------------------- ---------------------
BIN$4ltRoED1RaCHZglxktu9kA==$0 TESTDROP DROP TABLE 2006-05-29:17:07:48
BIN$V9qmi5hUQDKcTBMbfJkk5A==$0 TESTDROP2 DROP TABLE 2006-05-29:17:43:37
BIN$0SEStB60QhC4+hULXLkNOA==$0 TESTDROP3 DROP TABLE 2006-05-29:17:51:58
BIN$rezTtmaDQ9qn2jJEy28jdg==$0 TESTDROP4 DROP TABLE 2006-05-29:17:59:05
BIN$WXp0b1+7Qfea9YaMLw8aHA==$0 TESTDROP6 DROP TABLE 2006-05-29:18:23:16
BIN$A2ADdSwnRX2Ib5CXFe1iuQ==$0 TESTDROP5 DROP TABLE 2006-05-29:18:23:18
BIN$7lPg3xr3RFyemQ5cmCq6KA==$0 TESTDROP7 DROP TABLE 2006-05-30:12:08:34
7 rows selected


救回剛才被誤殺的table
SQL> flashback table testdrop7 to before drop ;
Done
note:
如有相同的table名字的話,可改用下面語法
flashback table "BIN$A2ADdSwnRX2Ib5CXFe1iuQ==$0" to before drop;

查尋是否真的救回來了
SQL> select * from testdrop7;
ID
----------
1
2


Flashback Table

以時間回覆:
先開起table的flashback的功能
SQL> alter table testdrop7 enable row movement;
Table altered
note:如沒有做這行的話,會出現錯誤訊習
ORA-08189: 未啟用資料列移動, 因此無法倒溯表格

現在testdrop7裡有5筆資料
SQL> select * from testdrop7;

ID
----------
1
2
3
4
5

增加二筆資料
SQL> insert into testdrop7 values (6);
1 row inserted

SQL> insert into testdrop7 values (7);
1 row inserted

SQL> commit;
Commit complete


查尋一下現在時間
SQL> select to_char(sysdate,'YYYY-MM-DD hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YYYY-MM-DDHH2
------------------------------
2006-05-30 14:21:43

再增一筆資料準備測試還原
SQL> insert into testdrop7 values (8);
1 row inserted

SQL> commit;
Commit complete

現在的時間
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YYYY-MM-DDHH2
------------------------------
2006-05-30 14:22:53

還原整個table到未加到數字8之前的狀態
SQL> flashback table testdrop7 to timestamp to_timestamp('2006-05-30 14:21:43','yyyy-mm-dd hh24:mi:ss');
Done

確時還原了
SQL> select * from testdrop7;

ID
----------
1
2
3
4
5
6
7

7 rows selected


以scn值回覆

查尋現在testdrop7裡的資料
SQL> select * from testdrop7;

ID
----------
1
2
3
4
5
6
7

7 rows selected

假裝誤刪一筆資料,並且commit了
SQL> delete from testdrop7 where id =7;
1 row deleted

SQL> commit;
Commit complete

現在testdrop7裡確時只有6筆了
SQL> select * from testdrop7;

ID
----------
1
2
3
4
5
6

6 rows selected

有關scn值就是記錄在這個table裡
SQL> desc flashback_transaction_query;
Name Type Nullable Default Comments
---------------- -------------- -------- ------- -----------------------------------------
XID RAW(8) Y Transaction identifier
START_SCN NUMBER Y Transaction start SCN
START_TIMESTAMP DATE Y Transaction start timestamp
COMMIT_SCN NUMBER Y Transaction commit SCN
COMMIT_TIMESTAMP DATE Y Transaction commit timestamp
LOGON_USER VARCHAR2(30) Y Logon user for transaction
UNDO_CHANGE# NUMBER Y 1-based undo change number
OPERATION VARCHAR2(32) Y forward operation for this undo
TABLE_NAME VARCHAR2(256) Y table name to which this undo applies
TABLE_OWNER VARCHAR2(32) Y owner of table to which this undo applies
ROW_ID VARCHAR2(19) Y rowid to which this undo applies
UNDO_SQL VARCHAR2(4000) Y SQL corresponding to this undo

查尋最後的scn值為何
SQL> select start_scn,
to_char(start_timestamp,'yyyy-mm-dd hh24:mi:ss') as s_time,
commit_scn,
to_char(commit_timestamp,'yyyy-mm-dd hh24:mi:ss') as c_time,
table_name,
undo_sql
from flashback_transaction_query
where table_name='TESTDROP7'
order by commit_timestamp;

START_SCN S_TIME COMMIT_SCN C_TIME TABLE_NAME UNDO_SQL
--------------- ------------------------- ------------------ --------------------- ------------- -----------------------------------
680553 2006-05-30 12:32:05 680731 2006-05-30 12:32:08 TESTDROP7 delete from "FLASHTEST"."TESTDROP7" where ROWID = 'AAAM1tAAEAAAAHNAAC';
685412 2006-05-30 14:04:47 685415 2006-05-30 14:04:53 TESTDROP7 delete from "FLASHTEST"."TESTDROP7" where ROWID = 'AAAM1tAAEAAAAHNAAE';
685412 2006-05-30 14:04:47 685415 2006-05-30 14:04:53 TESTDROP7 delete from "FLASHTEST"."TESTDROP7" where ROWID = 'AAAM1tAAEAAAAHNAAD';
686429 2006-05-30 14:20:51 686432 2006-05-30 14:20:57 TESTDROP7 delete from "FLASHTEST"."TESTDROP7" where ROWID = 'AAAM1tAAEAAAAHNAAG';
686429 2006-05-30 14:20:51 686432 2006-05-30 14:20:57 TESTDROP7 delete from "FLASHTEST"."TESTDROP7" where ROWID = 'AAAM1tAAEAAAAHNAAF';
686506 2006-05-30 14:22:25 686507 2006-05-30 14:22:25 TESTDROP7 delete from "FLASHTEST"."TESTDROP7" where ROWID = 'AAAM1tAAEAAAAHNAAH';
686574 2006-05-30 14:24:00 686576 2006-05-30 14:24:00 TESTDROP7 delete from "FLASHTEST"."TESTDROP7" where ROWID = 'AAAM1tAAAAAAAAJGg+';
686574 2006-05-30 14:24:00 686576 2006-05-30 14:24:00 TESTDROP7 insert into "FLASHTEST"."TESTDROP7"("ID") values ('8');
686574 2006-05-30 14:24:00 686576 2006-05-30 14:24:00 TESTDROP7 insert into "FLASHTEST"."TESTDROP7"("ID") values ('7');
686963 2006-05-30 14:31:58 686965 2006-05-30 14:32:01 TESTDROP7 insert into "FLASHTEST"."TESTDROP7"("ID") values ('7');
10 rows selected

現在table還沒有數值7
SQL> select * from testdrop7;

ID
----------
1
2
3
4
5
6

6 rows selected

輸入最後的scn值,進行還原
SQL> flashback table testdrop7 to scn 686963;
Done

SQL> select * from testdrop7;

ID
----------
1
2
3
4
5
6
7

7 rows selected


Flash Version Query
Oracle Flashback Version Query特性,利用保存的回滾信息,可以看到特定的表在時間段內的任何修改,如電影的回放一樣,可以了解表在該期間的任何變化

建立測試用table
SQL> create table testdrop10 (id number,text varchar(10));

Table created

新增測試資料
SQL> insert into testdrop10 values (1,'a');
1 row inserted

SQL> commit;
Commit complete

查尋一下現在時間跟scn值,以利後面查尋方便
SQL> select current_scn,to_char(systimestamp,'YYYY-MM-DD HH24:MI:SS') from v$database;

CURRENT_SCN TO_CHAR(SYSTIMESTAMP,'YYYY-MM-
----------- ------------------------------
691931 2006-05-30 15:51:08

做一些dml的動作
SQL> update testdrop10 set text='b' where id=1;
1 row updated

SQL> commit;
Commit complete

SQL> delete testdrop10 where id=1;
1 row deleted

SQL> commit;
Commit complete

SQL> insert into testdrop10 values (1,'c');
1 row inserted

SQL> commit;
Commit complete


SQL> SELECT versions_startscn,to_char(versions_starttime,'hh24:mi:ss') as s_time,
2 versions_endscn,to_char(versions_endtime,'hh24:mi:ss') as e_time,
3 versions_xid, versions_operation,
4 text
5 FROM testdrop10
6 VERSIONS BETWEEN TIMESTAMP
7 TO_TIMESTAMP('2006-05-30 15:51:08', 'YYYY-MM-DD HH24:MI:SS')
8 AND TO_TIMESTAMP('2006-05-30 15:53:30', 'YYYY-MM-DD HH24:MI:SS')
8 WHERE ID=1
9 ORDER BY versions_starttime;

VERSIONS_STARTSCN S_TIME VERSIONS_ENDSCN E_TIME VERSIONS_XID VERSIONS_OPERATION TEXT
----------------- -------- --------------- -------- ---------------- ---------------------------- ----------
691964 15:51:58 691975 15:52:13 060016007A010000 U b
691975 15:52:13 0A00250079010000 D b
691992 15:52:40 0400240078010000 I c
691964 15:51:58 a


note:如時間亂打的話會出現下面錯誤
SQL> SELECT versions_startscn,to_char(versions_starttime,'hh24:mi:ss') as s_time,
2 versions_endscn,to_char(versions_endtime,'hh24:mi:ss') as e_time,
3 versions_xid, versions_operation,
4 text
5 FROM testdrop10
6 VERSIONS BETWEEN TIMESTAMP TO_TIMESTAMP('2006-05-30 15:51:08', 'YYYY-MM-DD HH24:MI:SS')
7 AND TO_TIMESTAMP('2006-05-30 15:54:00', 'YYYY-MM-DD HH24:MI:SS')
8 WHERE ID=1
9 ORDER BY versions_starttime;

SELECT versions_startscn,to_char(versions_starttime,'hh24:mi:ss') as s_time,
versions_endscn,to_char(versions_endtime,'hh24:mi:ss') as e_time,
versions_xid, versions_operation,
text
FROM testdrop10
VERSIONS BETWEEN TIMESTAMP TO_TIMESTAMP('2006-05-30 15:51:08', 'YYYY-MM-DD HH24:MI:SS')
AND TO_TIMESTAMP('2006-05-30 15:54:00', 'YYYY-MM-DD HH24:MI:SS')
WHERE ID=1
ORDER BY versions_starttime

ORA-08186: 指定的時戳無效


Flashback Transaction Query

在上線的環境中是不可能去追 蹤每個事務,想要獲得已提交事務的XID,可由Flashback Version Query或直接查尋flashback_transaction_query這個table。

SQL> select * from testdrop10;


ID TEXT
---------- ----------
1 c

SQL> delete from testdrop10 where id=1;
1 row deleted

在commit之前先查一下xid值
SQL> select xid from v$transaction;
XID
----------------
0500180092010000

SQL> commit;
Commit complete

SQL> select undo_sql from flashback_transaction_query where xid='0500180092010000';
insert into "FLASHTEST"."TESTDROP10"("ID","TEXT") values ('1','c');

直接查尋flashback_transaction_query
SQL> desc flashback_transaction_query;
Name Type Nullable Default Comments
---------------- -------------- -------- ------- -----------------------------------------
XID RAW(8) Y Transaction identifier
START_SCN NUMBER Y Transaction start SCN
START_TIMESTAMP DATE Y Transaction start timestamp
COMMIT_SCN NUMBER Y Transaction commit SCN
COMMIT_TIMESTAMP DATE Y Transaction commit timestamp
LOGON_USER VARCHAR2(30) Y Logon user for transaction
UNDO_CHANGE# NUMBER Y 1-based undo change number
OPERATION VARCHAR2(32) Y forward operation for this undo
TABLE_NAME VARCHAR2(256) Y table name to which this undo applies
TABLE_OWNER VARCHAR2(32) Y owner of table to which this undo applies
ROW_ID VARCHAR2(19) Y rowid to which this undo applies
UNDO_SQL VARCHAR2(4000) Y SQL corresponding to this undo

SQL> select undo_sql from flashback_transaction_query where table_name='SYSDROP1';
delete from "SYS"."SYSDROP1" where ROWID = 'AAAM1sAABAAAO16AAB';
delete from "SYS"."SYSDROP1" where ROWID = 'AAAM1sAABAAAO16AAA';

沒有留言:

張貼留言