一、介绍

        数据泵(expdp,impdp)是Oracle 10g时引入的新技术,兼容了之前的数据导出导入工具(exp,imp)大部分功能,并进一步完善,提供了很多新功能以满足复杂的业务需求。区别于传统的exp,imp工具,数据泵相关命令需在数据库服务端执行。

        数据泵属于逻辑迁移,可跨操作系统版本,跨数据库版本。高版本兼容低版本,高版本向低版本导数据,导出时需添加低版本的版本号。

二、使用

环境:

操作系统版本+数据库版本 +数据库字符集

redhat 5.6+Oracle 10.2.0.5 +AMERICAN_AMERICA.ZHS16GBK

windows2008+Oracle11.2.0.4+SIMPLIFIED CHINESE_CHINA.ZHS16GBK

导数据之间的数据库字符集必须一致,否则可能出现乱码。

字符集查询命令

select userenv('language') from dual;

2.1 导出导入目录创建

--创建导出目录

create directory expdp as '/home/oracle/expdp';

--创建导入目录

create directory impdp as 'D:\impdp';

--查询创建的目录

set pagesize 200

set line 200

col DIRECTORY_PATH for a60

col OWNER for a10

select * from dba_directories;

--授权导数据用户(只是适用于导本用户下的对象,导出其他用户对象需有dba权限,用户有dba权限可跳过此步)

grant read,write on directory expdp to syd;

grant read,write on directory impdp to scott;

2.2 按表导出导入

--导出

expdp syd/oracle dumpfile=expdp_test_20200811.dmp directory=expdp tables=syd.test logfile=expdp_test_20200811.log exclude=statistics

--导入(与源端用户,表空间相同时)

impdp syd/oracle dumpfile=expdp_test_20200811.dmp directory=impdp logfile=expdp_test_20200811.log

--导入(与源端用户,表空间不同时)

impdp scott/oracle dumpfile=expdp_test_20200811.dmp directory=impdp remap_schema=syd:scott remap_tablespace=syd:users logfile=expdp_test_20200811.log

2.3 按用户导出导入

--导出

expdp syd/oracle dumpfile=expdp_sydt%U_20200811.dmp directory=expdp schemas=syd parallel=2 logfile=expdp_syd_20200811.log exclude=statistics

--导入(与源端用户,表空间相同时)

impdp syd/oracle dumpfile=expdp_sydt%U_20200811.dmp directory=impdp parallel=2 logfile=impdp_syd_20200811.log

--导入(与源端用户,表空间不同时)

impdp scott/oracle dumpfile=expdp_sydt%U_20200811.dmp directory=impdp remap_schema=syd:scott remap_tablespace=syd:users parallel=2 logfile=impdp_syd_20200811.log

2.4 只导表结构

导出语句中添加content=metadata_only参数即可

--按表导出

expdp syd/oracle dumpfile=expdp_test_20200811.dmp directory=expdp tables=syd.test logfile=expdp_test_20200811.log content=metadata_only exclude=statistics

--按照用户导出

expdp syd/oracle dumpfile=expdp_sydt%U_20200811.dmp directory=expdp schemas=syd parallel=2 logfile=expdp_syd_20200811.log content=metadata_only exclude=statistics

--按表导入

impdp syd/oracle dumpfile=expdp_test_20200811.dmp directory=impdp logfile=expdp_test_20200811.log

--按用户导入

impdp syd/oracle dumpfile=expdp_sydt%U_20200811.dmp directory=impdp parallel=2 logfile=impdp_syd_20200811.log

2.5 高版本向低版本数据库导数据

 

 

导出语句中添加version=10.2.0.5参数即可(低版本数据库的版本号)

--导出

expdp scott/oracle dumpfile=expdp_emp_20200811.dmp directory=impdp tables=scott.emp logfile=expdp_emp_20200811.log version=10.2.0.5 exclude=statistics

--导入

impdp scott/oracle dumpfile=EXPDP_EMP_20200811.DMP directory=expdp logfile=impdp_emp_20200811.log

2.6 收集统计信息

数据库sys管理员用户运行(数据量大的表或用户需收集统计信息,否则可能影响sql执行效率)

收集表和索引统计信息

exec dbms_stats.gather_table_stats(ownname => 'SYD',tabname => 'TEST',estimate_percent => 10,method_opt=> 'for all indexed columns',cascade=>TRUE);

收集用户的统计信息

exec dbms_stats.gather_schema_stats(ownname=>'SYD',estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');

三、其他

        此外数据泵还有其他很多参数,需要用时直接添加至语句中即可。生产环境遇到exp或其他迁移工具满足不了需求时,不妨尝试搜索下数据泵,说不定就有某个参数可以满足你的需求。

--指定过滤条件 
include,exclude,query

--跨数据库链方式导出
network_link

--导入时跳过追加清空等 
table_exists_action=skip,append,truncate,replace

--参数文件
参数太多,转换符太多时可在参数文件中编写,导出时调用参数文件。

。。。。。。。。

。。。。。。。。

。。。。。。。。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐