Oracle 僞列 ORA_ROWSCN

參考文檔(11gR2):

https://docs.oracle.com/cd/E11882_01/server.112/e41084/pseudocolumns007.htm#SQLRF50953

ORA_ROWSCN Pseudocolumn

ORA_ROWSCN reflects the system change-number (SCN) of the most recent change to a row. This change can be at the level of a block (coarse) or at the level of a row (fine-grained). The latter is provided by row-level dependency tracking. Refer to CREATE TABLE ... NOROWDEPENDENCIES | ROWDEPENDENCIES for more information on row-level dependency tracking. In the absence of row-level dependencies, ORA_ROWSCN reflects block-level dependencies.

Whether at the block level or at the row level, the ORA_ROWSCN should not be considered to be an exact SCN. For example, if a transaction changed row R in a block and committed at SCN 10, it is not always true that the ORA_ROWSCN for the row would return 10. While a value less than 10 would never be returned, any value greater than or equal to 10 could be returned. That is, the ORA_ROWSCN of a row is not always guaranteed to be the exact commit SCN of the transaction that last modified that row. However, with fine-grained ORA_ROWSCN, if two transactions T1 and T2 modified the same row R, one after another, and committed, a query on the ORA_ROWSCN of row R after the commit of T1 will return a value lower than the value returned after the commit of T2. If a block is queried twice, then it is possible for the value of ORA_ROWSCN to change between the queries even though rows have not been updated in the time between the queries. The only guarantee is that the value of ORA_ROWSCN in both queries is greater than the commit SCN of the transaction that last modified that row.

You cannot use the ORA_ROWSCN 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 DELETE statement.

ORA_ROWSCN is not supported for Flashback Query. Instead, use the version query pseudocolumns, which are provided explicitly for Flashback Query. Refer to the SELECT ... flashback_query_clause for information on Flashback Query and "Version Query Pseudocolumns" for additional information on those pseudocolumns.

Restriction on ORA_ROWSCN: This pseudocolumn is not supported for external tables.

使用谷歌翻一下:

ORA ROWSCN反映了系統更改號(SCN)的最近一次更改 。這種變化可以是塊級別的(粗糙的),也可以是行級別的(細粒度的)。後者由行級依賴項跟蹤提供。請參考創建表…有關行級依賴項跟蹤的更多信息,請參閱NOROWDEPENDENCIES。在沒有行級依賴項的情況下,ORA ROWSCN反映了塊級依賴項。

無論是在塊級還是在行級,ORA ROWSCN都不應該被認爲是確切的SCN。例如,如果一個事務更改了塊中的第R行並在scn10時提交,那麼該行的ORA ROWSCN並不總是返回10。雖然不會返回小於10的值,但是可以返回任何大於或等於10的值。也就是說,不能總是保證最後修改該行的事務的ORA ROWSCN是正確的commit SCN。但是,對於細粒度的ORA ROWSCN,如果兩個事務T1和T2一個接一個地修改相同的行R並提交,則在T1提交後對行R的ORA ROWSCN的查詢將返回一個低於T2提交後返回的值。如果一個塊被查詢兩次,那麼ORA ROWSCN的值可能在查詢之間發生變化,即使在查詢之間的時間內沒有更新行。惟一可以保證的是,這兩個查詢中的ORA ROWSCN的值大於最後修改該行的事務的提交SCN。

不能在視圖的查詢中使用ORA ROWSCN僞列。但是,您可以在創建視圖時使用它來引用基礎表。您還可以在UPDATE或DELETE語句的WHERE子句中使用這個僞列。
也不能再外部表中使用該僞劣。

-- 測試,以下測試在RDBMS 19.0.0.0上測試

-- 查看hr.employees 表中的ora_rowscn ,都是一樣的,有兩種可能,一種可能是這些行是在同一個事務中(比如同一個事務插入,然後提交)。或者是NOROWDEPENDENCIES,是基於block的。在同一個block上的行,具有相同的ora_rowscn.

13:33:11 SYS@test>SELECT ORA_ROWSCN, last_name   FROM hr.employees;

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 King
   2010440 Kochhar
   2010440 De Haan
   2010440 Hunold
   2010440 Ernst
   2010440 Austin
   2010440 Pataballa
   2010440 Lorentz
   2010440 Greenberg
   2010440 Faviet
   2010440 Chen

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Sciarra
   2010440 Urman
   2010440 Popp
   2010440 Raphaely
   2010440 Khoo
   2010440 Baida
   2010440 Tobias
   2010440 Himuro
   2010440 Colmenares
   2010440 Weiss
   2010440 Fripp

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Kaufling
   2010440 Vollman
   2010440 Mourgos
   2010440 Nayer
   2010440 Mikkilineni
   2010440 Landry
   2010440 Markle
   2010440 Bissot
   2010440 Atkinson
   2010440 Marlow
   2010440 Olson

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Mallin
   2010440 Rogers
   2010440 Gee
   2010440 Philtanker
   2010440 Ladwig
   2010440 Stiles
   2010440 Seo
   2010440 Patel
   2010440 Rajs
   2010440 Davies
   2010440 Matos

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Vargas
   2010440 Russell
   2010440 Partners
   2010440 Errazuriz
   2010440 Cambrault
   2010440 Zlotkey
   2010440 Tucker
   2010440 Bernstein
   2010440 Hall
   2010440 Olsen
   2010440 Cambrault

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Tuvault
   2010440 King
   2010440 Sully
   2010440 McEwen
   2010440 Smith
   2010440 Doran
   2010440 Sewall
   2010440 Vishney
   2010440 Greene
   2010440 Marvins
   2010440 Lee

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Ande
   2010440 Banda
   2010440 Ozer
   2010440 Bloom
   2010440 Fox
   2010440 Smith
   2010440 Bates
   2010440 Kumar
   2010440 Abel
   2010440 Hutton
   2010440 Taylor

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Livingston
   2010440 Grant
   2010440 Johnson
   2010440 Taylor
   2010440 Fleaur
   2010440 Sullivan
   2010440 Geoni
   2010440 Sarchand
   2010440 Bull
   2010440 Dellinger
   2010440 Cabrio

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Chung
   2010440 Dilly
   2010440 Gates
   2010440 Perkins
   2010440 Bell
   2010440 Everett
   2010440 McCain
   2010440 Jones
   2010440 Walsh
   2010440 Feeney
   2010440 OConnell

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2010440 Grant
   2010440 Whalen
   2010440 Hartstein
   2010440 Fay
   2010440 Mavris
   2010440 Baer
   2010440 Higgins
   2010440 Gietz

已選擇 107 行。

13:33:23 SYS@test>

-- 在13:35分修改一行,並提交。查詢此時的ORA_ROWSCN是 2540378。

13:35:30 SYS@test>update hr.employees set employee_id=207 where employee_id=206;

已更新 1 行。

13:35:32 SYS@test>commit;

提交完成。

13:35:36 SYS@test>
13:36:56 SYS@test>SELECT ORA_ROWSCN, last_name   FROM hr.employees   WHERE employee_id =207;

ORA_ROWSCN LAST_NAME
---------- -------------------------
   2540378 Gietz

13:36:57 SYS@test>

-- 根據scn 查看時間,是10月22日 下午1:35:33 ,和上面的commit時間13:35:32基本吻合(符合文檔中說的大於等於)。當然也可以針對這個時間進行閃回,進行數據恢復。

13:36:57 SYS@test>select scn_to_timestamp(2540378) from dual;

SCN_TO_TIMESTAMP(2540378)
---------------------------------------------------------------------------
22-10月-19 01.35.33.000000000 下午

13:37:51 SYS@test>

轉載官方的一個flashback吧

SELECT SCN_TO_TIMESTAMP(ORA_ROWSCN) FROM employees
   WHERE employee_id = 188;
SCN_TO_TIMESTAMP(ORA_ROWSCN)
---------------------------------------------------------------------------
28-AUG-03 01.58.01.000000000 PM

FLASHBACK TABLE employees TO TIMESTAMP
   TO_TIMESTAMP('28-AUG-03 01.00.00.000000000 PM');

SELECT salary FROM employees WHERE employee_id = 188;  
    SALARY
----------
      3800

關於基於block和row的scn,可以參考MOS:

How ORA_ROWSCN Works with Rowdependencies and Norowdependecies (文檔 ID 805424.1)

關於 NOROWDEPENDENCIES | ROWDEPENDENCIES

https://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_7002.htm#CJAEEGDA

NOROWDEPENDENCIES | ROWDEPENDENCIES

This clause lets you specify whether table will use row-level dependency tracking. With this feature, each row in the table has a system change number (SCN) that represents a time greater than or equal to the commit time of the last transaction that modified the row. You cannot change this setting after table is created.

ROWDEPENDENCIES Specify ROWDEPENDENCIES if you want to enable row-level dependency tracking. This setting is useful primarily to allow for parallel propagation in replication environments. It increases the size of each row by 6 bytes.

Restriction on the ROWDEPENDENCIES Clause Oracle does not support table compression for tables that use row-level dependency tracking. If you specify both the ROWDEPENDENCIES clause and the table_compression clause, then the table_compression clause is ignored. To remove the ROWDEPENDENCIES attribute, you must redefine the table using the DBMS_REDEFINITION package or recreate the table.

NOROWDEPENDENCIES Specify NOROWDEPENDENCIES if you do not want table to use the row-level dependency tracking feature. This is the default.

END

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章