现在是一个信息时代,数据规模和数据量的增长以爆炸式的速度扩张。之前几百M或数G规模的数据量都堪称庞大。
现如今测试系统所占空间都是这一数据的几十倍甚至百倍。原生imp/exp工就这两匹老马在处理这么大的数据量就力不从心了。
从10g开始,data pump横空出世,中文名叫数据泵。虽然oracle对自己产品的宣传一向有夸大的传统,
不过大家要理解,有了前面原生的exp/imp工具的铺垫,对比来看,数据泵的导入和导出,有了很大的提升。
关于 数据泵的好处,请大家自行百度一下。
data pump 导入导出工具是一个服务器端的工具,它是通过调用服务器端的data pump api的方式实现数据加载或卸载,
也就是说该工具一般都是在目标服务器上执行,导出或导入本地的对旬
(唯一的例外是通过netwkr_link参数,能够处理远端数据,需要结合本地的dblink使用,自己百度)
要使用data pump工具,要指定一个directory对象。
什么是directory?
字面意思就是目录,这个上当不是实体,只是一个指向,指向操作系统中的一个具体路径。
每个directory对象都有read、write两个权限,通过grant授权给指定用户。
拥有directory对象的read/write权限的用户就可以读/写该directory对象实际指定的操作系统路径下的文件。
即使dba在客户端执行data pump,文件最终也是生成服务器端,指定directory对象对应操作系统路径下,
而不是像imp/exp工具那样,将文件保存于执行imp/exp的机器上。
创建目录【操作系统层面要有此路径】
mkdir /home/oracle/expdp
sqlplus / as sysdba
SQL> create directory expdp as ‘/home/oracle/expdp‘;
SQL> grant read,write on directory expdp to scott;
select OWNER||‘,‘||DIRECTORY_NAME||‘,‘||DIRECTORY_PATH from dba_directories; 【查看所有目录】
1、导出某用户下所有对象
expdp scott/lipengfei directory=expdp dumpfile=scott_all.dmp SCHEMAS=SCOTT logfile=scott_all.log
2、导出部分表
expdp scott/lipengfei directory=expdp dumpfile=scott_emp.dmp tables=\(emp,dept\) logfile=scott_emp.log
create table e1 as select * from emp;
create table e2 as select * from emp;
create table d1 as select * from dept;
create table d2 as select * from dept;
expdp scott/lipengfei directory=expdp dumpfile=scott_E_D.dmp tables=\(scott.E%,scott.D%\);
3、指定条件导出
expdp scott/lipengfei directory=expdp dumpfile=scott_emp.dmp logfile=scott_emp.log tables=emp query=\"where sal \>1000\"
4、导出时除某对象【静态收集信息,序列,视图,表】
expdp scott/lipengfei exclude=STATISTICS,SEQUENCE,VIEW,TABLE:\" IN \(\‘EMP\‘,\‘DEPT\‘\)\" directory=expdp dumpfile=scott_2015_06_02.dmp logfile=scott_2015_06_02.log
5、导出同时压缩dmp文件(compression=ALL 11g中才有)
create table li nologging as select * from all_objects;
insert into li select * from li;
/
/
/
/
/
commit;
select segment_name,bytes/1024/1024 from user_segments;
expdp scott/lipengfei directory=EXPDP dumpfile=scott_all_compression.dmp SCHEMAS=SCOTT logfile=scott_all_compression.log compression=ALL
expdp scott/lipengfei directory=EXPDP dumpfile=scott_all.dmp SCHEMAS=SCOTT logfile=scott_all.log
ls -lh *.dmp 【官方说,理想状态压缩比率最高可以达到90%】
zip scott_all_compression.zip scott_all_compression.dmp 【第二次压缩】
6、content为all 时,将导出对象定义及其所有数据.为data_only时,只导出对象数据,为metadata_only时,只导出对象定义
expdp scott/lipengfei directory=EXPDP dumpfile=scott_metadata_only.dmp content=metadata_only logfile=scott_metadata_only.log
ls -lh *.dmp
---------------------------------------
1、导入某用户所有对象
sqlplus / as sysdba
SQL> create tablespace lipengfei datafile ‘/home/oracle/app/oracle/oradata/ecom/lipengfei.dbf‘ size 100M AUTOEXTEND OFF;
SQL> create user lipengfei identified by lipengfei default tablespace lipengfei;
SQL> alter user lipengfei account unlock;
SQL> grant connect,resource to lipengfei;
SQL> grant create table to lipengfei;
SQL> grant create view to lipengfei;
SQL> grant read, write on directory EXPDP to lipengfei ;
sqlplus lipengfei/lipengfei
create table hehe(a int,b varchar2(10));
insert into hehe values(2,‘d‘);
insert into hehe values(4,‘e‘);
insert into hehe values(6,‘f‘);
commit;
create view nimei as select a from hehe;
create table haha(id int);
insert into haha values(1);
commit;
expdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp SCHEMAS=LIPENGFEI logfile=lipengfei_all.log
sqlplus lipengfei/lipengfei
drop view nimei;
drop table hehe;
drop table haha;
impdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp logfile=lipengfei_all.log
2、导入的对象已存在
当使用IMPDP完成数据库导入时,如遇到表已存在时,Oracle提供给我们如下四种处理方式:
a.忽略(SKIP,默认行为);
b.在原有数据基础上继续增加(APPEND);
c.先DROP表,然后创建表,最后完成数据插入(REPLACE);
d.先TRUNCATE,再完成数据插入(TRUNCATE)。
sqlplus lipengfei/lipengfei
delete haha;
insert into haha values(6);
insert into haha values(66);
commit;
select * from haha;
impdp lipengfei/lipengfei directory=expdp dumpfile=lipengfei_all.dmp TABLE_EXISTS_ACTION=TRUNCATE logfile=lipengfei_all.log
3、lipengfei用户数据 导入 shiqiang用户
sqlplus / as sysdba
SQL> create tablespace shiqiang datafile ‘/home/oracle/app/oracle/oradata/ecom/shiqiang.dbf‘ size 100M AUTOEXTEND OFF;
SQL> create user shiqiang identified by shiqiang default tablespace shiqiang;
SQL> alter user shiqiang account unlock;
SQL> grant connect,resource to shiqiang;
SQL> grant create table to shiqiang;
SQL> grant create view to shiqiang;
SQL> grant read, write on directory EXPDP to shiqiang ;
impdp shiqiang/shiqiang directory=expdp remap_schema=lipengfei:shiqiang remap_tablespace=lipengfei:shiqiang dumpfile=lipengfei_all.dmp logfile=lipengfei_shiqiang.log ;
4、只导入部分表
sqlplus lipengfei/lipengfei
drop view nimei;
drop table hehe;
drop table haha;
impdp lipengfei/lipengfei directory=expdp tables=haha dumpfile=lipengfei_all.dmp logfile=lipengfei_only_haha.log
sqlplus lipengfei/lipengfei
select * from hehe;
select * from haha;
5、高版本导入低版本
11g导出
sqlplus shiqiang/shiqiang
SQL> select tname from tab;
expdp shiqiang/shiqiang directory=expdp dumpfile=shiqiang_11g_all.dmp SCHEMAS=SHIQIANG logfile=shiqiang_11g_all.log version=10.2.0.1.0
lcd d:\
get /home/oracle/expdp/shiqiang_11g_all.dmp
10g导入
SQL> create tablespace shiqiang datafile ‘/oracle/app/oradata/ecom/shiqiang.dbf‘ size 100M AUTOEXTEND OFF;
SQL> create user shiqiang identified by shiqiang default tablespace shiqiang;
SQL> alter user shiqiang account unlock;
SQL> grant connect,resource to shiqiang;
SQL> grant create table to shiqiang;
SQL> grant create view to shiqiang;
[[email protected] ~]$ mkdir /home/oracle/expdp
SQL> create or replace directory EXPDP as ‘/home/oracle/expdp‘;
SQL> grant read, write on directory EXPDP to shiqiang ;
lcd d:\
cd /home/oracle/expdp
put shiqiang_11g_all.dmp
impdp shiqiang/shiqiang directory=expdp dumpfile=shiqiang_11g_all.dmp logfile=shiqiang_11g_all.log