一. 官網對該偽列的說明
From:11gR2
http://download.Oracle.com/docs/cd/E11882_01/server.112/e17118/pseudocolumns007.htm#SQLRF50953
Foreach row, ORA_ROWSCN returns the conservative upper bound system change number(SCN) of the most recent change to the row in the current session. Thispseudocolumn is useful for determining approximately when a row was lastupdated.
Itis not absolutely precise, because Oracle tracks SCNs by transaction committedfor the block in which the row resides. You can obtain a more fine-grainedapproximation of the SCN by creating your tables with row-level dependencytracking. Refer to CREATE TABLE ... NOROWDEPENDENCIES| ROWDEPENDENCIES for more information on row-level dependency tracking.
ROWDEPENDENCIESSpecify ROWDEPENDENCIES if you want to enable row-level dependency tracking.This setting is useful primarily to allow for parallel propagation inreplication environments. It increases the size of each row by 6 bytes.
NOROWDEPENDENCIESSpecify NOROWDEPENDENCIES if you do not want table to use the row-leveldependency tracking feature. This is the default.
也就是說,使用表的預設建立參數,即norowdependencies時,此時的ora_rawscn 取自data block header的SCN,那麼這時候,對於同一個block裡的row而言,他們的ora_rowscn 是一樣的。
而在建立table時指定為rowdependencies時,那麼會為每行row 儲存一個ora_rowscn. 這樣對於同一個block裡的row,會有多個ora_rowscn 值。通過dump block,可以發現每個row 會多出一個dscn的資訊,該資訊就是用來儲存ora_rowscn的。
如:
tab 0, row 1, @0×1f88
tl: 12 fb: –H-FL– lb: 0×2 cc: 1
dscn 0×0000.00000000
tab 0, row 1, @0×1f88
tl: 12 fb: –H-FL– lb: 0×0 cc: 1
dscn 0×0005.105a6cc1
這是同一個row的dump 資訊,第一次dscn 為0. 此時資訊是從itl的Scn/Fsc中獲得的,當itl發生cleanout時會把Scn/Fsc刷到dscn,就是上面的第二段資訊。具體這塊後面會實驗證明。
關於block dump 和 itl 說明,參考:
Oracle datafile block 格式 說明 http://www.bkjia.com/Linux/2011-08/40286.htm
Orace ITL(InterestedTransaction List) 說明
You cannot use this pseudocolumn in a query to a view.However, you can use it to refer to the underlying table when creating a view.You can also use this pseudocolumn in the WHERE clause of an UPDATE or DELETEstatement.
--不能在視圖使用ora_rowscn偽列
ORA_ROWSCN is not supported for Flashback Query.Instead, use the version query pseudocolumns, which are provided explicitly forFlashback Query. Refer to the SELECT ... flashback_query_clausefor information on Flashback Query and "VersionQuery Pseudocolumns" for additional information on thosepseudocolumns.
--ora_rowscn 不支援Flashbackquery。
Restriction on ORA_ROWSCN: This pseudocolumn is notsupported for external tables.
--ora_rowscn 不支援外部表格
Example:
Thefirst statement below uses the ORA_ROWSCN pseudocolumn to get the system changenumber of the last operation on the employees table.
SELECT ORA_ROWSCN, last_name
FROM employees
WHERE employee_id = 188;
--使用ORA_ROWSCN擷取該row 最後一次更新時的SCN
The second statement uses the pseudocolumnwith the SCN_TO_TIMESTAMP function to determine the timestamp of the operation:
SELECTSCN_TO_TIMESTAMP(ORA_ROWSCN), last_name
FROM employees
WHERE employee_id = 188;
--使用SCN_TO_TIMESTAMP 和 ORA_ROWSCN,擷取最後一次修改row的時間
二. ORA_ROWSCN 說明
ORA_ROWSCN偽列是Oracle10g引入的,可以查詢表中記錄最後變更的SCN。這個新的偽列在某些環境下會非常有用,比如執行樂觀鎖定,或者增量資料幫浦的時候。但是,預設情況下,每行記錄的ORA_ROWSCN是基於Block的,除非在建表的時候執行開啟行級跟蹤(createtable … rowdependencies)。
2.1 樂觀鎖和ORA_ROWSCN
需要select ... for update做悲觀鎖定的時候,通過使用ORA_ROWSCN可以改成樂觀鎖定。一開始select資料的時候將ORA_ROWSCN查出來,修改後如果要寫回資料庫之前再比對下最新的ORA_ROWSCN就可以知道這期間資料是否有發生變化。
2.2 增量資料幫浦和ORA_ROWSCN
每次抽取後記錄最大的ORA_ROWSCN,下次抽取再基於上一次的SCN來獲得最近修改過的資料即可。在10g之前,很多系統要實現增量資料幫浦,要麼通過解析日誌,要麼加觸發器,要麼就在表上加一個時間截欄位。
ORA_ROWSCN其實就是第三種方式,只是這個欄位由Oracle來維護,這樣可以避免一些應用繞過時間截去更新其他欄位帶來的問題。不過,如果系統中使用了邏輯備庫或者streams等邏輯複製的方案,而資料幫浦又是基於邏輯備庫的話,ORA_ROWSCN就可能對抽取後的資料分析有影響了,因為通過這個得到的時間是邏輯備庫上記錄變更的時間,而不是源庫的時間了。
當然,如果純粹只是做資料幫浦,而不需要使用這個時間來做分析,還是問題不大的,但還是要考慮一旦邏輯備庫出現故障需要重做的,則這個增量抽取要怎麼來處理的問題。