理解SCN与TimeStamp的相互转换


在Oracle10g中,为了增强闪回操作(flashback),现已提供了函数对于SCN和时间戳来进行相互转换。

首先我们需要通过dbms_flashback.get_system_change_number,它可以获取系统当前的SCN值:

SQL> col scn for 9999999999999
SQL> select dbms_flashback.get_system_change_number scn from dual;
SCN
--------------
8908390522972

我们通过scn_to_timestamp函数可以将SCN转换为时间戳:

SQL> select scn_to_timestamp(8908390522972) scn from dual;
SCN
---------------------------------
05-JAN-07 10.56.30.000000000 AM

然后通过timestamp_to_scn则可以将时间戳转换为SCN:

SQL> select timestamp_to_scn
(scn_to_timestamp(8908390522972)) scn from dual;
SCN
--------------
8908390522972

请注意,通过这两个函数,最终Oracle将SCN和时间的关系建立起来,在Oracle10g之前,我们是没有办法通过函数转换得到SCN和时间的对应关系的,但一般可以通过logmnr分析日志来获得。

美中不足的是,这种转换需要依赖于数据库内部的数据记录,对于久远的SCN则不能转换,具体的示例如下:

SQL> select min(FIRST_CHANGE#) scn,
max(FIRST_CHANGE#) scn from v$archived_log;
SCN SCN
------------------ ------------------
8907349093953 8908393582271
SQL> select scn_to_timestamp(8907349093953) scn from dual;
select scn_to_timestamp(8907349093953) scn from dual
*
ERROR at line 1:
ORA-08181: specified number is not a valid system change number
ORA-06512: at "SYS.SCN_TO_TIMESTAMP", line 1
ORA-06512: at line 1
SQL> select scn_to_timestamp(8908393582271) scn from dual;

SCN
-------------------------------------------
05-JAN-07 11.45.50.000000000 AM

在Oracle10g中,为了增强闪回操作(flashback),现已提供了函数对于SCN和时间戳来进行相互转换。

首先我们需要通过dbms_flashback.get_system_change_number,它可以获取系统当前的SCN值:

SQL> col scn for 9999999999999
SQL> select dbms_flashback.get_system_change_number scn from dual;
SCN
--------------
8908390522972

我们通过scn_to_timestamp函数可以将SCN转换为时间戳:

SQL> select scn_to_timestamp(8908390522972) scn from dual;
SCN
---------------------------------
05-JAN-07 10.56.30.000000000 AM

然后通过timestamp_to_scn则可以将时间戳转换为SCN:

SQL> select timestamp_to_scn
(scn_to_timestamp(8908390522972)) scn from dual;
SCN
--------------
8908390522972

请注意,通过这两个函数,最终Oracle将SCN和时间的关系建立起来,在Oracle10g之前,我们是没有办法通过函数转换得到SCN和时间的对应关系的,但一般可以通过logmnr分析日志来获得。

美中不足的是,这种转换需要依赖于数据库内部的数据记录,对于久远的SCN则不能转换,具体的示例如下:

SQL> select min(FIRST_CHANGE#) scn,
max(FIRST_CHANGE#) scn from v$archived_log;
SCN SCN
------------------ ------------------
8907349093953 8908393582271
SQL> select scn_to_timestamp(8907349093953) scn from dual;
select scn_to_timestamp(8907349093953) scn from dual
*
ERROR at line 1:
ORA-08181: specified number is not a valid system change number
ORA-06512: at "SYS.SCN_TO_TIMESTAMP", line 1
ORA-06512: at line 1
SQL> select scn_to_timestamp(8908393582271) scn from dual;

SCN
-------------------------------------------
05-JAN-07 11.45.50.000000000 AM


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3