Oracle提供了逻辑备份和物理备份两种备份手段。早期的Exp/Imp和10g之后的Data Pump都是实现逻辑备份的重要方式。借助两个逻辑备份工具,我们可以方便的对数据库进行多角度、多维度的备份还原操作。
在数据表数据选择层面,query参数可以针对导出的数据表添加where条件。只有满足query条件的数据记录才能导出。这个特性在实际中是非常有用的。比如:我们在进行大规模测试的之前,可能需要先导出一小部分数据进行试验。或者恰恰需要导出满足条件的记录数据。
在使用query参数的时候,需要一些注意的问题。本文主要介绍在命令行(command line)和参数文件(parameter file)中的使用细节。
1、环境介绍
我们选择使用Oracle 11g进行实验。
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 – Production
使用Oracle Data Pump需要创建directory目录。
SQL> col directory_path for a20;
SQL> select directory_name, directory_path from dba_directories where directory_name='DMPDIR';
DIRECTORY_NAME DIRECTORY_PATH
------------------------------ --------------------
DMPDIR /upload
Query参数的官方说明。
QUERY
Predicate clause used to export a subset of a table.
For example, QUERY=employees:"WHERE department_id > 10".
2、命令行数据导出
在命令行导出过程中,三种导出模式(Schema、Tables和Full)都可以使用query参数。无论导出的数据表有多少,如果设置query条件,指定query的数据表就需要将满足条件的数据导出。
[oracle@bspdev10g upload]$ expdp userid=\"/ as sysdba\" directory=dmpdir dumpfile=scott_inin.dmp logfile=scott_inin.log tables=scott.inin query=scott.inin:\" where rownum\<10 \"
Export: Release 10.2.0.1.0 - Production on Saturday, 15 December, 2012 14:59:08
Copyright (c) 2003, 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
Starting "SYS"."SYS_EXPORT_TABLE_01": userid="/******** AS SYSDBA" directory=dmpdir dumpfile=scott_inin.dmp logfile=scott_inin.log tables=scott.inin query=scott.inin:" where rownum<10 "
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."ININ" 6.773 KB 9 rows
Master table "SYS"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYS.SYS_EXPORT_TABLE_01 is:
/upload/scott_inin.dmp
Job "SYS"."SYS_EXPORT_TABLE_01" successfully completed at 14:59:19
在导出表模式的时候,我们可以将数据表在query中指定出来。如果数据表导出的只有一个,query中的数据表名称可以省略。
[oracle@bspdev10g upload]$ expdp userid=\"/ as sysdba\" directory=dmpdir dumpfile=scott_inin.dmp logfile=scott_inin.log tables=scott.inin query=\" where rownum\<10 \"
Export: Release 10.2.0.1.0 - Production on Saturday, 15 December, 2012 15:00:24
Copyright (c) 2003, 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
Starting "SYS"."SYS_EXPORT_TABLE_01": userid="/******** AS SYSDBA" directory=dmpdir dumpfile=scott_inin.dmp logfile=scott_inin.log tables=scott.inin query=" where rownum<10 "
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."ININ" 6.773 KB 9 rows
Master table "SYS"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYS.SYS_EXPORT_TABLE_01 is:
/upload/scott_inin.dmp
Job "SYS"."SYS_EXPORT_TABLE_01" successfully completed at 15:00:34
Schema模式导出对象的时候,query字句也是可以使用的。注意:query没有指定的数据表也是可以导出的,而且是无条件的导出。
[oracle@bspdev10g upload]$ expdp userid=\"/ as sysdba\" directory=dmpdir dumpfile=scott_inin.dmp logfile=scott_inin.log schemas=scott query=scott.emp:\" where rownum\<10 \",scott.dept:\" where rownum\<10 \"
Export: Release 10.2.0.1.0 - Production on Saturday, 15 December, 2012 15:02:01
Copyright (c) 2003, 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
Starting "SYS"."SYS_EXPORT_SCHEMA_01": userid="/******** AS SYSDBA" directory=dmpdir dumpfile=scott_inin.dmp logfile=scott_inin.log schemas=scott query=scott.emp:" where rownum<10 ",scott.dept:" where rownum<10 "
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 256 KB
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."DEPT" 5.656 KB 4 rows
. . exported "SCOTT"."EMP" 7.625 KB 9 rows
. . exported "SCOTT"."ININ" 34.98 KB 760 rows
. . exported "SCOTT"."SALGRADE" 5.585 KB 5 rows
. . exported "SCOTT"."BONUS" 0 KB 0 rows
Master table "SYS"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYS.SYS_EXPORT_SCHEMA_01 is:
/upload/scott_inin.dmp
Job "SYS"."SYS_EXPORT_SCHEMA_01" successfully completed at 15:02:15
注意上面的细节:我们虽然设置的是schema导出模式,但是在query中设置条件的数据表,的确是按照条件类型进行导出。
最后,我们需要关注command line导出方式的一个重要要求,就是转义字符。在userid和query参数中,我们都可以看到转义字符的踪迹。在命令行方式下,我们需要对引号、大小比较符进行转移处理。很多时候,这样的命令和官方提示中存在一些差异,需要我们额外关注。
3、参数文件使用
当我们导出的条件很复杂、参数很多的时候,命令行参数不是什么很方便的选择。我们可以使用参数文件parameter file方法。借助parfile参数,我们可以将导出参数组织成一个文本格式文件。
[oracle@bspdev10g upload]$ ls -l
total 4
-rw-r--r-- 1 oracle oinstall 142 Dec 15 15:08 expdp_q.par
[oracle@bspdev10g upload]$ cat expdp_q.par
directory=dmpdir
dumpfile=exp_scott.dmp
logfile=exp_scott.log
schemas=scott
query=scott.emp:"where rownum<10 ", scott.inin:"where rownum<10"
在目录中,我们可以定义参数文件,其中包括了各个参数取值。执行导出命令:
[oracle@bspdev10g upload]$ expdp userid=\"/ as sysdba\" parfile=expdp_q.par
Export: Release 10.2.0.1.0 - Production on Saturday, 15 December, 2012 15:10:12
Copyright (c) 2003, 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
Starting "SYS"."SYS_EXPORT_SCHEMA_01": userid="/******** AS SYSDBA" parfile=expdp_q.par
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 256 KB
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."DEPT" 5.656 KB 4 rows
. . exported "SCOTT"."EMP" 7.625 KB 9 rows
. . exported "SCOTT"."ININ" 6.773 KB 9 rows
. . exported "SCOTT"."SALGRADE" 5.585 KB 5 rows
. . exported "SCOTT"."BONUS" 0 KB 0 rows
Master table "SYS"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYS.SYS_EXPORT_SCHEMA_01 is:
/upload/exp_scott.dmp
Job "SYS"."SYS_EXPORT_SCHEMA_01" successfully completed at 15:10:25
导出成功。使用参数文件的一个重要好处是不需要进行转义字符处理,直接使用就可以了。
4、结论
Query参数是exp/imp和Data Pump中很好使用的一个参数类型,本文记录下这种使用方法,权当记录。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/17203031/viewspace-751176/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/17203031/viewspace-751176/