Oracle数据库SCN号的应用


        Oracle SCN号是Oracle数据库的重要机制,Oracle SCN号跟时间有密切关系;也影响着读一致性,数据库实例启动,数据库恢复等重要操作,这篇文章结合之前的文章讨论SCN的部分应用。

一.Oracle SCN是Oracle数据库的内部时钟,请参考文章《Oracle SCN》http://space.itpub.net/23135684/viewspace-627343中的第一点。


二.Oracle SCN在启动过程中的作用,参考文章《Oracle SCN》http://space.itpub.net/23135684/viewspace-627343中的第二点。


三.ORA_ROWSCN伪列:

       下面是ORA_ROWSCN伪列使用的例子:
SQL> show user
USER 为 "TEST"
SQL> create table scntest(id number primary key,
  2  name varchar2(20));

表已创建。

SQL> insert into scntest values(1,'aaa');

已创建 1 行。

SQL> set linesize 200
SQL> select ora_rowscn,to_char(scn_to_timestamp(ora_rowscn),'yyyy-mm-dd hh24:mi:ss') opertime,id ,name from scntest;

ORA_ROWSCN OPERTIME                                       ID NAME
---------- -------------------------------------- ---------- ----------------------------------------
   2080149 2013-05-20 23:47:28                             1 aaa

SQL> insert into scntest values(2,'bbb');

已创建 1 行。

SQL> commit;

提交完成。

SQL> select ora_rowscn,to_char(scn_to_timestamp(ora_rowscn),'yyyy-mm-dd hh24:mi:ss') opertime,id ,name from scntest;

ORA_ROWSCN OPERTIME                                       ID NAME
---------- -------------------------------------- ---------- ----------------------------------------
   2080295 2013-05-20 23:48:16                             1 aaa
   2080295 2013-05-20 23:48:16                             2 bbb

SQL> insert into scntest values(3,'ccc');

已创建 1 行。

SQL> commit;

提交完成。

SQL> select ora_rowscn,to_char(scn_to_timestamp(ora_rowscn),'yyyy-mm-dd hh24:mi:ss') opertime,id ,name from scntest;

ORA_ROWSCN OPERTIME                                       ID NAME
---------- -------------------------------------- ---------- ----------------------------------------
   2080308 2013-05-20 23:48:43                             1 aaa
   2080308 2013-05-20 23:48:43                             2 bbb
   2080308 2013-05-20 23:48:43                             3 ccc

SQL> delete from scntest where id=1;

已删除 1 行。

SQL> commit;

提交完成。

SQL> select ora_rowscn,to_char(scn_to_timestamp(ora_rowscn),'yyyy-mm-dd hh24:mi:ss') opertime,id ,name from scntest;

ORA_ROWSCN OPERTIME                                       ID NAME
---------- -------------------------------------- ---------- ----------------------------------------
   2080316 2013-05-20 23:49:07                             2 bbb
   2080316 2013-05-20 23:49:07                             3 ccc

SQL> update scntest set name='abc' where id=2;

已更新 1 行。

SQL> commit;

提交完成。

SQL> select ora_rowscn,to_char(scn_to_timestamp(ora_rowscn),'yyyy-mm-dd hh24:mi:ss') opertime,id ,name from scntest;

ORA_ROWSCN OPERTIME                                       ID NAME
---------- -------------------------------------- ---------- ----------------------------------------
   2080331 2013-05-20 23:49:49                             2 abc
   2080331 2013-05-20 23:49:49                             3 ccc

        通过ORA_ROWSCN伪列可以查询出表的最后DML操作SCN号,进而能够得到操作时间。

四.按照指定SCN号导出一致性的数据。

1).exp工具的flashback_scn参数:

        下面例子导出指定SCN号的一致性数据。

C:\Users\LIUBINGLIN>sqlplus test/test123123

SQL*Plus: Release 11.2.0.3.0 Production on 星期二 5月 21 00:05:29 2013

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select * from scntest;

        ID NAME
---------- ----------------------------------------
         2 abc
         3 ccc

SQL> connect / as sysdba
已连接。
SQL> select current_scn from v$database;

CURRENT_SCN
-----------
    2081576

SQL> connect test/test123123
已连接。
SQL> insert into scntest values(4,'ddd');

已创建 1 行。

SQL> commit;

提交完成。

SQL> connect / as sysdba
已连接。
SQL> select * from test.scntest;

        ID NAME
---------- ----------------------------------------
         2 abc
         3 ccc
         4 ddd

SQL> grant execute on dbms_flashback to test;

授权成功。


C:\Users\LIUBINGLIN>exp test/test123123 file=c:\test.dmp tables=('scntest') log=c:\test.log flashback_scn=2081576

Export: Release 11.2.0.3.0 - Production on 星期二 5月 21 00:07:21 2013

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.


连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
已导出 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集
服务器使用 AL32UTF8 字符集 (可能的字符集转换)

即将导出指定的表通过常规路径...
. . 正在导出表                         SCNTEST导出了           2 行
成功终止导出, 没有出现警告。

C:\Users\LIUBINGLIN>sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on 星期二 5月 21 00:07:33 2013

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> drop table test.scntest purge;

表已删除。

SQL> exit
从 Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断开

C:\Users\LIUBINGLIN>imp test/test123123 file=c:\test.dmp tables=('scntest') log=c:\test.log

Import: Release 11.2.0.3.0 - Production on 星期二 5月 21 00:08:07 2013

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.


连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

经由常规路径由 EXPORT:V11.02.00 创建的导出文件
已经完成 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集中的导入
导入服务器使用 AL32UTF8 字符集 (可能的字符集转换)
. 正在将 TEST 的对象导入到 TEST
. 正在将 TEST 的对象导入到 TEST
. . 正在导入表                       "SCNTEST"导入了           2 行
成功终止导入, 没有出现警告。

C:\Users\LIUBINGLIN>sqlplus test/test123123

SQL*Plus: Release 11.2.0.3.0 Production on 星期二 5月 21 00:08:17 2013

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select * from scntest;

        ID NAME
---------- ----------------------------------------
         2 abc
         3 ccc

注意:如果用sys用户导出将收到如下报错:
EXP-00103: FLASHBACK_TIME 参数无效
EXP-00008: 遇到 ORACLE 错误 8185
ORA-08185: 用户 SYS 不支持闪回
ORA-06512: 在 "SYS.DBMS_FLASHBACK", line 12
ORA-06512: 在 line 1
EXP-00000: 导出终止失败


2).expdp工具的flashback_scn参数:

        expdp工具同样有flashback_scn参数。

C:\Users\LIUBINGLIN>expdp test/test123123 directory=dump_dir1 dumpfile=test2.dmp flashback_scn=2092288

Export: Release 11.2.0.3.0 - Production on 星期二 5月 21 00:21:36 2013

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
启动 "TEST"."SYS_EXPORT_SCHEMA_01":  test/******** directory=dump_dir1 dumpfile=test2.dmp flashback_scn=2092288
正在使用 BLOCKS 方法进行估计...
处理对象类型 SCHEMA_EXPORT/TABLE/TABLE_DATA
使用 BLOCKS 方法的总估计: 384 KB
处理对象类型 SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
处理对象类型 SCHEMA_EXPORT/TABLE/TABLE
处理对象类型 SCHEMA_EXPORT/TABLE/COMMENT
处理对象类型 SCHEMA_EXPORT/TABLE/INDEX/INDEX
处理对象类型 SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
处理对象类型 SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
处理对象类型 SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
处理对象类型 SCHEMA_EXPORT/TABLE/POST_TABLE_ACTION
处理对象类型 SCHEMA_EXPORT/TABLE/MATERIALIZED_VIEW_LOG
. . 导出了 "TEST"."MLOG$_TEST_P"                       7.031 KB       1 行
. . 导出了 "TEST"."SCNTEST"                            5.429 KB       3 行
. . 导出了 "TEST"."TEST_P":"P1"                        5.812 KB       1 行
. . 导出了 "TEST"."TEST_P":"P2"                        5.812 KB       1 行
. . 导出了 "TEST"."TEST_P":"P3"                        5.812 KB       1 行
. . 导出了 "TEST"."TEST_P":"P4"                        5.812 KB       1 行
. . 导出了 "TEST"."TEST_P":"P5"                            0 KB       0 行
已成功加载/卸载了主表 "TEST"."SYS_EXPORT_SCHEMA_01"
******************************************************************************
TEST.SYS_EXPORT_SCHEMA_01 的转储文件集为:
  C:\TEST2.DMP
作业 "TEST"."SYS_EXPORT_SCHEMA_01" 已于 00:22:10 成功完成



--end--


请使用浏览器的分享功能分享到微信等