【匯入匯出】Oracle 常用匯入匯出工具集錦

leonarding發表於2015-03-27


《Oracle 常用匯入匯出工具集錦》

新年新群招募: 中國Oracle精英聯盟 170513055
群介紹:本群是大家的一個技術分享社群,在這裡可以領略大師級的技術講座,還有機會參加Oracle舉辦的技術沙龍,與興趣相投的小夥伴一起笑談風雲起,感悟職場情!
資料庫版本


SYS@LEO1>select * from v$version;

BANNER

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

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

PL/SQL Release 11.2.0.1.0 - Production

CORE    11.2.0.1.0      Production

TNS for Linux: Version 11.2.0.1.0 - Production

NLSRTL Version 11.2.0.1.0 – Production

作業系統資訊

[oracle@leonarding1 admin]$ uname -a

Linux leonarding1.oracle.com 2.6.32-200.13.1.el5uek #1 SMP Wed Jul 27 21:02:33 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

比較使用sql*loader的直接載入方式和傳統載入方式的效能差異,給出演示過程和結論。

第一 我們先要生成平面資料(文字資料)

LEO1@LEO1>create table leo2 as select * from dba_objects;      建立資料來源,我們的平面資料就是從這個表中取出

Table created.

第二 我們利用spool工具將螢幕中顯示出來的記錄寫入到指定檔案,這樣我們就可以得到一個平面檔案啦

set termout off;            是否在螢幕上顯示輸出內容,off螢幕不顯示查詢語句,主要與spool結合使用

set feedback off;           關閉本次sql命令處理的記錄條數,預設為on即去掉最後的已經選擇的行數

set echo off;              關閉指令碼中正在執行的SQL語句的顯示

set heading off;            關閉標題的輸出,設定為off就去掉了select結果的欄位名只顯示資料

set trimout on;            去除標準輸出每行後面多餘的空格

set trimspool on;          將每行後面多餘的空格去掉【linesize-實際字元數=多餘空格】

 

spool /home/oracle/sql_loader/leo3.txt      在螢幕上的所有內容都包含在該檔案中

select owner||','||object_name||','||object_id||','||object_type from leo2;   

spool off                         只有關閉spool輸出,才會在輸出檔案中看到輸出的內容

備註:在實用SPOOL輸出內容到本地檔案時,需注意編碼格式,否則會出現亂碼的問題

[oracle@leonarding1 sql_loader]$ ll

total 28468

-rw-r--r-- 1 oracle oinstall  3246601 Jun 22 14:06 leo3.txt          已經生成平面檔案leo3.txt

[oracle@leonarding1 sql_loader]$ cat leo3.txt | wc -l              檔案中有72678行記錄

72678

第三 建立裝入的表leo3_loader

LEO1@LEO1>create table leo3_loader

 (

   owner       varchar2(30),

   object_name varchar2(130),

   object_id   number,

   object_type varchar2(20)

 );

  2    3    4    5    6    7 

第四 建立sql*loader的控制檔案leo3_loader.ctl

[oracle@leonarding1 sql_loader]$ vim leo3_loader.ctl

load data

infile '/home/oracle/sql_loader/leo3.txt'                             待載入的資料檔案

badfile '/home/oracle/sql_loader/leo3_bad.txt'                       格式不匹配寫入壞檔案

discardfile '/home/oracle/sql_loader/leo3_discard.txt'                  條件不匹配寫入丟棄檔案

append into table leo3_loader                                     追加的方式插入資料

fields terminated by ","                                           欄位與欄位之間的分隔符

trailing nullcols                                                 這句的意思是將沒有對應值的列都置為null

(owner,object_name,object_id,object_type)                         資料插入的對應欄位

 

第五 執行sqlldr直接載入命令

[oracle@leonarding1 sql_loader]$ sqlldr leo1/leo1 control=leo3_loader.ctl log=leo3_loader.log direct=true

SQL*Loader: Release 11.2.0.1.0 - Production on Sat Jun 22 14:08:31 2013

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

Load completed - logical record count 72678.

已經載入了72678行,條件不匹配有72行,實際載入入72606

LEO1@LEO1>select count(*) from leo3_loader;   表中也是

     72606

我們在看一下sql*loader日誌

。。。省略前部份。。。

Total logical records skipped:          0

Total logical records read:         72678

Total logical records rejected:         0

Total logical records discarded:       72                   條件不匹配有72

Total stream buffers loaded by SQL*Loader main thread:       17

Total stream buffers loaded by SQL*Loader load thread:        6

 

Run began on Sat Jun 22 14:08:31 2013

Run ended on Sat Jun 22 14:08:34 2013

 

Elapsed time was:     00:00:02.60                       所用耗時2.6

CPU time was:         00:00:00.13

使用conventional傳統載入方式寫入資料

LEO1@LEO1>truncate table leo3_loader;                   清空表在載入一次

[oracle@leonarding1 sql_loader]$ sqlldr leo1/leo1 control=leo3_loader.ctl log=leo3_loader.log

LEO1@LEO1>select count(*) from leo3_loader;   表中也是

     72606

我們在看一下sql*loader日誌

。。。省略前部份。。。

Total logical records skipped:          0

Total logical records read:         72678

Total logical records rejected:         0

Total logical records discarded:       72                   條件不匹配有72

 

Run began on Sat Jun 22 15:25:45 2013

Run ended on Sat Jun 22 15:26:05 2013

 

Elapsed time was:     00:00:20.79                       所用耗時2.6

CPU time was:         00:00:00.48

小結:經過比對directconventional要提高了20倍效率,為什麼direct會這麼高效呢,下面我們來說說這兩種的區別。

Direct 特點

1)資料繞過SGA直接寫入磁碟的資料檔案

2)資料直接寫入高水位線HWM之後的新塊,不會掃描HWM之前的空閒塊

3commit之後移動HWM他人才能看到

4)不對已用空間進行掃描

5)使用direct幾乎不產生redo log,不是完全不產生(安全性差),但會產生undo資料

6)適用OLAP線上分析場景,增 刪 改不頻繁的場景

Conventional傳統載入特點

1)資料先載入 -> SGA -> 磁碟的資料檔案

2)會掃描高水位線HWM之前的資料塊,如果有空閒塊(碎片經常DML導致)就會利用,如果沒有再插入新塊

3)高水位線HWM之前的資料塊是放在SGA區的

4)會產生redo logundo資料

5)安全性高,可恢復資料

6)傳統載入與SQL語句insert插入沒區別

 

比較資料泵和exp/imp對相同資料匯出/匯入的效能差異,給出演示過程

EXP/IMP 是資料庫傳統的匯出匯入工具,它有使用方便,在服務端和客戶端都可用的優點,速度沒有expdp快,功能沒有expdp強大

EXP  工具幫助文件,我們可以參考幫助文件進行命令列配置

[oracle@leonarding1 ~]$ exp –help    或者    exp help=y

 

Export: Release 11.2.0.1.0 - Production on Thu Jun 20 07:28:33 2013

 

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

 

 

 

You can let Export prompt you for parameters by entering the EXP

command followed by your username/password:

 

     Example: EXP SCOTT/TIGER

 

Or, you can control how Export runs by entering the EXP command followed

by various arguments. To specify parameters, you use keywords:

 

     Format:  EXP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)

     Example: EXP SCOTT/TIGER GRANTS=Y TABLES=(EMP,DEPT,MGR)

               or TABLES=(T1:P1,T1:P2), if T1 is partitioned table

 

USERID must be the first parameter on the command line.

 

Keyword    Description (Default)      Keyword      Description (Default)

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

USERID     username/password          FULL         export entire file (N)

BUFFER     size of data buffer        OWNER        list of owner usernames

FILE       output files (EXPDAT.DMP)  TABLES       list of table names

COMPRESS   import into one extent (Y) RECORDLENGTH length of IO record

GRANTS     export grants (Y)          INCTYPE      incremental export type

INDEXES    export indexes (Y)         RECORD       track incr. export (Y)

DIRECT     direct path (N)            TRIGGERS     export triggers (Y)

LOG        log file of screen output  STATISTICS   analyze objects (ESTIMATE)

ROWS       export data rows (Y)       PARFILE      parameter filename

CONSISTENT cross-table consistency(N) CONSTRAINTS  export constraints (Y)

 

OBJECT_CONSISTENT    transaction set to read only during object export (N)

FEEDBACK             display progress every x rows (0)

FILESIZE             maximum size of each dump file

FLASHBACK_SCN        SCN used to set session snapshot back to

FLASHBACK_TIME       time used to get the SCN closest to the specified time

QUERY                select clause used to export a subset of a table

RESUMABLE            suspend when a space related error is encountered(N)

RESUMABLE_NAME       text string used to identify resumable statement

RESUMABLE_TIMEOUT    wait time for RESUMABLE

TTS_FULL_CHECK       perform full or partial dependency check for TTS

VOLSIZE              number of bytes to write to each tape volume

TABLESPACES          list of tablespaces to export

TRANSPORT_TABLESPACE export transportable tablespace metadata (N)

TEMPLATE             template name which invokes iAS mode export

 

Export terminated successfully without warnings.

 

建立測試表leo1

LEO1@LEO1>set linesize 400 pagesize 999                   格式化

LEO1@LEO1>drop table leo1 purge;                        清空環境

Table dropped.

LEO1@LEO1>create table leo1 as select * from dba_objects;     建立測試表leo1

Table created.

LEO1@LEO1>insert into leo1 select * from leo1;

72543 rows created.

LEO1@LEO1>insert into leo1 select * from leo1;

145086 rows created.

LEO1@LEO1>insert into leo1 select * from leo1;

290172 rows created.

LEO1@LEO1>insert into leo1 select * from leo1;

580344 rows created.

LEO1@LEO1>commit;

Commit complete.

LEO1@LEO1>insert into leo1 select * from leo1;              

1160688 rows created.

LEO1@LEO1>commit;

Commit complete.

LEO1@LEO1>select count(*) from leo1;                      現在是232

  COUNT(*)

----------

   2321376

[oracle@leonarding1 ~]$ mkdir exp_dump                    建立一個匯出檔案的資料夾

[oracle@leonarding1 ~]$ exp leo1/leo1 file='/home/oracle/exp_dump/leo1.dmp' tables=leo1 rows=y

Export: Release 11.2.0.1.0 - Production on Fri Jun 21 06:38:28 2013

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export done in US7ASCII character set and AL16UTF16 NCHAR character set

server uses ZHS16GBK character set (possible charset conversion)

 

About to export specified tables via Conventional Path ...

. . exporting table                           LEO1    2321376 rows exported

Export terminated successfully without warnings.  成功終止匯出,沒有出現告警

[oracle@leonarding1 ~]$ date

Fri Jun 21 06:39:40 CST 2013

匯出從06:38:28開始到06:39:40結束,一共用時72

LEO1@LEO1>truncate table leo1;                           清空表,我再匯入一下

Table truncated.

[oracle@leonarding1 ~]$ cd exp_dump/

[oracle@leonarding1 exp_dump]$ ll

total 261356

-rw-r--r-- 1 oracle oinstall 267362304 Jun 21 06:39 leo1.dmp      這個就是我們匯出的檔案

[oracle@leonarding1 exp_dump]$ imp leo1/leo1 file='/home/oracle/exp_dump/leo1.dmp' full=y ignore=y

 

Import: Release 11.2.0.1.0 - Production on Fri Jun 21 06:45:04 2013

 

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

 

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

 

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

import done in US7ASCII character set and AL16UTF16 NCHAR character set

import server uses ZHS16GBK character set (possible charset conversion)

. importing LEO1's objects into LEO1

. importing LEO1's objects into LEO1

. . importing table                         "LEO1"    2321376 rows imported

Import terminated successfully without warnings.  成功終止匯入,沒有出現告警

[oracle@leonarding1 exp_dump]$ date

Fri Jun 21 06:48:26 CST 2013

匯入從06:45:04開始到06:48:26結束,一共用時202

 

EXPDP/IMPDP 是Oracle推崇的資料泵匯入匯出工具,用於代替傳統的EXP/IMP,只能在服務端使用,效率比EXP/IMP快幾十倍,有續傳功能和並行功能。這個工具始於Oracle 10g,從Oracle 11g開始不再提供老的EXP/IMP的諮詢但工具還可以使用。

EXPDP工具的效率差不多比EXP快幾倍

IMPDP工具的效率差不多比IMP快幾十倍

所以說這兩個工具適用於大資料匯入匯出的場景

匯出檔案的格式更接近於資料庫本身的檔案格式,避免了資料寫入檔案時的轉換

直接路徑載入,跳過SGA記憶體區,直接載入到高水位線之後

後設資料metadata和資料data在匯出的過程中可以重疊進行,提高匯出的效率。

 

我們再來看看EXPDP/IMPDP的匯出匯入效果

在使用EXPDP/IMPDP之前我們需要定義一個目錄物件,告知EXPDP/IMPDP工具匯出的檔案和匯入的檔案的存放目錄

LEO1@LEO1>create directory exp_dump as '/home/oracle/exp_dump';

Directory created.

[oracle@leonarding1 exp_dump]$ expdp leo1/leo1 directory=exp_dump dumpfile=expdp_leo1.dmp tables=leo1;

 

Export: Release 11.2.0.1.0 - Production on Fri Jun 21 07:07:09 2013

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Starting "LEO1"."SYS_EXPORT_TABLE_01":  leo1/******** directory=exp_dump dumpfile=expdp_leo1.dmp tables=leo1

Estimate in progress using BLOCKS method...

Processing object type TABLE_EXPORT/TABLE/TABLE_DATA

Total estimation using BLOCKS method: 264 MB

Processing object type TABLE_EXPORT/TABLE/TABLE

. . exported "LEO1"."LEO1"                               223.9 MB 2321376 rows

Master table "LEO1"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded

******************************************************************************

Dump file set for LEO1.SYS_EXPORT_TABLE_01 is:

  /home/oracle/exp_dump/expdp_leo1.dmp

Job "LEO1"."SYS_EXPORT_TABLE_01" successfully completed at 07:08:22

匯出從07:07:09開始到07:08:22結束,一共用時73秒,匯出檔案大小223.9MB 行數2321376 rows,與exp時間相差1秒,expdp在資料量比較大的時候會體現出高效率。

現在我們impdp匯入

LEO1@LEO1>drop table leo1;   先把表刪除,因為如果表結構存在的話,impdp會認為後設資料已存在報錯,不可匯入

Table dropped.

[oracle@leonarding1 exp_dump]$ impdp leo1/leo1 directory=exp_dump dumpfile=expdp_leo1.dmp

 

Import: Release 11.2.0.1.0 - Production on Fri Jun 21 07:24:07 2013

 

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

 

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Master table "LEO1"."SYS_IMPORT_FULL_01" successfully loaded/unloaded

Starting "LEO1"."SYS_IMPORT_FULL_01":  leo1/******** directory=exp_dump dumpfile=expdp_leo1.dmp

Processing object type TABLE_EXPORT/TABLE/TABLE

Processing object type TABLE_EXPORT/TABLE/TABLE_DATA

. . imported "LEO1"."LEO1"                               223.9 MB 2321376 rows

Job "LEO1"."SYS_IMPORT_FULL_01" successfully completed at 07:27:27

匯入從07:24:07開始到07:27:27結束,一共用時200秒,匯入檔案大小223.9MB 行數2321376 rows,比imp時間節約了2秒,impdp也是在資料量比較大的時候會體現出高效率。

 

 

用外部表的方式查詢當天資料庫alert日誌檔案中當天所有的ora-錯誤資訊,給出演示過程。

外部表:表中的資料以作業系統檔案的方式來存放,現在表中的資料不是放在資料庫中了而是放在作業系統上面,Oracle提供了一種直接讀取外部資料的機制。

外部表好處:1.資料二次開發

            2.大資料量遷移

            3.充分利用作業系統空間

            4.不佔用資料庫空間

            5.支援標準SQL條件檢索

外部表也需要目錄物件的支援,通過目錄物件可以知道從哪個目錄讀取文字資料

LEO1@LEO1>create directory alert as '/u02/app/oracle/diag/rdbms/leo1/LEO1/trace';

Directory created.

這是Oracle 11g 告警日誌目錄

grant read,write on directory alert to public;            對這個目錄物件授予讀/寫許可權,並授予所有使用者

LEO1@LEO1>select * from dba_directories;

OWNER         DIRECTORY_NAME                 DIRECTORY_PATH

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

SYS             EXP_DUMP                      /home/oracle/exp_dump

SYS             XMLDIR                         /u02/app/oracle/product/11.2.0/db_1/rdbms/xml

SYS             ALERT                          /u02/app/oracle/diag/rdbms/leo1/LEO1/trace

SYS             DATA_PUMP_DIR                 /u02/app/oracle/admin/LEO1/dpdump/

SYS             ORACLE_OCM_CONFIG_DIR        /u02/app/oracle/product/11.2.0/db_1/ccr/state

我們下面就是Oracle告警日誌檔案當作資料庫的一個外部資料來源來訪問,我們使用外部表的方式抽取alert日誌資料,然後使用標準SQL語句來檢索“ora-錯誤資訊”。

下面我們就來建立一個外部表

LEO1@LEO1>create table leo_alert (content varchar2(4000))     alert日誌資料量多因此字串設定的大一點

organization external

(

type oracle_loader                                       如果你設定的是oracle_datapump請修改為loader

default directory alert

access parameters (

records delimited by newline                               每條記錄用換行區分

nobadfile                                               沒有壞檔案,丟棄檔案,日誌檔案

nodiscardfile

nologfile

)

location ('alert_LEO1.log')                                  載入告警日誌檔案內容

);  2    3    4    5    6    7    8    9   10   11   12   13 

LEO1@LEO1>select count(*) from leo_alert;                   一共7198

  COUNT(*)

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

      7198

我們抽取其中10ORA-開頭的錯誤記錄顯示出來

LEO1@LEO1>select * from leo_alert where content like '%ORA-%' and rownum<=10;

CONTENT

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

ORA-210 signalled during: create tablespace test datafile '/u02/app/oracle/oradata/LEO1/test01.dbf' size 10m autoextend off...

ORA-00210: cannot open the specified control file

ORA-00202: control file: '/u02/app/oracle/oradata/LEO1/control01.ctl'

ORA-27041: unable to open file

ORA-00210: cannot open the specified control file

ORA-00202: control file: '/u02/app/oracle/oradata/LEO1/control01.ctl'

ORA-27041: unable to open file

ORA-00210: cannot open the specified control file

ORA-00202: control file: '/u02/app/oracle/oradata/LEO1/control01.ctl'

ORA-27037: unable to obtain file status

10 rows selected.

小結:這裡需要注意幾個問題,我們在建立外部表的時候需要設定沒有壞檔案,丟棄檔案,日誌檔案引數否則會報錯ORA-29913: error in executing ODCIEXTTABLEOPEN callout

 

sql*loader exp/imp expdp/impdp organization_external direct

 

劉盛Leonarding
2013.6.22
北京&summer
分享技術~成就夢想
Blog
www.leonarding.com



##########################################################################################
如果喜歡我的文章就請掃下面二維碼吧!關注微訊號:leonarding_public
在這裡你能得到技術、實事、熱點訊息等新興事物的思考和觀點,別的地方可能沒有的東西。我將為大家提供最新技術與資訊動態,傳遞正能量。
【匯入匯出】Oracle 常用匯入匯出工具集錦

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

相關文章