天天看點

物化視圖是否可以exp導出_用EXP、IMP遷移包含物化視圖日志的資料

2014-1-15日修改下面文章:物化視圖日志的遷移由于不能更改建立物化日志的使用者名,是以導緻不是同名的使用者入庫會失效,這是oracle的bug,如果非要不同名使用者下使用,可以使用以下語句建立新的内部觸發器:

exec dbms_snapshot_utl.sync_up_log(‘ring’,’T_USERINFO’);

select * from dba_internal_triggers where table_name=’T_USERINFO’and  Owner_name=’RING’;

這個問題給我最大感觸:遷移資料庫的時候,要保持新庫和老庫都一樣,包含全局名,sid,使用者名等,這樣就不會出現亂七八糟的問題,這也是比較傻瓜的遷移方式。

今天在測試環境中,原來正常快速重新整理的物化視圖不能同步資料了,最後發現是因為在同一個庫做exp、imp操作後使得物化視圖日志失效了,不記錄基表的資料變化了。

在解決問題的過程中發現了其他一些問題,這裡一并記錄。

源庫:[email protected]> select * from v$version;

BANNER

----------------------------------------------------------------

Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production

PL/SQL Release 9.2.0.1.0 - Production

CORE 9.2.0.1.0 Production

TNS for 32-bit Windows: Version 9.2.0.1.0 - Production

NLSRTL Version 9.2.0.1.0 - Production

--在源庫建立使用者sys@TEST1> create user a identified by a default tablespace suk;

使用者已建立。

sys@TEST1> create user b identified by b default tablespace suk;

使用者已建立。

sys@TEST1> grant connect,resource,create materialized view to a;

授權成功。

sys@TEST1> grant connect,resource,create materialized view to b;

授權成功。

[email protected]> conna/[email protected]

已連接配接。[email protected]> create table t(a int primary key);

表已建立。

[email protected]> create materialized view log on t;

實體化視圖日志已建立。

[email protected]> insert into t values(1);

已建立 1 行。

[email protected]> commit;

送出完成。

[email protected]> select * from mlog$_t;

A SNAPTIME$$ D O CHANGE_VECTOR$$

---------- -------------- - - ------------------------

1 01-1月 -00 I N FE

其他庫:

SQL> select * from v$version;

BANNER

----------------------------------------------------------------

Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production

PL/SQL Release 9.2.0.4.0 - Production

CORE 9.2.0.3.0 Production

TNS for Linux: Version 9.2.0.4.0 - Production

NLSRTL Version 9.2.0.4.0 - Production

--在其他庫建立使用者

SQL> create user suk identified by suk default tablespace suk;

User created.

SQL> create user a identified by a default tablespace suk;

User created.

SQL> grant connect,resource,create materialized view to suk;

Grant succeeded.

SQL> grant connect,resource,create materialized view to a;

Grant succeeded.

1、導入同一個庫中的另一個使用者

E:oracleora92bin>impb/[email protected]=e:a.dmp fromuser=a touser=b log=e:b.log

Import: Release 9.2.0.1.0 - Production on 星期二 10月 30 13:46:10 2007

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.

連接配接到: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production

With the Partitioning, OLAP and Oracle Data Mining options

JServer Release 9.2.0.1.0 - Production

經由正常路徑導出由EXPORT:V09.02.00建立的檔案

警告: 此對象由 A 導出, 而不是目前使用者

已經完成ZHS16GBK字元集和AL16UTF16 NCHAR 字元集中的導入

. . 正在導入表 "MLOG$_T" 1行被導入

. . 正在導入表 "T" 1行被導入

IMP-00015: 由于對象已存在,下列語句失敗:

"CREATE SNAPSHOT LOG ON "A"."T" WITH PRIMARY KEY EXCLUDING NEW VALUES USING "

"("MLOG$_T", (5, 'TEST1.COM', 98, '2007-10-30:13:43:30', '2007-10-30:13:43:3"

"0', '2007-10-30:13:43:30', '4000-01-01:00:00:00', '4000-01-01:00:00:00', 1,"

" "A", '2007-10-30:13:43:30', 2, 0, ("RUPD$_T")))"

成功終止導入,但出現警告。

導入失敗,從log看原因很清楚:雖然是要導入到其他使用者下,但建立物化視圖日志時仍然嘗試在原來的使用者下建立,結果當然是失敗的。

這個還不是最主要的,最嚴重的是這樣導緻了原來的物化視圖日志失效。

[email protected]> col CHANGE_VECTOR$$ format [email protected]> select * from mlog$_t;

A SNAPTIME$$ D O CHANGE_VECTOR$$

---------- -------------- - - ------------------------------

1 01-1月 -00 I N FE

[email protected]> insert into t values(2);

已建立 1 行。

[email protected]> select * from mlog$_t;

A SNAPTIME$$ D O CHANGE_VECTOR$$

---------- -------------- - - ------------------------------

1 01-1月 -00 I N FE

從上面看到,物化視圖日志已經不能記錄基表的資料變化,所有基于這個表的快速重新整理的物化視圖将不能快速重新整理(重新整理時不會報錯,但是資料修改的資料同步不過去)。

此時的解決方式是,重建原來使用者的物化視圖日志,然後對引用到這些表的物化視圖做一次完全重新整理。

2、導入其他庫中的其他使用者

[[email protected] dmp]$ imp suk/suk file=a.dmp fromuser=a touser=suk log=imp_suk.log

Import: Release 9.2.0.4.0 - Production on 星期二 10月 30 13:16:04 2007

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.

Connected to: Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production

With the Partitioning option

JServer Release 9.2.0.4.0 - Production

Export file created by EXPORT:V09.02.00 via conventional path

Warning: the objects were exported by A, not by you

import done in ZHS16GBK character set and AL16UTF16 NCHAR character set

. . importing table "MLOG$_T" 1 rows imported

. . importing table "T" 1 rows imported

IMP-00017: following statement failed with ORACLE error 942:

"BEGIN SYS.DBMS_SNAPSHOT_UTL.SYNC_UP_LOG('A','T'); END;"

IMP-00003: ORACLE error 942 encountered

ORA-00942: table or view does not exist

ORA-06512: at "SYS.DBMS_SNAPSHOT_UTL", line 1589

ORA-06512: at line 1

"CREATE SNAPSHOT LOG ON "A"."T" WITH PRIMARY KEY EXCLUDING NEW VALUES USING "

"("MLOG$_T", (5, 'TEST1.COM', 98, '2007-10-30:13:43:30', '2007-10-30:13:43:3"

"0', '2007-10-30:13:43:30', '4000-01-01:00:00:00', '4000-01-01:00:00:00', 1,"

" "A", '2007-10-30:13:43:30', 2, 0, ("RUPD$_T")))"

IMP-00003: ORACLE error 1435 encountered

ORA-01435: user does not exist

IMP-00000: Import terminated unsuccessfully

導入失敗,原因和上面所述一樣。

SQL> conn suk/suk

Connected.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID

------------------------------ ------- ----------

MLOG$_T TABLE

RUPD$_T TABLE

T TABLE

oracle在導入的時候是先建立物化視圖日志表,然後再建立物化視圖日志的。在這裡,物化視圖日志表雖然建立成功了,但是物化視圖日志不能建立成功,此時這系列MOG$_、RUPD$_沒有任何作用。

這種情況下,如果需要在T上記錄物化視圖日志,則在删除對應的MLOG$和RUPD$表後重建物化視圖日志。

如果有必要,你還需要在其他庫上重建指向這些表的物化視圖。

3、導入其他庫中的相同使用者

[[email protected] dmp]$ imp a/a file=a.dmp fromuser=a touser=a log=imp_a.log

Import: Release 9.2.0.4.0 - Production on 星期二 10月 30 13:53:14 2007

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.

Connected to: Oracle9i Enterprise Edition Release 9.2.0.4.0 - 64bit Production

With the Partitioning option

JServer Release 9.2.0.4.0 - Production

Export file created by EXPORT:V09.02.00 via conventional path

import done in ZHS16GBK character set and AL16UTF16 NCHAR character set

. . importing table "MLOG$_T" 1 rows imported

. . importing table "T" 1 rows imported

Import terminated successfully without warnings.

從導入資訊看,完全沒有錯誤,且物化視圖日志仍然有效。

SQL> select * from mlog$_t;

A SNAPTIME$$ D O CHANGE_VECTOR$$

---------- ---------- - - ------------------------------

1 01-1月 -00 I N FE

SQL> insert into t values(10);

1 row created.

SQL> select * from mlog$_t;

A SNAPTIME$$ D O CHANGE_VECTOR$$

---------- ---------- - - ------------------------------

1 01-1月 -00 I N FE

10 01-1月 -00 I N FE

從這個測試看,物化視圖日志也可以反應基表資料的變化,那麼基于這個表的物化視圖也可以快速重新整理。

對于這個問題,Utilities的import章節有如下解釋:

Importing a Snapshot into a Different Schema

Snapshots, snapshot logs, and related items are exported with the schema name explicitly given in the DDL statements; therefore, snapshots and their related items cannot be imported into a different schema.

If you attempt to use FROMUSER and TOUSER to import snapshot data, an error will be written to the Import log file and the items will not be imported.

但是為何在把含有物化視圖日志的表導入到同一個庫的另一個使用者後,會使物化視圖日志失效的問題這裡仍然沒有解釋。

結論:

1、含有物化視圖日志的表導入到同一個庫的另一個使用者後,原來使用者下物化視圖日志會失效;新使用者的物化視圖日志不能成功導入。

2、含有物化視圖日志的表導入到另一個庫的另一個使用者後,原來使用者下的物化視圖日志不受影響;物化視圖日志不能成功導入到新資料庫中。

3、含有物化視圖日志的表導入到另一個庫的同一個使用者後,原來使用者下的物化視圖日志不受影響;物化視圖日志可以成功導入到新資料庫中并正常工作。