首页 » ORACLE 9i-23c » oracle ORA_ROWSCN 行记录的更新时间

oracle ORA_ROWSCN 行记录的更新时间

在这介绍两个oracle 10G开始提供的一个伪列ORA_ROWSCN,它又分为两种模式一种是基于block,这是默认的模式,还有一种是基于row上,这种模式只能在建里表时指定ROWDEPENDENCIES,不可以通过后期的alter table ,同时会给数据库带来性能负载
每个Block在头部是记录了该block最近事务的SCN的,所以默认情况下,只需要从block头部直接获取这个值就可以了,不需要其他任何的开销,Oracle就能做到这一点。但是这明显第一种模式是scn是不准确的,因为不可能每个事务都能修改整个 块的数据。

在10g之前,很多系统要实现增量数据抽取,要么通过解析日志,要么加触发器,要么就在表上加一个时间截字段。ORA_ROWSCN其实就是第三种方式,只是这个字段由Oracle来维护,这样可以避免一些应用绕过时间截去更新其他字段带来的问题。

下面做一个实验来证明,首先在默认状态下修改同一个块上的其中一条数据,然后再启用行级跟踪,修改同一块上的一条数据,观察ora_rowscn变化

SQL> conn anbob/anbob
Connected.
SQL> create table testscn(
2 id number(5),
3 name varchar2(10),
4 sex number(1),
5 addr varchar2(100)
6 );

Table created.

SQL> insert into testscn values(1,’anbob’,’1′,’beijing’);

1 row created.

SQL> insert into testscn values(2,’sesebook’,’1′,’beijing’);

1 row created.

SQL> insert into testscn values(3,’weejar’,’1′,’beijing’);

1 row created.

SQL> commit;

Commit complete.

SQL> set linesize 150
SQL> select ora_rowscn,id,name,sex,addr from testscn;

ORA_ROWSCN ID NAME SEX
———- ———- ——————– ———-
ADDR
——————————————————————————————————————————————————
46007034 1 anbob 1
beijing

46007034 2 sesebook 1
beijing

46007034 3 weejar 1
beijing

SQL> col addr for a50
SQL> run
1* select ora_rowscn,id,name,sex,addr from testscn

ORA_ROWSCN ID NAME SEX ADDR
———- ———- ——————– ———- ————————————————–
46007034 1 anbob 1 beijing
46007034 2 sesebook 1 beijing
46007034 3 weejar 1 beijing

SQL> select ora_rowscn,id,name,sex,addr,dbms_rowid.ROWID_BLOCK_NUMBER(rowid) blockid from testscn;

ORA_ROWSCN ID NAME SEX ADDR BLOCKID
———- ———- ——————– ———- ————————————————– ———-
46007034 1 anbob 1 beijing 1429
46007034 2 sesebook 1 beijing 1429
46007034 3 weejar 1 beijing 1429
——–可以看出记录是在同一个block上

SQL> select scn_to_timestamp(ora_rowscn) stime,id,name,sex,addr from testscn;

STIME ID NAME SEX ADDR
—————————————- ———- ——————– ———- ————————————————–
16-5月 -11 01.37.54.000000000 下午 1 anbob 1 beijing
16-5月 -11 01.37.54.000000000 下午 2 sesebook 1 beijing
16-5月 -11 01.37.54.000000000 下午 3 weejar 1 beijing

SQL> update testscn set sex=0 where id=2;

1 row updated.

SQL> commit;

Commit complete.

SQL> select scn_to_timestamp(ora_rowscn) stime,id,name,sex,addr from testscn;

STIME ID NAME SEX ADDR
—————————————- ———- ——————– ———- ————————————————–
16-5月 -11 01.47.21.000000000 下午 1 anbob 1 beijing
16-5月 -11 01.47.21.000000000 下午 2 sesebook 0 beijing
16-5月 -11 01.47.21.000000000 下午 3 weejar 1 beijing
–因为是同一个块上,所以这个块上的所有数据的scn都更新了

第二种模式
SQL> create table testscn2 ROWDEPENDENCIES as select * from testscn;

Table created.

SQL> select scn_to_timestamp(ora_rowscn) stime,id,name,sex,addr from testscn2;

STIME ID NAME SEX ADDR
—————————————- ———- ——————– ———- ————————————————–
16-5月 -11 02.15.42.000000000 下午 1 anbob 1 beijing
16-5月 -11 02.15.42.000000000 下午 2 sesebook 0 beijing
16-5月 -11 02.15.42.000000000 下午 3 weejar 1 beijing

SQL> select ora_rowscn,id,name,sex,addr,dbms_rowid.ROWID_BLOCK_NUMBER(rowid) blockid from testscn;

ORA_ROWSCN ID NAME SEX ADDR BLOCKID
———- ———- ——————– ———- ————————————————– ———-
46007328 1 anbob 1 beijing 1429
46007328 2 sesebook 0 beijing 1429
46007328 3 weejar 1 beijing 1429

SQL> update testscn2 set sex=0 where id=3;

1 row updated.

SQL> commit;

Commit complete.

SQL> select scn_to_timestamp(ora_rowscn) stime,id,name,sex,addr from testscn2;

STIME ID NAME SEX ADDR
—————————————- ———- ——————– ———- ————————————————–
16-5月 -11 02.15.42.000000000 下午 1 anbob 1 beijing
16-5月 -11 02.15.42.000000000 下午 2 sesebook 0 beijing
16-5月 -11 02.16.39.000000000 下午 3 weejar 0 beijing

SQL> 
打赏

目前这篇文章有3条评论(Rss)评论关闭。

  1. Berniece Arnesen | #1
    2011-12-21 at 06:52

    comment1, elimite cream 5, %-), hcl propranolol, 986, tamiflu cost, 054, doxycycline lyme disease, %-]], order elimite, 71935, cheap celexa buy, >:OO, tetracycline chlamydia, kbmvq, avodart penis, ekptl, levaquin, hjqwx, allopurinol and viagra, (, erection viagra, 8-[[[, floxin i.v., :]]], cheap lexapro, lmns, cipro benadryl, PP, online retin a pharmacy, 639913, can nexium cause insomnia, wdpwau, zithromax side effects, 501, order online retin a, D, cause menopause zoloft, stdj, tamiflu and bird flu, jwekqw, acomplia.com, :-OO, 2b allergic bactrim reaction, >:PPP, ergomar, 8-DD, discount tamiflu, ipwx, stromectol, 248377, tetracycline dosage, %-PPP, viagra mexico, 30916, buy metformin without prescription, :O, vermox 100mg, vspk, cheap tetracycline, >:-PPP, side effects of mobic medicine, tcx, buy acomplia, 8PP, propecia cheap usa, 38335, buy levitra, 955, free neurontin, ore,

  2. 万嗔 | #2
    2011-05-20 at 09:35

    很好,学习了!

    • Jayne | #3
      2011-06-23 at 08:43

      There’s a terrific amount of konlwgede in this article!