1. 程式人生 > 其它 >關於exp/imp的總結學習(r4筆記第26天)

關於exp/imp的總結學習(r4筆記第26天)

關於exp/imp,是很常用的資料匯出匯入工具,在10g開始推出的資料泵datapump相當於是exp/imp的補充和升級版本。在後續章節再做一個總結。 exp/imp的使用相對比較簡單,通常用做在不同的資料庫或者環境之間轉移資料,即使資料庫位於不同的平臺,也可以通過統一的介面來做資料的匯入匯出工作。 使用方法可以通過exp -help,imp -help來得到一個簡單的幫助說明。

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 usernamesFILE output files (EXPDAT.DMP) TABLES list of table namesCOMPRESS import into one extent (Y) RECORDLENGTH length of IO recordGRANTS export grants (Y) INCTYPE incremental export typeINDEXES 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 filenameCONSISTENT 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 fileFLASHBACK_SCN SCN used to set session snapshot back toFLASHBACK_TIME time used to get the SCN closest to the specified timeQUERY select clause used to export a subset of a tableRESUMABLE suspend when a space related error is encountered(N)RESUMABLE_NAME text string used to identify resumable statementRESUMABLE_TIMEOUT wait time for RESUMABLE TTS_FULL_CHECK perform full or partial dependency check for TTSVOLSIZE number of bytes to write to each tape volumeTABLESPACES list of tablespaces to exportTRANSPORT_TABLESPACE export transportable tablespace metadata (N)TEMPLATE template name which invokes iAS mode export

USERID must be the first parameter on the command line.Keyword Description (Default) Keyword Description (Default)--------------------------------------------------------------------------USERID username/password FULL import entire file (N)BUFFER size of data buffer FROMUSER list of owner usernamesFILE input files (EXPDAT.DMP) TOUSER list of usernamesSHOW just list file contents (N) TABLES list of table namesIGNORE ignore create errors (N) RECORDLENGTH length of IO recordGRANTS import grants (Y) INCTYPE incremental import typeINDEXES import indexes (Y) COMMIT commit array insert (N)ROWS import data rows (Y) PARFILE parameter filenameLOG log file of screen output CONSTRAINTS import constraints (Y)DESTROY overwrite tablespace data file (N)INDEXFILE write table/index info to specified fileSKIP_UNUSABLE_INDEXES skip maintenance of unusable indexes (N)FEEDBACK display progress every x rows(0)TOID_NOVALIDATE skip validation of specified type ids FILESIZE maximum size of each dump fileSTATISTICS import precomputed statistics (always)RESUMABLE suspend when a space related error is encountered(N)RESUMABLE_NAME text string used to identify resumable statementRESUMABLE_TIMEOUT wait time for RESUMABLE COMPILE compile procedures, packages, and functions (Y)STREAMS_CONFIGURATION import streams general metadata (Y)STREAMS_INSTANTIATION import streams instantiation metadata (N)DATA_ONLY import only data (N)VOLSIZE number of bytes in file on each volume of a file on tapeThe following keywords only apply to transportable tablespacesTRANSPORT_TABLESPACE import transportable tablespace metadata (N)TABLESPACES tablespaces to be transported into databaseDATAFILES datafiles to be transported into databaseTTS_OWNERS users that own data in the transportable tablespace set

匯出模式有四種,資料庫,表空間,使用者,表級四個級別。 對於資料庫級的匯入匯出,需要擁有exp_full_database許可權,一般這個操作也是通過dba來執行,但是使用的場景相對比較少。 一個簡單例子如下: exp n1/n1 file=db_backup.dmp buffer=9102000 log=db_backup.log full=Y 至於匯入,就不能直接使用全庫匯入了,一般都按照要去來看哪些資料缺失了,針對性的補充匯入。 對於表空間的匯入匯出,這個功能和表空間傳輸的特性是相關聯的,如果要使用表空間傳輸,需要用到,transport_tablespaces,tablespace這兩個引數而且需要使用sys來做這個操作。 一個簡單的例子如下 匯出

exp 'sys/oracle@PROD as sysdba' file=hr_PROD.dmp transport_tablespace=y tablespaces=tbs4

匯入: imp 'sys/oracle123@repos as sysdba' file=hr_PROD.dmp transport_tablespace=y tablespaces=tbs4 datafiles=/u01/app/db/oradata/repos/tbs04.dbf

對於使用者級的匯入匯出,可能使用要頻繁一些。 匯出的簡單示例,匯出appc下的所有資訊,匯出dump為a.dmp exp n1/n1 file=a.dmp owner=appc 匯入的時候,匯入到work賬戶中 imp n1/n1 file=a.dmp fromuser=appc touser=work 最後就是表級的匯入匯出,使用頻率最為頻繁。 最簡單的一個例子就是 exp n1/n1 file=a.dmp tables=TEST imp appc/appc file=a.dmp tables=TEST 其實我們來做些更多的使用補充。 -->使用parfile來完成複雜的匯入匯出 這個功能可能在做一些動態的資料匯入匯出的時候尤為有用,可以只關注於parfile的部分,比如我們匯出appc下的表test1,test2,test3 就可以定義parfile appc.parfile,內容為 tables=test1,test2,test3 匯入匯出命令類似 exp appc/appc file=a.dmp parfile=appc.parfile logfile=appc_exp.log 匯入命令類似 imp appc/appc file=a.dmp parfile=appc.parfile logfile=appc_exp.log 如果要指定匯出幾十個,上百個表,使用parfile就能避免命令的繁瑣。 -->使用query來做細粒度的匯入匯出 SQL> select object_id from test; OBJECT_ID ---------- 20 46 如果我只想匯出object_id為20的資料,就可以使用query來進行邏輯層面的匯出。 [ora11g@rac1 ora11g]$ exp n1/n1 file=a.dmp tables=test query='where object_id=20' About to export specified tables via Conventional Path ... . . exporting table TEST 1 rows exported Export terminated successfully without warnings. 如果要實現複雜的邏輯匯出,也未嘗不可。 -->生成資料指令碼 可能對於生成ddl指令碼的功能大家並不在意。但是在特定的場景中也是很實用的。 比如我們匯出表test,t的建立指令碼。


[ora11g@rac1 ora11g]$ exp n1/n1 file=a.dmp tables=test,t rows=n
Export: Release 11.2.0.3.0 - Production on Sun Jan 25 06:44:11 2015
Note: table data (rows) will not be exported
About to export specified tables via Conventional Path ...
. . exporting table                           TEST
. . exporting table                              T
Export terminated successfully without warnings.

然後使用imp來生成ddl指令碼,這個過程不是在做實際的匯入,只是在生成指令碼。

[ora11g@rac1 ora11g]$ imp n1/n1 file=a.dmp indexfile=a.log tables=test,t
Import: Release 11.2.0.3.0 - Production on Sun Jan 25 06:45:04 2015
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
Export file created by EXPORT:V11.02.00 via conventional path
import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
Import terminated successfully without warnings.
[ora11g@rac1 ora11g]$ ls -lrt
total 28
-rw-r--r-- 1 ora11g dba 24576 Jan 25 06:44 a.dmp
-rw-r--r-- 1 ora11g dba  1610 Jan 25 06:45 a.log

生成的指令碼如下,可能還需要進一步處理,但是相對來說也算比較清晰了。

[ora11g@rac1 ora11g]$ less a.log 
REM  CREATE TABLE "N1"."TEST" ("OWNER" VARCHAR2(30) NOT NULL ENABLE, 
REM  "OBJECT_NAME" VARCHAR2(30) NOT NULL ENABLE, "SUBOBJECT_NAME" 
REM  VARCHAR2(30), "OBJECT_ID" NUMBER NOT NULL ENABLE, "DATA_OBJECT_ID" 
REM  NUMBER, "OBJECT_TYPE" VARCHAR2(19), "CREATED" DATE NOT NULL ENABLE, 
REM  "LAST_DDL_TIME" DATE NOT NULL ENABLE, "TIMESTAMP" VARCHAR2(19), 
REM  "STATUS" VARCHAR2(7), "TEMPORARY" VARCHAR2(1), "GENERATED" 
REM  VARCHAR2(1), "SECONDARY" VARCHAR2(1), "NAMESPACE" NUMBER NOT NULL 
REM  ENABLE, "EDITION_NAME" VARCHAR2(30)) PCTFREE 10 PCTUSED 40 INITRANS 1 
REM  MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 
REM  FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE 
REM  "POOL_DATA" LOGGING NOCOMPRESS ;
REM  CREATE TABLE "N1"."T" ("OWNER" VARCHAR2(30), "OBJECT_NAME" 
REM  VARCHAR2(128), "SUBOBJECT_NAME" VARCHAR2(30), "OBJECT_ID" NUMBER, 
REM  "DATA_OBJECT_ID" NUMBER, "OBJECT_TYPE" VARCHAR2(19), "CREATED" DATE, 
REM  "LAST_DDL_TIME" DATE, "TIMESTAMP" VARCHAR2(19), "STATUS" VARCHAR2(7), 
REM  "TEMPORARY" VARCHAR2(1), "GENERATED" VARCHAR2(1), "SECONDARY" 
REM  VARCHAR2(1), "NAMESPACE" NUMBER, "EDITION_NAME" VARCHAR2(30)) PCTFREE 
REM  10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 2097152 NEXT 
REM  1048576 MINEXTENTS 1 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL 
REM  DEFAULT) TABLESPACE "POOL_DATA" LOGGING NOCOMPRESS ;
CONNECT N1;
CREATE UNIQUE INDEX "N1"."IND_T" ON "T" ("OBJECT_ID" ) PCTFREE 10 INITRANS 
2 MAXTRANS 255 STORAGE(INITIAL 262144 NEXT 1048576 MINEXTENTS 1 FREELISTS 
1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "POOL_DATA" LOGGING ;

-->能夠提升效能的引數 在匯入匯出的過程中,為了提高效能不妨考慮以下幾個引數 grants=n --一般來說資料的匯入匯出,是不需要這些許可權資訊的,我們需要的只是資料。 statistics=none --統計資訊很可能不需要,多個環境之間的資料量不同,尤其是大的分割槽表,如果匯入統計資訊還是很耗時的。可以酌情使用 constraints=n --在資料匯入的過程中,很可能不需要這個選項,大多數情況下,不需要我們來改變目標環境的結構 indexes=n --這個選項一般也是不需要的,很可能目標環境已經有對應的索引了。 buffer=9102000 這個引數很重要,對於exp/imp來說,根據自己的條件來設定,我一般設定為9M,基本都夠用了。