美文网首页
数据泵导出导入

数据泵导出导入

作者: 苏水的北 | 来源:发表于2021-11-05 16:27 被阅读0次

    案例一:把主库PRR表数据导出,然后导入到测试库PRR_2021表中。这两个表均为分区表,我们想要做的就是把PRR表的Q4分区已追加方式导入到测试库PRR_2021表中,因为PRR_2021表里面有之前PRR表的历史数据,所以不能truncate掉数据。

    1、先在测试环境下检查数据库的环境:

    1.1、查看STDF用户的默认表空间、默认临时表空间:
    SQL> select username,default_tablespace,temporary_tablespace from dba_users where username = 'STDF';
    USERNAME                 DEFAULT_TABLESPACE     TEMPORARY_TABLESPACE
    ---------------------------------------- ------------------------------ ------------------------------
    STDF                     STDF_DATA_TEST         TEMP
    

    备注:发现备库的STDF下用户默认表空间与主库不同,查询到表下无数据,所以计划删除这个表空间,从新定义默认表空间。

    1.2、查询某个表空间都有哪些用户在使用:
    select * from dba_tables where tablespace_name='STDF_DATA'
    
    1.3、删除STDF_DATA_TEST表空间及对应数据文件:
    drop tablespace STDF_DATA_TEST including contents and datafiles;
    
    1.4、删除STDF用户(加了cascade就可以把用户连带的数据全部删掉):
    drop user STDF cascade;
    

    2、在测试库中搭建实验环境:

    2.1、主库表空间、用户信息查询:
    SQL> select file_name,tablespace_name from dba_data_files;
    FILE_NAME                TABLESPACE_NAME
    ---------------------------------------- ----------------------------------------
    /u02/ora_data/STDFDB/stdftdata210703.dbf     STDF_DATA
    /u02/ora_data/STDFDB/index_stdf_data210801.dbf  INDEX_STDF_DATA
    /u02/ora_data/STDFDB/data_partition211003.dbf   STDF_DATA_PARTITION1
    
    2.2、由于我们要用数据泵导出导入,所以查询后上面的表空间都是主库STDF用户下表所使用的表空间,为了不影响导入出错,我们需要给测试库创建相同表空间。

    创建STDF_DATA表空间并增加数据文件:

    create tablespace STDF_DATA datafile '/u01/app/oracle/oradata/TESTDB/stdf20211101.dbf' size 32760M autoextend on next 32m maxsize 32760M extent management local;
    
    alter tablespace STDF_DATA add datafile '/u01/app/oracle/oradata/TESTDB/STDF_DATA_20211102.dbf'  size 32700M autoextend on next 10M maxsize 32700M;
    

    创建STDF_DATA_PARTITION1表空间并增加数据文件:

    create tablespace STDF_DATA_PARTITION1 datafile '/u01/app/oracle/oradata/TESTDB/data_partition211101.dbf' size 32760M autoextend on next 32m maxsize 32760M extent management local;
    
    alter tablespace STDF_DATA_PARTITION1 add datafile '/u01/app/oracle/oradata/TESTDB/data_partition211102.dbf'  size 32700M autoextend on next 10M maxsize 32700M;
    
    alter tablespace STDF_DATA_PARTITION1 add datafile '/u01/app/oracle/oradata/TESTDB/data_partition211102.dbf'  size 32700M autoextend on next 10M maxsize 32700M;
    

    创建INDEX_STDF_DATA索引表空间并增加数据文件:

    create tablespace INDEX_STDF_DATA datafile '/u01/app/oracle/oradata/TESTDB/index_stdf_data211101.dbf ' size 32760M autoextend on next 32m maxsize 32760M extent management local;
    alter tablespace INDEX_STDF_DATA add datafile '/u01/app/oracle/oradata/TESTDB/index_stdf_data211102.dbf'  size 32700M autoextend on next 10M maxsize 32700M;
    
    2.3、创建stdf用户,并授权DBA权限:
    SQL> create user stdf identified by oracle default tablespace STDF_DATA; 
    
    User created.
    
    SQL> grant dba to stdf;
    

    3、用数据泵导出主库中2张表(PRR主表、PRR_2021历史表,均为分区表):

    备注:我们的目的是把PRR主表的数据,以分区的形式每月都做迁移,迁移至PRR_2021表中。

    3.1、用数据泵导出主库中的STDF_DATA_PRR表的Q4分区:
    [oracle@stdfdb.htkjxa.com ~]$expdp stdf/UsRS8fdp directory=dump_bak  dumpfile=STDF_DATA_PRR_Q4_211103.dmp tables=STDF_DATA_PRR:Q4
    
    Export: Release 19.0.0.0.0 - Production on Wed Nov 3 09:56:22 2021
    Version 19.10.0.0.0
    
    Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.
    
    Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    Starting "STDF"."SYS_EXPORT_TABLE_02":  stdf/******** directory=dump_bak dumpfile=STDF_DATA_PRR_Q4_211103.dmp tables=STDF_DATA_PRR:Q4 
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
    Processing object type TABLE_EXPORT/TABLE/TABLE
    Processing object type TABLE_EXPORT/TABLE/COMMENT
    Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
    . . exported "STDF"."STDF_DATA_PRR":"Q4"                 173.3 GB 888987765 rows
    Master table "STDF"."SYS_EXPORT_TABLE_02" successfully loaded/unloaded
    ******************************************************************************
    Dump file set for STDF.SYS_EXPORT_TABLE_02 is:
      /u01/dump_bak/STDF_DATA_PRR_Q4_211103.dmp
    Job "STDF"."SYS_EXPORT_TABLE_02" successfully completed at Wed Nov 3 10:14:35 2021 elapsed 0 00:18:10
    
    3.2、用数据泵导出主库中的STDF_DATA_PRR_2021表:
    [oracle@stdfdb.htkjxa.com ~]$expdp stdf/UsRS8fdp directory=dump_bak  dumpfile=STDF_DATA_PRR_2021_211103.dmp tables=STDF_DATA_PRR_2021
    
    Export: Release 19.0.0.0.0 - Production on Wed Nov 3 09:15:37 2021
    Version 19.10.0.0.0
    
    Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.
    
    Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    Starting "STDF"."SYS_EXPORT_TABLE_02":  stdf/******** directory=dump_bak dumpfile=STDF_DATA_PRR_2021_211103.dmp tables=STDF_DATA_PRR_2021 
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
    Processing object type TABLE_EXPORT/TABLE/TABLE
    Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202107"       1.774 GB 14206176 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202108"       55.12 GB 125154631 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202109"       137.1 GB 597688477 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202110"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202111"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202112"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202201"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202202"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202203"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202204"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202205"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202206"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202207"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202208"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202209"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202210"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202211"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q202212"           0 KB       0 rows
    . . exported "STDF"."STDF_DATA_PRR_2021":"Q_OTHERS"          0 KB       0 rows
    Master table "STDF"."SYS_EXPORT_TABLE_02" successfully loaded/unloaded
    ******************************************************************************
    Dump file set for STDF.SYS_EXPORT_TABLE_02 is:
      /u01/dump_bak/STDF_DATA_PRR_2021_211103.dmp
    Job "STDF"."SYS_EXPORT_TABLE_02" successfully completed at Wed Nov 3 09:41:31 2021 elapsed 0 00:25:49
    

    4、把数据泵导出的2张表传输到备库下(实验环境),然后分别导入:

    4.1、由于导入的表都是9亿行左右,所以保险起见,先扩容undo表空间:
    alter tablespace undotbs1 add datafile '/u01/app/oracle/oradata/TESTDB/undotbs02.dbf' size 32700M autoextend on next 128M maxsize 32700M;
    alter tablespace undotbs1 add datafile '/u01/app/oracle/oradata/TESTDB/undotbs03.dbf' size 32700M autoextend on next 128M maxsize 32700M;
    
    4.2、先导入STDF_DATA_PRR_2021表:

    备注:此表为分区表也建立了分区索引,此表之前已经包含了主表STDF_DATA_PRR前3个月的历史数据。

    [oracle@STDFTestDB dump_bak]$ impdp stdf/oracle directory=dump_bak  dumpfile=STDF_DATA_PRR_2021_211103.dmp  tables=STDF_DATA_PRR_2021 logfile=stdf_test.log cluster=no
    
    Import: Release 19.0.0.0.0 - Production on Thu Nov 4 11:32:10 2021
    Version 19.3.0.0.0
    
    Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.
    
    Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    Master table "STDF"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
    import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
    export done in ZHS16GBK character set and AL16UTF16 NCHAR character set
    Warning: possible data loss in character set conversions
    Starting "STDF"."SYS_IMPORT_TABLE_01":  stdf/******** directory=dump_bak dumpfile=STDF_DATA_PRR_2021_211103.dmp tables=STDF_DATA_PRR_2021 logfile=stdf_test.log cluster=no 
    Processing object type TABLE_EXPORT/TABLE/TABLE
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202107"       1.774 GB 14206176 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202108"       55.12 GB 125154631 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202109"       137.1 GB 597688477 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202110"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202111"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202112"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202201"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202202"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202203"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202204"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202205"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202206"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202207"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202208"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202209"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202210"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202211"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q202212"           0 KB       0 rows
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q_OTHERS"          0 KB       0 rows
    Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
    Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
    Job "STDF"."SYS_IMPORT_TABLE_01" completed with 3 error(s) at Thu Nov 4 17:17:43 2021 elapsed 0 05:45:29
    
    4.3、再导入STDF_DATA_PRR_Q4_211103.dmp文件:

    备注:因为这个STDF_DATA_PRR_Q4_211103.dmp文件,他是STDF_DATA_PRR表的数据,属于STDF_DATA表空间。我们现在的实验目的是:把他的数据灌入STDF_DATA_PRR_2021表空,还不能清除了STDF_DATA_PRR_2021表之前的历史数据。所以我们在导入过程中就需要更换表名称,更换表空间名称,采用追加的方式导入。

    [oracle@STDFTestDB dump_bak]$ impdp stdf/oracle directory=dump_bak  dumpfile=STDF_DATA_PRR_Q4_211103.dmp   logfile=stdf_test1.log cluster=no  REMAP_TABLESPACE=STDF_DATA:STDF_DATA_PARTITION1 REMAP_TABLE=STDF_DATA_PRR:STDF_DATA_PRR_2021  table_exists_action=append
    
    Import: Release 19.0.0.0.0 - Production on Fri Nov 5 09:41:34 2021
    Version 19.3.0.0.0
    
    Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.
    
    Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    Master table "STDF"."SYS_IMPORT_FULL_02" successfully loaded/unloaded
    import done in AL32UTF8 character set and AL16UTF16 NCHAR character set
    export done in ZHS16GBK character set and AL16UTF16 NCHAR character set
    Warning: possible data loss in character set conversions
    Starting "STDF"."SYS_IMPORT_FULL_02":  stdf/******** directory=dump_bak dumpfile=STDF_DATA_PRR_Q4_211103.dmp logfile=stdf_test1.log cluster=no REMAP_TABLESPACE=STDF_DATA:STDF_DATA_PARTITION1 REMAP_TABLE=STDF_DATA_PRR:STDF_DATA_PRR_2021 table_exists_action=append 
    Processing object type TABLE_EXPORT/TABLE/TABLE
    Table "STDF"."STDF_DATA_PRR_2021" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    . . imported "STDF"."STDF_DATA_PRR_2021":"Q4"            173.3 GB 888987765 rows
    Processing object type TABLE_EXPORT/TABLE/COMMENT
    Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
    Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
    Job "STDF"."SYS_IMPORT_FULL_02" successfully completed at Fri Nov 5 15:38:37 2021 elapsed 0 05:56:55
    

    impdp语法举例:

    --将导出文件导入到一个新的schema下,并更改成新的表空间
    impdp system/oracle directory=DP_DIR dumpfile=expdp_scott_1017.dmp logfile= t_expdp_scott_1017.log remap_schema=hr:SCOTT REMAP_TABLESPACE=TBS_HR:TBS_SCOTT
    
    
    --将导出文件导入到一个新的schema下重命名表名,并更改成新的表空间
    impdp system/oracle directory=DP_DIR dumpfile=expdp_scott_1017.dmp logfile= t_expdp_scott_1017.log remap_schema=hr:SCOTT REMAP_TABLESPACE=TBS_HR:TBS_SCOTT REMAP_TABLE=hr.t_A:t_BBB
    
    --将导出文件导入到一个新的schema下重命名表名,并更改成新的表空间,并采用追加方式导入
    impdp system/oracle directory=DP_DIR dumpfile=expdp_scott_1017.dmp logfile= t_expdp_scott_1017.log remap_schema=hr:SCOTT REMAP_TABLESPACE=TBS_HR:TBS_SCOTT REMAP_TABLE=hr.t_A:t_BBB table_exists_action=append
    

    table_exists_action参数说明:

    table_exists_action=skip/append/replace/truncate
    impdp恢复数据库的时候有一个table_exists_action选项,其中
    skip是跳过————默认就是该选项
    append追加
    replace覆盖,不释放表空间
    truncate先清空表释放表空间,然后执行恢复
    

    相关文章

      网友评论

          本文标题:数据泵导出导入

          本文链接:https://www.haomeiwen.com/subject/tqwqzltx.html