oracle数据泵导出导入

 
    数据备份要先看看需要导出的对象大小,如果有些日志表跟业务无关占用空间很大,就可以不导出这类表从而增加数据导出的效率。

    数据泵确实很好用,但是网上看到的例子总是不是那么全,自己抽空把这些年用到的例子总结了一下,希望能帮到一些道友。


    注:如下例子中testUser是用户名testUserPwd是用户密码

一、查看数据库的大小
 
全库大小

select segment_name,segment_type, sum(bytes)/1024/1024/1024 GB from  dba_segments where segment_type in('TABLE','INDEX','TABLE PARTITION')
group by segment_name ,segment_type
order by GB desc

某个用户的大小

select segment_name,segment_type, sum(bytes)/1024/1024/1024 GB from  dba_segments where segment_type in('TABLE','INDEX','TABLE PARTITION')
and OWNER ='TESTUSER'
group by segment_name ,segment_type
order by GB desc

1、表占用空间:

select segment_name, sum(bytes)/1024/1024/1024 GB from user_segments where segment_type='TABLE' group by segment_name
order by GB desc

2、索引占用空间:

select segment_name ,sum(bytes)/1024/1024/1024 GB from user_segments where segment_type ='INDEX' group by segment_name
order by GB desc 

3、分区表TABLE PARTITION占用空间:

select segment_name,sum(bytes)/1024/1024/1024 GB   from user_segments where segment_type='TABLE PARTITION' group by segment_name
order by GB desc


二、导出、导入脚本


1、创建路径
(sqlplus里运行)

select * from dba_directories;  --查看现有的路径

create directory exp as '/mnt/share/';        --数据库里创建路径
grant read on directory  exp    to testUser;  --给testUser这个用户赋予读路径的权限
grant write on directory  exp    to testUser; --给testUser这个用户赋予写路径的权限


(linux系统下oracle用户下运行)
2、导出脚本
    普通用法
    --导出testUser用户下的所有数据  

expdp testUser/testUserPwd directory=exp dumpfile=FILE.bak schemas=testUser;  

    特殊用法
    (1)导出时排除某表EXCLUDE用法
    
 --不导出EMP和DEPT表     

 expdp testUser/testUserPwd directory=exp_dir dumpfile=datafile.bak EXCLUDE=TABLE:\"IN\(\'EMP\',\'DEPT\'\)\"

 --不导出REPORT开头的表   

 expdp testUser/testUserPwd directory=exp dumpfile=no_report.bak EXCLUDE=TABLE:\"LIKE \'REPORT%\'\"   

 --导出排除MV开头和TMP开头的表

 expdp testUser/testUserPwd dumpfile=exp logfile=/opt/out.log directory=expdir EXCLUDE=TABLE:\"LIKE \'MV%\'\", EXCLUDE=TABLE:\"LIKE \'TMP%\'\"

 --除了排除指定字母开头的表,还可以再排除特定的表

expdp testUser/testUserPwd dumpfile=exp logfile=/opt/out.log directory=expdir EXCLUDE=TABLE:\"LIKE \'MV%\'\", EXCLUDE=TABLE:\"LIKE \'TMP%\'\" ,EXCLUDE=TABLE:\"IN \(\'T1\'\,\'T2\'\)\"

    (2)只导出特定的表tables用法

expdp testUser/testUserPwd  dumpfile=exp:scott.bak tables = emp,dept

    (3)INCLUDE用法

expdp testUser/testUserPwd  DIRECTORY=exp SCHEMAS=testUser DUMPFILE=yes_report.bak  INCLUDE=TABLE:"LIKE'REPORT%'";  

3、导入脚本
   普通用法
    --导入testUser用户下的所有数据

impdp testUser/testUserPwd  dumpfile=exp:FILE.bak   

   特殊用法
   (1)只导入特殊的表    

impdp scott/tiger dumpfile=exp:scott.bak tables = emp,dept

   (2)导入时数据存在就truncate(删除)   

 impdp scott/tiger dumpfile=exp:scott.bak tables = emp table_exists_action=truncate;
    注:table_exists_action后面的参数还可以跟 append\replace\truncate\skip

(3)导入的时候更换模式、表空间以及表改名

1
2
3
4
5
6
7
8
9
10
导出指定schema的一个表,导入到一个新的schema下并重命名
--在源库中导出指定schema的一个表的数据
expdp system/oracle directory=EXPDUMP dumpfile=expdp_scott.dmp logfile=expdp_scott.log TABLES=hr.t1;
  
--通过scp或其他方式传到目标库
--将导出文件导入到一个新的schema下,并更改成新的表空间
impdp system/oracle directory=DP_DIR dumpfile=expdp_scott.dmp logfile=expdp_scott.log remap_schema=hr:SCOTT REMAP_TABLESPACE=TBSHR:TBSSCOTT
  
--将导出文件导入到一个新的schema下重命名表名,并更改成新的表空间
impdp system/oracle directory=DP_DIR dumpfile=expdp_scott.dmp logfile= expdp_scott.log remap_schema=hr:SCOTT REMAP_TABLESPACE=TBSHR:TBSSCOTT REMAP_TABLE=hr.t1:t2

 

三、 并行导出导入
 
(1)并行导出
    举 例:某用户对象占用了4G左右的空间,实际导出后的DUMP文件约为1G,我们尝试在导出该用户时指定并行度为4,设置单个文件不超过500M,则语法如下:

expdp user/pwd directory=exp dumpfile=expdp_20210430_%U.dmp logfile=expdp_20210430.log filesize=500M parallel=4

(2)并行导入
    举例:某dmp文件中包含了40张表,我们尝试在导入该DMP文件时指定并行度为4,则语法如下:

impdp user/pwd directory=exp dumpfile=expdp_20210430_%U.dmp logfile=expdp_20210430.log parallel=4

                                                      

posted @   万笑佛  阅读(308)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示