Oracle10g之傳輸表空間詳解

可傳輸表空間概述

Oracle 的可傳輸表空間特性通過將 元數據和數據文件 簡單地從一個數據庫移動到另一個數據庫,提供 在數據庫之間有效移動大數據的一種簡易方法。代替重新創建對象,可移植表空間可以讓 毫不費力地移動大對象,而所花費的時間是你手動創建這些對象的時間。 可移植表空間包括將屬於源數據庫的所有數據文件拷貝到目標數據庫,並將關於表空間 數據目錄信息從源數據庫拷貝到目標數據庫。因此,數據泵取導出和導入實用程序是可移 表空間特性的一部分。還可以傳送屬於表的索引表空間,使整個數據移植非常地快。

可移植表空間的應用場景 : 
把數據從源數據庫移動到數據倉庫 
把數據從升級數據庫移動到數據集市 
把數據從數據倉庫移動到數據集市 
執行表空間時間點恢復 (PITR) 
歸檔歷史數據

然而,在 Oracle9i 數據庫和更低版本中,可傳輸表空間僅限於在目標數據庫和源數據庫都運行在同一操作系統平臺上的少數情況下才有用 — 例如,您不能在 Solaris 和 HP-UX 平臺之間傳輸表空間。
在Oracle 數據庫 10g 中,這個侷限消失了。

字節順序和平臺 
數據文件所以不能跨平臺,主要是由於不同平臺的字節順序不同,這是計算機領域由來已久的問題之一,在各種計算機體系結構中,由於對於字、字節等的存儲機制有所不同,通信雙方交流的信息單元(比特、字節、字、雙字等)應該以什麼樣的順序進行傳送就成了一個問題,如果不達成一致的規則,通信雙方將無法進行正確的編/譯碼從而導致通信失敗。
目前在各種體系的計算機中通常採用的字節存儲機制主要有兩種:Big-Endian和Little-Endian 。
一些操作系統(包括Windows)在低位內存地址中存放二進制數據的最低有效字節,因此這種系統被稱爲Little Endian;一些操作系統(包括Solaris)將最高有效字節存儲在低位內存地址中,因此這種系統被稱爲Big Endian。
舉一個簡單點的例子,假如1122這樣一個數據要存入不同系統,對於Little Endian的系統,存儲的順序就是2211,小頭在前;而對於Big Endian的系統來說,存儲順序就是1122,大頭在前,顯然Big Endian更符合我們通常的語言習慣。
那麼跨平臺的問題就出現了,當一個Little Endian的系統試圖從一個Big Endian的系統中讀取數據時,就需要通過轉換,否則不同的字節順序將導致數據不能被正確讀取。

數據庫所處平臺的字節序可通過如下查詢得到

SQL>select * from v$transportable_platform
PLATFORM_ID PLATFORM_NAME                          ENDIAN_FORMAT
----------- -------------------------------------- --------------
          1 Solaris[tm] OE (32-bit)                Big
          2 Solaris[tm] OE (64-bit)                Big
          7 Microsoft Windows IA (32-bit)          Little
         10 Linux IA (32-bit)                      Little
          6 AIX-Based Systems (64-bit)             Big
          3 HP-UX (64-bit)                         Big
          5 HP Tru64 UNIX                          Little
          4 HP-UX IA (64-bit)                      Big
         11 Linux IA (64-bit)                      Little
         15 HP Open VMS                            Little
          8 Microsoft Windows IA (64-bit)          Little
PLATFORM_ID PLATFORM_NAME                          ENDIAN_FORMAT
----------- -------------------------------------- --------------
          9 IBM zSeries Based Linux                Big
         13 Linux 64-bit for AMD                   Little
         16 Apple Mac OS                           Big
         12 Microsoft Windows 64-bit for AMD       Little
         17 Solaris Operating System (x86)         Little
         18 IBM Power Based Linux                  Big
 
傳輸表空間

傳輸表空間的簡要操作步驟

1) 確定平臺的 Endian 格式 
2) 確保表空間爲自包含並使其只讀 
3) 用 exp、expdp等實用程序導出元數據 
4) 轉換數據文件以匹配 Endian 格式 ( 若一致可跳過)

5) 拷貝文件到目標系統
6) 使用 imp導入實用程序導入元數據

1、確定平臺的 Endian 格式

源平臺

SQL> col PLATFORM_NAME for a30
SQL> SELECT d.platform_name, endian_format FROM v$transportable_platform tp, v$database d WHERE tp.platform_name = d.platform_name;
PLATFORM_NAME                  ENDIAN_FORMAT
------------------------------ --------------
Linux IA (32-bit)              Little

目標平臺:

SQL> col platform_name for a40
SQL> SELECT d.platform_name, endian_format FROM v$transportable_platform tp, v$database d WHERE tp.platform_name = d.platform_name;
PLATFORM_NAME                  ENDIAN_FORMAT
------------------------------ --------------
Linux IA (32-bit)              Little
 
這裏平臺之間的字節序一致,不需要轉換。

2、確保表空間爲自包含並使其只讀

自包含表示用於傳輸的內部表空間集沒有任何對象引用指向外部表空間集。自包含分爲兩種:一般自包含表空間集和完全(嚴格)自包含表空間集。
下面是一些典型的違反自包含的例子:
索引在待傳輸表空間集中而表卻不在,即索引在內部表空間集,而表在外部表空間集。(注意,如果表在待傳輸表空間集中,而索引不在並不違反自包含原則,即表在內部表空間集,而索引在外部表空間集,不違反自包含。當然如果你堅持這樣傳輸的話,會造成目標庫中該表索引丟失)。
分區表中只有部分分區在待傳輸表空間集(對於分區表,要麼全部包含在待傳輸表空間集中,要麼全不包含,解決:需要進行分區交換,完全包含在內部表空間集中)。
待傳輸表空間中,對於引用完整性約束,如果約束指向的表不在待傳輸表空間集,則違反自包含約束;但如果不傳輸該約束,則與約束指向無關。

對於包含LOB列的表,如果表在待傳輸表空間集中,而Lob列不在,也是違反自包含原則的。
自包含還有嚴格(strict)或完全(full)self_contained.這時:
set the TTS_FULL_CHECK parameter to TRUE,對象及其依賴對象完全在內部表空間集
而非嚴格自包含,表在內部表空間集,依賴於表的對象在外部白空間集不違反,但只是依賴於表的對象在內部表空間集,而表在外部表空間集就違反了。

我們可以通過使用dbms_tts包裏的存儲過程transport_set_check,檢查要傳輸的表空間是否是自包含。具體定義如下

PROCEDURE TRANSPORT_SET_CHECK
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 TS_LIST                        CLOB                    IN
 INCL_CONSTRAINTS               BOOLEAN                 IN     DEFAULT
 FULL_CHECK                     BOOLEAN                 IN     DEFAULT

TS_LIST表示要傳輸的表空間名稱列表,不同的表空間名稱之間以逗號隔開

INCL_CONSTRAINTS表示要檢查的子表外建(true表示是,false表示否。默認爲false),也就是說,如果在要傳輸的表空間裏的某個子表上存在外建,且該外建所指向的父表在其他表空間內,則INCL_CONSTRAINTS爲true,表明違反了自包含。否則爲false,表明沒有違反自包含。

FULL_CHECK表示是否要檢查表的索引(true表示是,false表示否。默認爲false),也就是說,如果在要傳輸的表空間裏的某個表的索引位於其他表空間內,FULL_CHECK則爲true,表明違反了自包含。否則爲false,表明沒有違反自包含。檢查後的結果在transport_set_violations視圖中體現。

下面做一個簡單測試

外建約束測試

SQL> create table t tablespace users
  2  as
  3  select object_id,object_name from user_objects;
Table created.
SQL> create table t_child tablespace example
  2  as
  3  select object_id,object_name from user_objects
  4  where 1=2;
Table created.

SQL> alter table t add primary key(object_id);
Table altered.

SQL> alter table t_child add foreign key(object_id)
  2  references
  3* t(object_id)

進行外鍵檢查

SQL> execute dbms_tts.transport_set_check('example',incl_constraints=>true);
PL/SQL procedure successfully completed.
SQL> select * from transport_set_violations;
VIOLATIONS
--------------------------------------------------------------------------------
Constraint SYS_C005435 between table HR.T in tablespace USERS and table HR.T_CHI
LD in tablespace EXAMPLE
SQL> execute dbms_tts.transport_set_check('users',incl_constraints=>true);
PL/SQL procedure successfully completed.
SQL> select * from transport_set_violations;
no rows selected

將users和example表空間一起進行檢查

SQL> execute dbms_tts.transport_set_check('users,example',incl_constraints=>true);
PL/SQL procedure successfully completed.
SQL> select * from transport_set_violations;
no rows selected

進行FULL_CHECK測試

SQL> create index t_index on t_child(object_id) tablespace indx;
Index created.

SQL> execute dbms_tts.transport_set_check('example',full_check=>true);
PL/SQL procedure successfully completed.
SQL> select * from transport_set_violations;
VIOLATIONS
--------------------------------------------------------------------------------
Index HR.T_INDEX in tablespace INDX points to table HR.T_CHILD in tablespace EXA
MPLE
SQL> execute dbms_tts.transport_set_check('example,indx',full_check=>true);
PL/SQL procedure successfully completed.

SQL> select * from transport_set_violations;
no rows selected

將索引和外鍵同時檢查。
SQL> execute dbms_tts.transport_set_check('users,example,indx',true,true);
PL/SQL procedure successfully completed.
SQL> select * from transport_set_violations;
no rows selected
 
現在將users,example,indx表空間置爲只讀

SQL> alter tablespace users read only;
Tablespace altered.
SQL> alter tablespace example read only;
Tablespace altered.
SQL> alter tablespace indx read only;
Tablespace altered.

3、用 exp、expdp等實用程序導出元數據

$ exp \'/ as sysdba \' file=usr_exp_idx.tbs.dmp transport_tablespace=y tablespace=users,example,indx
LRM-00101: unknown parameter name 'tablespace'
EXP-00019: failed to process parameters, type 'EXP HELP=Y' for help
EXP-00000: Export terminated unsuccessfully
[oracle@dg1 ~]$ exp \'/ as sysdba \' file=usr_exp_idx.tbs.dmp transport_tablespace=y tablespaces=users,example,indx
Export: Release 10.2.0.1.0 - Production on Thu Nov 17 10:43:09 2011
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8ISO8859P1 character set (possible charset conversion)
Note: table data (rows) will not be exported
About to export transportable tablespace metadata...
...........................................................................................................

. end transportable tablespace metadata export
Export terminated successfully without warnings.

4、轉換數據文件以匹配 Endian 格式 ( 若一致可跳過)

這裏筆者的平臺字節序是一致的不需要轉換,如需轉換則可以使用如下命令

$ rman target /
RMAN> convert tablespace trans
2> to platform 'Microsoft Windows IA (32-bit)'
3> format '/tmp/%N_%f';

5、拷貝文件到目標系統

$ scp /u01/app/oracle/oradata/czmmiao/indx01.dbf /u01/app/oracle/oradata/czmmiao/example.dbf /u01/app/oracle/oradata/czmmiao/users01.dbf usr_exp_idx.tbs.dmp 192.168.0.102:/home/oracle/
[email protected]'s password: 
indx01.dbf                                    100%   10MB   5.0MB/s   00:02    
example.dbf                                   100%  100MB   4.4MB/s   00:23    
users01.dbf                                   100% 5128KB   5.0MB/s   00:01    
usr_exp_idx.tbs.dmp                           100% 1072KB   1.1MB/s   00:01

6、使用 imp導入實用程序導入元數據

在導入之前我們需要確認目標數據庫具有我們想導入的表空間內的對象的屬主。

源數據庫的對象屬主

SQL> select distinct owner from dba_segments where tablespace_name in ('EXAMPLE','USERS','INDX');
OWNER
------------------------------
HR
SCOTT
OE
PM
SH
IX
SYS
TRANS

如果在目標平臺沒有存在該用戶就會報如下錯誤。

IMP-00003: ORACLE error 29342 encountered
ORA-29342: user HR does not exist in the database
ORA-06512: at "SYS.DBMS_PLUGTS", line 1895
ORA-06512: at line 1
IMP-00000: Import terminated unsuccessfully

注意,如果在目標平臺已經存在相應的表空間也無法成功導入,報錯如下

IMP-00003: ORACLE error 29349 encountered
ORA-29349: tablespace 'USERS' already exists
ORA-06512: at "SYS.DBMS_PLUGTS", line 1801
ORA-06512: at line 1
IMP-00000: Import terminated unsuccessfully
 
執行導入命令

$ imp \'/ as sysdba \' file=usr_exp_idx.tbs.dmp transport_tablespace=y tablespaces=example,users,indxdatafiles=/home/oracle/users01.dbf,/home/oracle/example.dbf,/home/oracle/indx01.dbf

注意表空間名要與源數據庫一致

將這3個表空間設爲可讀寫

SQL> alter tablespace example read write;
Tablespace altered.
SQL>  alter tablespace indx read write;
Tablespace altered.

SQL> alter tablespace users read write;
Tablespace altered.
 
至此,導入成功。


來自:http://blog.csdn.net/yrg5101/article/details/7775123


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章