Oracle 分区表 导出导入 迁移

在Blog:分区表总结 里提到一种创建分区表的方法。使用导出导入。

Oracle分区表总结

http://blog.csdn.net/xujinyang/article/details/6832735

这种方法的步骤是:

(1)将普通表dump出来

(2)创建分区表

(3)将dump文件导入数据。

分区表的迁移和这个步骤差不多。有2点要注意:

(1)分区表导出的dump文件比普通表导出的大。

(2)导入分区表的时间要比普通表的时间要长。

补充一些exp/imp,expdp/impdp与分区表有关的知识:使用exp -help查看:

example:impscott/tiger ignore=y tables=(emp,dept) full=n

ortables=(t1:p1,t1:p2), if t1 is partitioned table

example:expscott/tiger grants=y tables=(emp,dept,mgr)

ortables=(t1:p1,t1:p2), if t1 is partitioned table

example:expdpscott/tiger dumpfile=scott.dmp directory=dmpdir schemas=scottortables=(t1:p1,t1:p2), if t1 is partitioned table

example:impdpscott/tiger directory=dmpdir dumpfile=scott.dmp

一些优化参考Blog:

exp/imp与expdp/impdp对比及使用中的一些优化事项

http://blog.csdn.net/xujinyang/article/details/6831324

迁移分区表的步骤如下:

(1)导出分区表,可以使用exp或者expdp

(2)建立新的分区表

(3)导入分区表。

A)如果是imp,加ignore=y参数,该参数会忽略创建表时的错误并继续加载数据。

B)如果是impdp,加table_exists_action=append参数.

table_exists_action:action to take if imported object already exists.valid keywords: (skip), append, replace and truncate.

示例1:使用exp/imp

分区表:

create table pdba (id, time) partition by range (time)

(partition p1 values less than (to_date('2010-10-1', 'yyyy-mm-dd')),

partition p2 values less than (to_date('2010-11-1', 'yyyy-mm-dd')),

partition p3 values less than (to_date('2010-12-1', 'yyyy-mm-dd')),

partition p4 values less than (maxvalue))

as select id, time from sys.dba;

SQL> select partition_name from user_tab_partitions where table_name='PDBA';

PARTITION_NAME

------------------------------

P1

P2

P3

P4

SQL> select count(*) from pdba partition(p1);

COUNT(*)

----------

1718285

SQL> select count(*) from pdba partition(p2);

COUNT(*)

----------

183667

SQL> select count(*) from pdba partition(p3);

COUNT(*)

----------

188701

SQL> select count(*) from pdba partition(p4);

COUNT(*)

----------

622582

SQL>

1.导出表或者某个分区

(1)导出整个表:

C:/Users/Administrator.DavidDai>exp 'sys/sys as sysdba' tables=pdba file='d:/partition.dmp' log='d:/partition.log'

Export: Release 11.2.0.1.0 - Production on星期四3月3 15:29:42 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

已导出ZHS16GBK字符集和AL16UTF16 NCHAR字符集

即将导出指定的表通过常规路径...

. .正在导出表PDBA

. .正在导出分区P1导出了1718285行

. .正在导出分区P2导出了183667行

. .正在导出分区P3导出了188701行

. .正在导出分区P4导出了622582行

成功终止导出,没有出现警告。

(2)导出某个分区:

C:/Users/Administrator.DavidDai>exp 'sys/sys as sysdba' tables=pdba:p4 file='d:/partition_p4.dmp' log='d:/partition_p4.log'

Export: Release 11.2.0.1.0 - Production on星期四3月3 15:30:09 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

已导出ZHS16GBK字符集和AL16UTF16 NCHAR字符集

即将导出指定的表通过常规路径...

. .正在导出表PDBA

. .正在导出分区P4导出了622582行

成功终止导出,没有出现警告。

说明:开始用的分区表是11g的Interval分区表,结果用exp导,报:

EXP-00006: internal inconsistency error
EXP-00000: Export terminated unsuccessfully

exp不支持11g的新特性。参考:

Exporting System or Composite Partitioned Table Using Classic Export Gives EXP-6 AND EXP-0 [ID 762774.1]

http://blog.csdn.net/xujinyang/article/details/6884215

2.创建分区表

2.1可以使用一下语句获取表的DDL语句:

SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name) FROM USER_TABLES u;

更多参考:

ORACLE使用DBMS_METADATA.GET_DDL获取DDL语句

http://blog.csdn.net/xujinyang/article/details/6830002

2.2使用imp语句获取:

C:/Users/Administrator.DavidDai>imp 'sys/sys as sysdba' tables=pdbaindexfile='D:/table.sql'file='d:/partition.dmp' ignore=y

Import: Release 11.2.0.1.0 - Production on星期四3月3 15:49:47 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

经由常规路径由EXPORT:V11.02.00创建的导出文件

已经完成ZHS16GBK字符集和AL16UTF16 NCHAR字符集中的导入

. .正在跳过分区"PDBA":"P1"

. .正在跳过分区"PDBA":"P2"

. .正在跳过分区"PDBA":"P3"

. .正在跳过分区"PDBA":"P4"

成功终止导入,没有出现警告。

这里我们在imp上加了个参数:indexfile='D:/table.sql',这条imp语句只会在对应的文件里生成分区表的ddl语句。然后编辑创建好就可以了。

最简单的方法就是使用第三方的工具,如Toad,直接就能查到表的定义语句了。

3.导入分区数据

我们在第一步导出里做了2种,一个是导出全表,另一个是导出一个分区。我们分别导入验证。

3.1导入一个分区

C:/Users/Administrator.DavidDai>imp 'sys/sys as sysdba' tables=pdba:p4 file='d:/partition_p4.dmp' ignore=y

Import: Release 11.2.0.1.0 - Production on星期四3月3 15:58:27 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

经由常规路径由EXPORT:V11.02.00创建的导出文件

已经完成ZHS16GBK字符集和AL16UTF16 NCHAR字符集中的导入

.正在将SYS的对象导入到SYS

.正在将SYS的对象导入到SYS

. .正在导入分区"PDBA":"P4"导入了622582行

成功终止导入,没有出现警告。

SQL> select count(*) from pdba partition(p4);

COUNT(*)

----------

622582

SQL> select count(*) from pdba partition(p1);

COUNT(*)

----------

0

3.2导入整个表

导入之前先把P4分区的数据truncate掉:

SQL> alter table pdba truncate partition p4;

表被截断。

SQL> select count(*) from pdba partition(p4);

COUNT(*)

----------

0

C:/Users/Administrator.DavidDai>imp 'sys/sys as sysdba' tables=pdba file='d:/partition.dmp' ignore=y

Import: Release 11.2.0.1.0 - Production on星期四3月3 16:01:08 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

经由常规路径由EXPORT:V11.02.00创建的导出文件

已经完成ZHS16GBK字符集和AL16UTF16 NCHAR字符集中的导入

.正在将SYS的对象导入到SYS

.正在将SYS的对象导入到SYS

. .正在导入分区"PDBA":"P1"导入了1718285行

. .正在导入分区"PDBA":"P2"导入了183667行

. .正在导入分区"PDBA":"P3"导入了188701行

. .正在导入分区"PDBA":"P4"导入了622582行

成功终止导入,没有出现警告。

示例2:使用expdp/impdp

1.导出dump文件

create directory dump as 'd:/backup';

grant read, write on directory dump to system;

(1)整个表

C:/Users/Administrator.DavidDai>Expdp system/system DIRECTORY=dump DUMPFILE=partition.dmp TABLES=PDBA logfile=exp.log;

Export: Release 11.2.0.1.0 - Production on星期四3月3 16:18:15 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

启动"SYSTEM"."SYS_EXPORT_TABLE_01":system/******** DIRECTORY=dump DUMPFILE=partition.dmp TABLES=PDBA logfile=exp.log;

正在使用BLOCKS方法进行估计...

处理对象类型TABLE_EXPORT/TABLE/TABLE_DATA

使用BLOCKS方法的总估计: 61 MB

处理对象类型TABLE_EXPORT/TABLE/TABLE

处理对象类型TABLE_EXPORT/TABLE/PRE_TABLE_ACTION

. .导出了"SYSTEM"."PDBA":"P1"31.12 MB 1718285行

. .导出了"SYSTEM"."PDBA":"P4"11.28 MB622582行

. .导出了"SYSTEM"."PDBA":"P3"3.422 MB188701行

. .导出了"SYSTEM"."PDBA":"P2"3.331 MB183667行

已成功加载/卸载了主表"SYSTEM"."SYS_EXPORT_TABLE_01"

******************************************************************************

SYSTEM.SYS_EXPORT_TABLE_01的转储文件集为:

D:/BACKUP/PARTITION.DMP

作业"SYSTEM"."SYS_EXPORT_TABLE_01"已于16:18:34成功完成

(2)一个分区

C:/Users/Administrator.DavidDai>Expdp system/system DIRECTORY=dump DUMPFILE=partition_p4.dmp TABLES=PDBA:P4 logfile=exp_p4.log;

Export: Release 11.2.0.1.0 - Production on星期四3月3 16:19:23 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

启动"SYSTEM"."SYS_EXPORT_TABLE_01":system/******** DIRECTORY=dump DUMPFILE=partition_p4.dmp TABLES=PDBA:P4 logfile=exp_p4.log;

正在使用BLOCKS方法进行估计...

处理对象类型TABLE_EXPORT/TABLE/TABLE_DATA

使用BLOCKS方法的总估计: 14 MB

处理对象类型TABLE_EXPORT/TABLE/TABLE

处理对象类型TABLE_EXPORT/TABLE/PRE_TABLE_ACTION

. .导出了"SYSTEM"."PDBA":"P4"11.28 MB622582行

已成功加载/卸载了主表"SYSTEM"."SYS_EXPORT_TABLE_01"

******************************************************************************

SYSTEM.SYS_EXPORT_TABLE_01的转储文件集为:

D:/BACKUP/PARTITION_P4.DMP

作业"SYSTEM"."SYS_EXPORT_TABLE_01"已于16:19:32成功完成

2.创建分区表

用dbms_metadate.get_ddl()直接获取就可以了,方法同示例1.

3.导入dump文件

(1)导入一个分区

C:/Users/Administrator.DavidDai>impdp system/system DIRECTORY=dump DUMPFILE=partition_p4.dmpTABLES=PDBA:P4logfile=imp_p4.logtable_exists_action=append

--注意这个参数,后面不用加分号,直接回车就执行了。

Import: Release 11.2.0.1.0 - Production on星期四3月3 16:24:15 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

已成功加载/卸载了主表"SYSTEM"."SYS_IMPORT_TABLE_01"

启动"SYSTEM"."SYS_IMPORT_TABLE_01":system/******** DIRECTORY=dump DUMPFILE=partition_p4.dmp TABLES=PDBA logfile=imp_p4.log table_exists_action=append

处理对象类型TABLE_EXPORT/TABLE/TABLE

ORA-39152:表"SYSTEM"."PDBA"已存在。由于附加了table_exists_action,数据将附加到现有表,但是将跳过所有相关元数据。

处理对象类型TABLE_EXPORT/TABLE/TABLE_DATA

. .导入了"SYSTEM"."PDBA":"P4"11.28 MB622582行

作业"SYSTEM"."SYS_IMPORT_TABLE_01"已经完成,但是有1个错误(于16:24:21完成)

(2)导入整个表

C:/Users/Administrator.DavidDai>impdp system/system DIRECTORY=dump DUMPFILE=partition.dmp TABLES=PDBA logfile=imp.log table_exists_action=append

Import: Release 11.2.0.1.0 - Production on星期四3月3 16:26:51 2011

Copyright (c) 1982, 2009, Oracle and/or its affiliates.All rights reserved.

连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

已成功加载/卸载了主表"SYSTEM"."SYS_IMPORT_TABLE_01"

启动"SYSTEM"."SYS_IMPORT_TABLE_01":system/******** DIRECTORY=dump DUMPFILE=partition.dmp TABLES=PDBA logfile=imp.log table_exists_action=append

处理对象类型TABLE_EXPORT/TABLE/TABLE

ORA-39152:表"SYSTEM"."PDBA"已存在。由于附加了table_exists_action,数据将附加到现有表,但是将跳过所有相关元数据。

处理对象类型TABLE_EXPORT/TABLE/TABLE_DATA

. .导入了"SYSTEM"."PDBA":"P1"31.12 MB 1718285行

. .导入了"SYSTEM"."PDBA":"P4"11.28 MB622582行

. .导入了"SYSTEM"."PDBA":"P3"3.422 MB188701行

. .导入了"SYSTEM"."PDBA":"P2"3.331 MB183667行

作业"SYSTEM"."SYS_IMPORT_TABLE_01"已经完成,但是有1个错误(于16:27:02完成)

------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值