資料泵對已經存在的表載入索引 imp 和impdp

楊奇龍發表於2010-06-04

這裡探討了使用imp 和impdp對含有索引的表的匯入的一點差別。

使用imp可以在表存在的情況下,不刪除表,且匯入表的資料和索引。

1)建立實驗表cust(已存在)

SQL> conn scott/yang
已連線。
SQL> select * from cust;

        ID CUTNAME                                                             
---------- ----------                                                          
         1 JANE                                                                
         2 Jone                                                                
         3 TOM                                                                 
         4 yang                                                                 
         5 yangyi                                                              
         6 xiaonan                                                             
已選擇6行。
SQL> create index indcust_id on cust(id);

索引已建立。
2)匯出表

HOST exp scott/yang file=cust.dmp   tables=cust

連線到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
已匯出 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集

即將匯出指定的表透過常規路徑...
. . 正在匯出表                            CUST匯出了           6 行
成功終止匯出, 沒有出現警告。

SQL> drop index indcust_id;
索引已刪除

3)使用imp匯入表

SQL> HOST imp scott/yang file=cust.dmp    tables=cust  ignore=y

連線到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

經由常規路徑由 EXPORT:V11.01.00 建立的匯出檔案
已經完成 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集中的匯入
. 正在將 SCOTT 的物件匯入到 SCOTT
. 正在將 SCOTT 的物件匯入到 SCOTT
. . 正在匯入表                          "CUST"匯入了           6 行
成功終止匯入, 沒有出現警告。
SQL> select * from cust;

        ID CUTNAME                                                             
---------- ----------                                                          
         1 JANE                                                                
         2 Jone                                                                
         3 TOM                                                                 
         4 yang                                                                 
         5  yangyi                                                              
         6 xiaonan                                                             
         1 JANE                                                                
         2 Jone                                                                
         3 TOM                                                                 
         4 yang                                                                 
         5 yangyi                                                                                                                      
         6 xiaonan                                                             
已選擇12行。
SQL> select index_name from user_indexes
  2  where table_name='CUST';

INDEX_NAME                                                                     
------------------------------
                                                 
INDCUST_ID          

4)impdp的預設工作並非如此,監測到表存在時,impdp會跳過索引的建立                                                        

SQL> create table texp(id number,name varchar2(30));

表已建立。

SQL> insert into texp
  2  select rownum,tname
  3  from tab;
已建立9行。
SQL> commit;
提交完成。
SQL> create index indtexp_id on texp (id);
索引已建立。

5)匯出表

expdp scott/yang  directory=dump dumpfile=scottexp.dp tables=texp

連線到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
啟動 "SCOTT"."SYS_EXPORT_TABLE_01":  scott/******** directory=dump dumpfile=scotttexp.dmp tables=texp
正在使用 BLOCKS 方法進行估計...
處理物件型別 TABLE_EXPORT/TABLE/TABLE_DATA
使用 BLOCKS 方法的總估計: 64 KB
處理物件型別 TABLE_EXPORT/TABLE/TABLE
處理物件型別 TABLE_EXPORT/TABLE/INDEX/INDEX
處理物件型別 TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
. . 匯出了 "SCOTT"."TEXP"                              5.507 KB       9 行
已成功載入/解除安裝了主表 "SCOTT"."SYS_EXPORT_TABLE_01"
******************************************************************************
SCOTT.SYS_EXPORT_TABLE_01 的轉儲檔案集為:
  F:\DUMP\SCOTTTEXP.DMP
作業 "SCOTT"."SYS_EXPORT_TABLE_01" 已於 23:29:38 成功完成


清除資料,並刪除索引:
SQL> drop index indtexp_id;
索引已刪除。
SQL> truncate table texp;
表被截斷。

6)然後再匯入表

impdp scott/yang  directory=dump dumpfile=scottexp.dp tables=texp
SQL> select count(*) from texp;

  COUNT(*)                                                                     
----------                                                                     
         9                                                                     
SQL> select index_name from user_indexes
  2  where table_name='TEXP';

未選定行

資料雖然匯入了,但是索引沒有建立。不過要解決這個問題也很簡單,透過INCLUDE就可以解決這個問題:
SQL> truncate table texp;
表被截斷。
impdp scott/yang directory=dump  dumpfile=scottexp.dp tables=texp table_exists_action=truncate include=index    

 連線到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
已成功載入/解除安裝了主表 "SCOTT"."SYS_IMPORT_TABLE_01"
啟動 "SCOTT"."SYS_IMPORT_TABLE_01":  scott/******** directory=dump dumpfile=scotttexp.dmp tables=texp table_exists_action=truncate include=index
處理物件型別 TABLE_EXPORT/TABLE/INDEX/INDEX
處理物件型別 TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
作業 "SCOTT"."SYS_IMPORT_TABLE_01" 已於 23:35:39 成功完成

SQL> select count(*) from texp;
  COUNT(*)                                                                     
----------                                                                     
         0                                                                
SQL> select index_name from user_indexes
  2  where table_name='TEXP';
INDEX_NAME                                                                     
------------------------------
                                                 
INDTEXP_ID    

 SQL> drop index indtexp_id;
索引已刪除。

impdp  scott/yangdirectory=dump dumpfile=scotttexp.dmp tables=texp table_exists_action=truncate include=index include=table_data

連線到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
已成功載入/解除安裝了主表 "SCOTT"."SYS_IMPORT_TABLE_01"
啟動 "SCOTT"."SYS_IMPORT_TABLE_01":  scott/******** directory=dump dumpfile=scotttexp.dmp tables=texp table_exists_action=truncate include=index include=table_data
處理物件型別 TABLE_EXPORT/TABLE/TABLE_DATA
. . 匯入了 "SCOTT"."TEXP"                              5.507 KB       9 行
處理物件型別 TABLE_EXPORT/TABLE/INDEX/INDEX
處理物件型別 TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
作業 "SCOTT"."SYS_IMPORT_TABLE_01" 已於 23:37:57 成功完成

最後檢查一下是否成功
SQL> select count(*) from texp;
  COUNT(*)                                                                     
----------                                                                     
         9                                                                     
SQL> select index_name from user_indexes
  2  where table_name='TEXP';
INDEX_NAME                                                                     
------------------------------                                                 
INDTEXP_ID
                                                                     

 

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

相關文章