Oracle 分区表 导出导入 迁移

    技术2022-05-19  27

     

     

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

           Oracle 分区表 总结

           http://blog.csdn.net/tianlesoftware/archive/2009/10/23/4717318.aspx

     

    这种方法的步骤是:

    1)将普通表dump出来

    2)创建分区表

    3)将dump 文件导入数据。

     

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

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

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

     

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

     

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

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

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

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

     

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

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

     

    一些优化参考Blog

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

    http://www.cndba.cn/Dave/article/1428

     

     

    迁移分区表的步骤如下:

    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

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

     

    说明:开始用的分区表是11gInterval 分区表,结果用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/tianlesoftware/archive/2011/03/03/6220799.aspx

     

    2. 创建分区表

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

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

     

    更多参考:

    ORACLE 使用DBMS_METADATA.GET_DDL获取DDL语句

    http://www.cndba.cn/Dave/article/1231

     

    2.2 使用imp 语句获取:

    C:/Users/Administrator.DavidDai>imp 'sys/sys as sysdba' tables=pdba indexfile='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 MB  622582

    . . 导出了 "SYSTEM"."PDBA":"P3"                        3.422 MB  188701

    . . 导出了 "SYSTEM"."PDBA":"P2"                        3.331 MB  183667

    已成功加载/卸载了主表 "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 MB  622582

    已成功加载/卸载了主表 "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.dmp TABLES=PDBA:P4 logfile=imp_p4.log table_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 MB  622582

    作业 "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 MB  622582

    . . 导入了 "SYSTEM"."PDBA":"P3"                        3.422 MB  188701

    . . 导入了 "SYSTEM"."PDBA":"P2"                        3.331 MB  183667

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

     

     

     

     

     

     

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

    QQ: 492913789

    Email:ahdba@qq.com

    Blog: http://www.cndba.cn/dave

    DBA1 群:62697716(); DBA2 群:62697977()

    DBA3 群:62697850   DBA 超级群:63306533;    

    聊天 群:40132017

    --加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请


    最新回复(0)