当前位置:操作系统 > Unix/Linux >>

关于查询current_scn导致SCN增进及SCN的计算方法示例

关于查询current_scn导致SCN增进及SCN的计算方法示例
 
第一、关于current_scn:
Oracle10g在v$database视图中引入了current_scn,这个SCN来自底层表,代表当前的SCN,current_scn的查询会直接导致SCN的增进,而其他方式并不会。也就是说在这里的current_scn就像是一个Sequence一样,查询会导致增进。这也很好理解,v$database只能通过增进当前的SCN才能保证获得的SCN是Current的。可是如果不查询呢?这个值肯定是不会增长的。
而使用dbms_flashback.get_system_change_number方式查询并不会导致SCN的增进。
实验1:使用dbms_flashback.get_system_change_number方式查询:SCN不因查询而变化
BYS@bys1>select dbms_flashback.get_system_change_number current_scn from dual;
CURRENT_SCN
-----------
    1780068
BYS@bys1>select dbms_flashback.get_system_change_number current_scn from dual;
CURRENT_SCN
-----------
    1780068
BYS@bys1>select dbms_flashback.get_system_change_number current_scn from dual;
CURRENT_SCN
-----------
    1780069
BYS@bys1>select dbms_flashback.get_system_change_number current_scn from dual;
CURRENT_SCN
-----------
    1780069
BYS@bys1>select dbms_flashback.get_system_change_number current_scn from dual;
CURRENT_SCN
-----------
    1780069
BYS@bys1>select dbms_flashback.get_system_change_number current_scn from dual;
CURRENT_SCN
-----------
    1780069
实验2:使用current_scn查询,每次查询到的SCN是不一样的
BYS@bys1>select current_scn from v$database;
CURRENT_SCN
-----------
    1780089
BYS@bys1>select current_scn from v$database;
CURRENT_SCN
-----------
    1780090
BYS@bys1>select current_scn from v$database;
CURRENT_SCN
-----------
    1780091

 

 
第二、关于scn计算方说明:
scn计算方法:SCN=(SCN_WRP * 4294967296) + SCN_BAS
因为 kscnbas占4个byte,所以2的32(4*8)次方 = 4294967296,所以scn = (SCN_WRP * 4294967296) + SCN_BAS
而又因为kscnwrp占2个byte,所以scn最大值为ff.ffff=2的48(6*8)次方-1 = 281474976710655
BYS@bys1>conn / as sysdba   ---需要使用SYS用户
Connected.
SYS@bys1>select * from (select time_mp,time_dp,scn_wrp,scn_bas from smon_scn_time order by time_mp desc)  where rownum<2;

   TIME_MP TIME_DP      SCN_WRP    SCN_BAS
---------- --------- ---------- ----------
1378949107 12-SEP-13          0    1781024
SYS@bys1>select (0*4294967296)+1781024 as scn from dual;
       SCN
----------
   1781024
SYS@bys1>select dbms_flashback.get_system_change_number current_scn from dual;
CURRENT_SCN
-----------
    1781410 
   ----大致能对上哈哈。
 
第三、SCN与时间戳转换-SMON_SCN_TIME表
SMON_SCN_TIME表基础知识
作用:由smon收集scn和time映射关系,用于flashback/查询scn和time对应关系等操作
保留条数:官方文档给出说明instance number N * 12 times per hour * 24 hours * 5 days = 1440N rows,因为每次的时间间隔不是非常准确的5分钟,所以在具体的条数在实际生产环境中有一定的出入
采集和删除:smon进程每5分钟采集一次插入到SMON_SCN_TIME表中,同时将删除历史数据(超过5天前数据),采用下面语句delete from smon_scn_time where thread=0 and time_mp = (select min(time_mp) from smon_scn_time where thread=0),如果有时候index出了问题,导致该语句执行很慢
查询scn对应time:如果scn超过SMON_SCN_TIME表范围,将提示错误;或者查询time对应的scn,如果超过范围也同样报错。
停止/开启smon进程收集scn信息
stop
alter system set events'12500 trace name context forever, level 10';

start
alter system set events'12500 trace name context off';
SCN与timestamp的互相转化
SQL> select  scn_to_timestamp(1900758)  from dual;
SCN_TO_TIMESTAMP(1900758)
--------------------------------------------------------------------------------
29-JUN-13 06.09.23.000000000 PM

SQL> select  timestamp_to_scn(scn_to_timestamp(1900758))  from dual;
TIMESTAMP_TO_SCN(SCN_TO_TIMEST
------------------------------
                       1900757
SQL> select  timestamp_to_scn(to_timestamp('2013-06-29 18:10:01','yyyy-mm-dd hh24:mi:ss'))  from dual;
TIMESTAMP_TO_SCN(TO_TIMESTAMP(
------------------------------
                       1900887

 


CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,