Oracle資料庫SCN號的應用

尛樣兒發表於2013-05-20

        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--


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23135684/viewspace-761578/,如需轉載,請註明出處,否則將追究法律責任。

相關文章