【数据泵】EXPDP导出表结构(真实案例)
BLOG文档结构图
因工作需要现需要把一个生产库下的元数据(表定义,索引定义,函数定义,包定义,存储过程)导出到测试库上,本来以为很简单的,可是做的过程发现很多的问题,现记录如下,希望有同样需要的朋友不要再走弯路了。
导读
各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~:
① EXPDP和IMPDP如何导出导入元数据,包括表定义,索引定义,函数定义,包定义,存储过程(重点)
② 表的初始化定义参数initial,及如何批量修改该参数
③ 如何导出DMP文件中的DDL语句(重点)
④ 10g和11g默认情况下有哪些用户及其作用
⑤ linux中的批量替换sed命令
⑥ sqlldr和spool命令
本文如有错误或不完善的地方请大家多多指正,ITPUB留言或QQ皆可,您的批评指正是我写作的最大动力。
实验环境介绍
源库:10.2.0.1 AIX
目标库:11.2.0.3 RHEL6.5
执行导出工作
确定需要导出的用户
oracle安装好后有很多的系统默认用户,比如sys和system,对于这2个用户里的元数据我们就没有必要再重新导出嘛,不然导入的时候还提示错误,看着实在不好。
官网信息:
All databases created by the Database Configuration Assistant (DBCA) include the SYS, SYSTEM, SYSMAN, and DBSNMP database accounts. In addition, Oracle Database provides several other administrative accounts. Before using these accounts, you must unlock them and reset their passwords.
11g默认用户比较多:
User Name | Description | See Also |
ANONYMOUS | Enables HTTP access to Oracle XML DB. | |
APEX_030200 | The account owns the Application Express schema and metadata. | |
APEX_PUBLIC_USER | The minimally privileged account used for Application Express configuration with Oracle HTTP Server and mod_plsql. | |
APPQOSSYS | Used for storing and managing all data and metadata required by Oracle Quality of Service Management. | None |
BI | The account that owns the Business Intelligence schema included in the Oracle Sample Schemas. It is available only if you loaded the sample schemas. | |
CTXSYS | The Oracle Text account. | |
DBSNMP | The account used by the Management Agent component of Oracle Enterprise Manager to monitor and manage the database. | Oracle Enterprise Manager Grid Control Installation and Basic Configuration |
DIP | The account used by the Directory Integration Platform (DIP) to synchronize the changes in Oracle Internet Directory with the applications in the database. | None |
DVSYS | There are two roles associated with this account. The Database Vault owner role manages the Database Vault roles and configurations. The Database Vault Account Manager is used to manage database user accounts. | |
Note: Part of Oracle Database Vault user interface text is stored in database tables in the DVSYS schema. By default, only the English language is loaded into these tables. You can use Oracle Database Vault Configuration Assistant to add more languages to Oracle Database Vault. For the necessary steps, see Appendix C in Oracle Database Vault Administrator's Guide | ||
EXFSYS | The account owns the Expression Filter schema. | None |
FLOWS_FILES | The account owns the Application Express uploaded files. | |
HR | The account that owns the Human Resources schema included in the Oracle Sample Schemas. It is available only if you loaded the sample schemas. | |
IX | The account that owns the Information Transport schema included in the Oracle Sample Schemas. It is available only if you loaded the sample schemas. | |
LBACSYS | The Oracle Label Security administrator account. | |
MDDATA | The schema used by Oracle Spatial for storing geocoder and router data. | |
MDSYS | The Oracle Spatial and Oracle Multimedia Locator administrator account. | |
MGMT_VIEW | An account used by Oracle Enterprise Manager Database Control. | None |
OE | The account that owns the Order Entry schema included in the Oracle Sample Schemas. It is available only if you loaded the sample schemas. | |
ORDPLUGINS | The Oracle Multimedia user. Plug-ins supplied by Oracle and third-party plug-ins are installed in this schema. | |
ORDSYS | The Oracle Multimedia administrator account. | |
ORDDATA | This account contains the Oracle Multimedia DICOM data model. | |
OUTLN | The account that supports plan stability. Plan stability enables you to maintain the same execution plans for the same SQL statements. OUTLN acts as a role to centrally manage metadata associated with stored outlines. | |
ORACLE_OCM | This account contains the instrumentation for configuration collection used by the Oracle Configuration Manager. | Oracle Configuration Manager Installation and Administration Guide |
OWBSYS | The account used by Oracle Warehouse Builder as its default repository. You must unlock this account after installing the Oracle Database and before launching the Warehouse Builder Repository Assistant. | Oracle Warehouse Builder Installation and Administration Guide |
OWBSYS_AUDIT | This account is used by the Warehouse Builder Control Center Agent to access the heterogeneous execution audit tables in the OWBSYS schema. | Oracle Warehouse Builder Installation and Administration Guide |
PM | The account that owns the Product Media schema included in the Oracle Sample Schemas. It is available only if you loaded the sample schemas. | |
SCOTT | An account used by Oracle sample programs and examples. | |
SH | The account that owns the Sales History schema included in the Oracle Sample Schemas. It is available only if you loaded the sample schemas during an Enterprise Edition installation. | |
SI_INFORMTN_SCHEMA | The account that stores the information views for the SQL/MM Still Image Standard. | |
SPATIAL_CSW_ADMIN_USR | The Catalog Services for the Web (CSW) account. It is used by the Oracle Spatial CSW cache manager to load all record type metadata, and record instances from the database into the main memory for the record types that are cached. | |
SPATIAL_WFS_ADMIN_USR | The Web Feature Service (WFS) account. It is used by the Oracle Spatial WFS cache manager to load all feature-type metadata, and feature instances from the database into main memory for the feature types that are cached. | |
SYS | The account used to perform database administration tasks. | |
SYSMAN | The account used to perform Oracle Enterprise Manager database administration tasks. | Oracle Enterprise Manager Grid Control Installation and Basic Configuration |
SYSTEM | Another account used to perform database administration tasks. | |
WMSYS | The account used to store the metadata information for Oracle Workspace Manager. | |
XDB | The account used for storing Oracle XML DB data and metadata. |
10g下比较少:
CTXSYS | CTXSYS | The Oracle Text account | |
DBSNMP | DBSNMP | The account used by the Management Agent component of Oracle Enterprise Manager to monitor and manage the database | Oracle Enterprise Manager Grid Control Installation and Basic Configuration |
LBACSYS | LBACSYS | The Oracle Label Security administrator account | |
MDDATA | MDDATA | The schema used by Oracle Spatial for storing Geocoder and router data | |
MDSYS | MDSYS | The Oracle Spatial and Oracle interMedia Locator administrator account | |
DMSYS | DMSYS | The Oracle Data Mining account. | |
OLAPSYS | MANAGER | The account used to create OLAP metadata structures. It owns the OLAP Catalog (CWMLite). | |
ORDPLUGINS | ORDPLUGINS | The Oracle interMedia user. Plug-ins supplied by Oracle and third party format plug-ins are installed in this schema. | |
ORDSYS | ORDSYS | The Oracle interMedia administrator account | |
OUTLN | OUTLN | The account that supports plan stability. Plan stability enables you to maintain the same execution plans for the same SQL statements. OUTLN acts as a role to centrally manage metadata associated with stored outlines. | |
SI_INFORMTN_SCHEMA | SI_INFORMTN_SCHEMA | The account that stores the information views for the SQL/MM Still Image Standard | |
SYS | CHANGE_ON_INSTALL | The account used to perform database administration tasks | |
SYSMAN | CHANGE_ON_INSTALL | The account used to perform Oracle Enterprise Manager database administration tasks. Note that SYS and SYSTEM can also perform these tasks. | Oracle Enterprise Manager Grid Control Installation and Basic Configuration |
SYSTEM | MANAGER | Another account used to perform database administration tasks. |
确定需要导出的用户在哪些表空间,及其表初始化时占用的表空间大小
SELECT D.tablespace_name, SUM(D.initial_extent)/1024/1024 initial_extent
FROM DBA_SEGMENTS D
WHERE D.owner IN
(SELECT a.username
FROM DBA_USERS A
WHERE A.account_status = 'OPEN'
AND A.username NOT IN
('SYS', 'SYSTEM', 'MGMT_VIEW', 'SYSMAN', 'DBSNMP'))
GROUP BY D.tablespace_name
ORDER BY initial_extent desc ;
SELECT SUM(D.initial_extent)/1024/1024 initial_extent
FROM DBA_SEGMENTS D
WHERE D.owner IN
(SELECT a.username
FROM DBA_USERS A
WHERE A.account_status = 'OPEN'
AND A.username NOT IN
('SYS', 'SYSTEM', 'MGMT_VIEW', 'SYSMAN', 'DBSNMP'))
;
由此可以知道,创建这些元数据大约需要49G的空间,如果涉及到数据的话,还需要判断数据占用空间,这里一定需要判断这个,不然执行导入的时候会因为表空间不足而不能导入,我第一次导入的时候就是因为这里没有判断导致花费了很长的时间,我一直扩展表空间,但是就是就表空间不足的错误(ORA-01659),想想建表不会花这么大的空间的吧,最后查看了表的定义才知道,原来表初始化的时候就很大,这个问题后边还需要再处理一下的,不然测试库没法导入,当然存储够的话就另当别论了。
ORA-39171: Job is experiencing a resumable wait.
ORA-01659: unable to allocate MINEXTENTS beyond 4 in tablespace DWII_SOR_F_01
确定需要导出的用户中有哪些无效的对象、及总共需要导出的对象数量
这一步也很重要,决定着最终导出结果的正确性验证。
SELECT d.OWNER,count(1)
FROM dba_objects d
WHERE d.OWNER in (SELECT a.username
FROM DBA_USERS A
WHERE A.account_status = 'OPEN'
AND A.username NOT IN
('SYS', 'SYSTEM', 'MGMT_VIEW', 'SYSMAN', 'DBSNMP'))
group by d.OWNER;
SELECT d.OWNER,
d.status,count(1)
FROM dba_objects d
WHERE d.OWNER in (SELECT a.username
FROM DBA_USERS A
WHERE A.account_status = 'OPEN'
AND A.username NOT IN
('SYS', 'SYSTEM', 'MGMT_VIEW', 'SYSMAN', 'DBSNMP'))
group by d.OWNER, d.status;
SELECT d.OWNER,
d.OBJECT_NAME,
d.OBJECT_TYPE,
d.status
FROM dba_objects d
WHERE d.status = 'INVALID'
and d.owner in (SELECT a.username
FROM DBA_USERS A
WHERE A.account_status = 'OPEN'
AND A.username NOT IN
('SYS', 'SYSTEM', 'MGMT_VIEW', 'SYSMAN', 'DBSNMP'));
expdp数据泵利用content=metadata_only导出元数据
导出命令,注意这里不导出数据只导出定义我们采用content=metadata_only来处理:
expdp lhr/lhr directory=DATA_PUMP_DIR dumpfile=lhrsql20150515.dmp logfile=lhrsql20150515.log content=metadata_only schemas=TEST,SQCHECK,DWUSER,DPA,CNYDM,ONL1,LHR,TEST1,FXDM,DWII_ETL,DWUSER1,SOR,DW_ETL,NRDM,NRDM_ETL,FXDM_ETL,LCM2,CNY_ETL
由于是事后写文档,所以这里只贴出导出元数据的日志:
;;;
Export: Release 10.2.0.1.0 - 64bit Production on Friday, 15 May, 2015 13:05:54
Copyright (c) 2003, 2005, Oracle. All rights reserved.
;;;
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
Starting "LHR"."SYS_EXPORT_SCHEMA_01": lhr/******** directory=DATA_PUMP_DIR dumpfile=lhrsql20150515.dmp logfile=lhrsql20150515.log content=metadata_only schemas=TEST,SQCHECK,DWUSER,DPA,CNYDM,ONL1,LHR,TEST1,FXDM,DWII_ETL,DWUSER1,SOR,DW_ETL,NRDM,NRDM_ETL,FXDM_ETL,LCM2,CNY_ETL
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/DB_LINK
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/PACKAGE/PACKAGE_SPEC
Processing object type SCHEMA_EXPORT/FUNCTION/FUNCTION
Processing object type SCHEMA_EXPORT/FUNCTION/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/PROCEDURE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/PACKAGE/COMPILE_PACKAGE/PACKAGE_SPEC/ALTER_PACKAGE_SPEC
Processing object type SCHEMA_EXPORT/FUNCTION/ALTER_FUNCTION
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
Processing object type SCHEMA_EXPORT/VIEW/VIEW
Processing object type SCHEMA_EXPORT/VIEW/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/VIEW/COMMENT
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Master table "LHR"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for LHR.SYS_EXPORT_SCHEMA_01 is:
/oracle/product/10.2.0/db_1/rdbms/log/lhrsql20150515.dmp
Job "LHR"."SYS_EXPORT_SCHEMA_01" successfully completed at 13:09:49
接下来就是把导出来的文件利用ftp工具或scp传递到目标端,我这里就直接传到我的虚拟机上吧。
执行导入工作
先建立相应的表空间
根据上边的工作我们知道需要在测试库上建立以下的表空间,我这里都设置的是20M,实际情况下应该根据上边查询出来的结果来设置相应的大小,表空间应用包含用户的默认表空间及用户下对象所在的表空间:
create tablespace DWII_CNY_BK_F_01 datafile '+DATA' size 20M;
create tablespace DWII_DPA_F_01 datafile '+DATA' size 20M;
create tablespace DWII_DPA_I_01 datafile '+DATA' size 20M;
create tablespace DWII_DPA_S_01 datafile '+DATA' size 20M;
create tablespace DWII_SOR_F_01 datafile '+DATA' size 20M;
create tablespace DWII_SOR_I_01 datafile '+FRA' size 20M;
create tablespace DW_USER datafile '+FRA' size 20M;
create tablespace SQCHECK datafile '+FRA' size 20M;
create tablespace SD_CNY_D_01 datafile '+FRA' size 20M;
create tablespace SD_CNY_F_01 datafile '+FRA' size 20M;
create tablespace SD_DPA_D_01 datafile '+FRA' size 20M;
create tablespace SD_DPA_F_01 datafile '+FRA' size 20M;
create tablespace SD_SORT_T_01 datafile '+FRA' size 20M;
create tablespace DWII_FXDM_F_01 datafile '+FRA' size 20M;
create tablespace SD_SOR_T_01 datafile '+FRA' size 20M;
如果空间不够,我们可以追加数据文件:alter tablespace DWII_DPA_F_01 add datafile '+FRA' size 50M;
执行导入语句
一般情况下,如果存储够的话,我们把相应的表空间设置大一点之后这里直接执行导入语句就可以了,但是我是在本机的虚拟机里执行的,由前边的情况我们可以知道大约需要49G的空间,这个显然不太合适,哪该怎么办呢?我能想到的办法只有如下2种,如果大家还有好的办法可以给我留言。
① 在源库上修改表的定义后然后再执行导出命令
② 从已经导出来的dmp文件中抽取其中的DDL语句,然后将DDL语句导入到数据库中,update掉其中的STORAGE(INITIAL 参数后再将语句导出到sql文本中执行sql语句,这样可以解决表的定义问题。
③ 从已经导出来的dmp文件中抽取其中的DDL语句,然后利用linux的sed批量替换功能替换掉不正确的参数。
显然,第一种比较方便,也比较快,但是不实用,由于是生产库,参数不能随便修改,我们就采用第二或第三种办法,也可以多演示一种impdp的用法。
导出DMP文件中的DDL语句
我们在impdp的导入命令中添加sqlfile参数后执行导入并不会真正将数据导入到数据库,而会抽取出dmp文件中的DDL语句,如下:
[oracle@rhel6_lhr dpdump]$ impdp lhr/lhr directory=DATA_PUMP_DIR dumpfile=lhrsql20150515.dmp logfile=imp_exptest.log sqlfile=lhrsql20150515.sql
Import: Release 11.2.0.3.0 - Production on Fri May 15 15:08:03 2015
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "LHR"."SYS_SQL_FILE_FULL_01" successfully loaded/unloaded
Starting "LHR"."SYS_SQL_FILE_FULL_01": lhr/******** directory=DATA_PUMP_DIR dumpfile=lhrsql20150515.dmp logfile=imp_exptest.log sqlfile=lhrsql20150515.sql
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/DB_LINK
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/PACKAGE/PACKAGE_SPEC
Processing object type SCHEMA_EXPORT/FUNCTION/FUNCTION
Processing object type SCHEMA_EXPORT/FUNCTION/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/PROCEDURE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/PACKAGE/COMPILE_PACKAGE/PACKAGE_SPEC/ALTER_PACKAGE_SPEC
Processing object type SCHEMA_EXPORT/FUNCTION/ALTER_FUNCTION
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
Processing object type SCHEMA_EXPORT/VIEW/VIEW
Processing object type SCHEMA_EXPORT/VIEW/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/VIEW/COMMENT
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "LHR"."SYS_SQL_FILE_FULL_01" successfully completed at 15:21:50
[oracle@rhel6_lhr dpdump]$ ll lhrsql20150515.sql
-rw-r--r-- 1 oracle asmadmin 65707967 May 15 15:21 lhrsql20150515.sql
[oracle@rhel6_lhr dpdump]$ more lhrsql20150515.sql
-- CONNECT LHR
ALTER SESSION SET EVENTS '10150 TRACE NAME CONTEXT FOREVER, LEVEL 1';
ALTER SESSION SET EVENTS '10904 TRACE NAME CONTEXT FOREVER, LEVEL 1';
ALTER SESSION SET EVENTS '25475 TRACE NAME CONTEXT FOREVER, LEVEL 1';
ALTER SESSION SET EVENTS '10407 TRACE NAME CONTEXT FOREVER, LEVEL 1';
ALTER SESSION SET EVENTS '10851 TRACE NAME CONTEXT FOREVER, LEVEL 1';
ALTER SESSION SET EVENTS '22830 TRACE NAME CONTEXT FOREVER, LEVEL 192 ';
-- new object type path: SCHEMA_EXPORT/USER
-- CONNECT SYSTEM
CREATE USER "CNY_ETL" IDENTIFIED BY VALUES '4686A1050F638F44'
DEFAULT TABLESPACE "DW_USER"
TEMPORARY TABLESPACE "TEMP";
CREATE USER "LCM2" IDENTIFIED BY VALUES '48BCFDF435352212'
DEFAULT TABLESPACE "DWII_SOR_F_01"
TEMPORARY TABLESPACE "TEMP";
CREATE USER "FXDM_ETL" IDENTIFIED BY VALUES 'EA010AEA839BFA14'
DEFAULT TABLESPACE "DW_USER"
TEMPORARY TABLESPACE "TEMP";
CREATE USER "NRDM_ETL" IDENTIFIED BY VALUES '54A4A046AEE8B31E'
DEFAULT TABLESPACE "DW_USER"
TEMPORARY TABLESPACE "TEMP";
CREATE USER "NRDM" IDENTIFIED BY VALUES '1AE3DF7368DF560D'
DEFAULT TABLESPACE "SD_CNY_F_01"
TEMPORARY TABLESPACE "TEMP";
CREATE USER "DW_ETL" IDENTIFIED BY VALUES '91635F9C0744E7EC'
DEFAULT TABLESPACE "DW_USER"
TEMPORARY TABLESPACE "TEMP";
CREATE USER "SOR" IDENTIFIED BY VALUES 'BA3A6C912E6BFF14'
DEFAULT TABLESPACE "DWII_SOR_F_01"
TEMPORARY TABLESPACE "TEMP";
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。省略
[oracle@rhel6_lhr dpdump]$ tail -n 50 lhrsql20150515.sql
c := 'SPOT_EXCHNG_RATE_SRC';
EXECUTE IMMEDIATE stmt USING 'C',t,p,sp,c,s,
2,.5,2,156,0,2.65784513562818E+35,2.65784513872303E+35,5,0,nv,nv,
TO_DATE('2015-05-14 22:00:18',df),'33303031','33303032',nv,2,nv;
c := 'SPOT_EXCHNG_RATE_SRC_NM';
EXECUTE IMMEDIATE stmt USING 'C',t,p,sp,c,s,
2,.5,2,156,0,1.18758942587854E+36,1.20817519861257E+36,12,0,nv,nv,
TO_DATE('2015-05-14 22:00:18',df),'E4B8ADE997B4E4BBB7','E8AFA2E4BBB7E59D87E580BC',nv,2,nv;
c := 'FLEG_SWAP_PNT';
EXECUTE IMMEDIATE stmt USING 'C',t,p,sp,c,s,
14,.0714285714285714,14,156,0,-64.01,16.5,4,0,nv,nv,
TO_DATE('2015-05-14 22:00:18',df),'3E256466','C11133',nv,2,nv;
c := 'SRC_SYS_LBL';
EXECUTE IMMEDIATE stmt USING 'C',t,p,sp,c,s,
1,1,1,156,0,3.44097282552972E+35,3.44097282552972E+35,5,0,nv,nv,
TO_DATE('2015-05-14 22:00:18',df),'42454E4D','42454E4D',nv,2,nv;
END;
/
DECLARE
c varchar2(60);
nv varchar2(1);
df varchar2(21) := 'YYYY-MM-DD:HH24:MI:SS';
s varchar2(60) := 'DPA';
t varchar2(60) := 'BNCHMK_OPTN_DLT_PARAM_F';
p varchar2(1);
sp varchar2(1);
stmt varchar2(300) := 'INSERT INTO "SYS"."IMPDP_STATS" (type,version,c1,c2,c3,c4,c5,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,d1,r1,r2,ch1,flags,cl1) VALUES (:1,6,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15,:16,:17,:18,:19,:20,:21,:22,:23)';
BEGIN
NULL;
c := 'CRT_TMST';
EXECUTE IMMEDIATE stmt USING 'C',t,p,sp,c,s,
1,1,1,156,0,2457157.57244213,2457157.57244213,11,0,nv,nv,
TO_DATE('2015-05-14 22:00:18',df),'7873050E0E2D14','7873050E0E2D14',nv,2,nv;
c := 'PPLN_WKDT';
EXECUTE IMMEDIATE stmt USING 'C',t,p,sp,c,s,
1,1,1,156,0,20150422,20150422,6,0,nv,nv,
TO_DATE('2015-05-14 22:00:18',df),'C415100517','C415100517',nv,2,nv;
c := 'PPLN_TMST';
EXECUTE IMMEDIATE stmt USING 'C',t,p,sp,c,s,
1,1,1,156,0,2457157.57244213,2457157.57244213,11,0,nv,nv,
TO_DATE('2015-05-14 22:00:18',df),'7873050E0E2D14','7873050E0E2D14',nv,2,nv;
DBMS_STATS.IMPORT_TABLE_STATS('"DPA"','"BNCHMK_OPTN_DLT_PARAM_F"',NULL,'"IMPDP_STATS"',NULL,NULL,'"SYS"');
DELETE FROM "SYS"."IMPDP_STATS";
END;
/
[oracle@rhel6_lhr dpdump]$
可以看到生成的SQL语句开始部分是ddl的建用户语句,最后是导入表的统计信息部分。
处理导出来的ddl文件
方法一,利用sed命令批量替换 (推荐)
这一步其实如果linux的sed命令熟悉的话可以直接替换掉sql语句中的相关行,我今天也是临时网上搜了下,经过大量实验,然后居然成功了,如下:
[oracle@rhel6_lhr dpdump]$ sed 's/^ STORAGE(INITIAL.*/STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645/g' lhrsql20150515.sql > lhrsql20150515.sql_bk2
[oracle@rhel6_lhr dpdump]$ more lhrsql20150515.sql_bk2 | grep "STORAGE(INITIAL"
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
[oracle@rhel6_lhr dpdump]$ more lhrsql20150515.sql_bk2 | grep "STORAGE(INITIAL 131072"
[oracle@rhel6_lhr dpdump]$ more lhrsql20150515.sql | grep "STORAGE(INITIAL 131072"
STORAGE(INITIAL 131072 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 131072 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 131072 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 131072 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 131072 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
STORAGE(INITIAL 131072 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
。。。。。。。。。。。。。
可以看到替换已经成功,如果不熟悉该命令,那么先熟悉该命令就麻烦了。
方法二:sqlldr导入到数据库,利用update来更新
都是搞数据库的,这样做虽然麻烦点,但是绝对不会出错,而且也是一种技巧,如果有的文本很大又很难处理的话我们就可以导入到数据库中,然后处理。
首先建表:
create table imp_sql_lhr (id number ,text varchar2(4000)) ;
sqlldr的控制文件内容:sqlldr_table.ctl :
UNRECOVERABLE
load data
LENGTH CHARACTER
infile 'lhrsql20150515.sql'
APPEND imp_sql_lhr
trailing nullcols
(
id SEQUENCE(1,1),
text char(4000) "TRIM(:text)"
)
sqlldr命令:
sqlldr lhr/lhr control=sqlldr_table.ctl log=a.log parallel=y readsize=4194304 streamsize=10485760 date_cache=5000 direct=true
导入到数据库后,我们就可以非常方便的来处理表中的数据了,如下:
SELECT * FROM imp_sql_lhr a where a.text like '%STORAGE(INITIAL%' and a.text not like '%STORAGE(INITIAL 65536%' ;
执行更新语句:
UPDATE imp_sql_lhr t
SET t.text = 'STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645'
WHERE t.text LIKE '%STORAGE(INITIAL%'
AND t.text NOT LIKE '%STORAGE(INITIAL 65536 %';
最后利用spool来导出到sql文本中:
set echo on
set trimspool on
set trimout on
set linesize 4000
set pagesize 0
set sqlblanklines on
set feedback off
set serveroutput off
set term off
set echo off
spool lhrsql20150515.sql_bk2
SELECT t.text from imp_sql_lhr t order by id;
spool off
执行处理好的sql语句
该步骤比较简单,就是把处理好的ddl语句提前执行一下,让数据库中包含相应的对象,这样再执行impdp导入的时候就不会再创建这些表了。
需要注意的是:DDL语句中创建存过、函数、包的语句中是不包含对象所属的schema的,这样的话如果那个用户执行sql脚本的话,这些对象就创建在那个用户下了,这个显然是错误的,不是我们期望的,那么如何处理这个问题呢,想了想,很简单的嘛,我们sql脚本执行完毕后,再执行一次impdp的命令就可以把这些对象重建,然后把错误的存过删除就可以了。
SELECT t.* from imp_sql_lhr t where t.text like 'CREATE PROCEDURE%' order by id;
[oracle@rhel6_lhr dpdump]$ sqlplus lhr/lhr
SQL*Plus: Release 11.2.0.3.0 Production on Fri May 15 16:32:08 2015
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
16:32:09 SQL> set echo off;
16:32:21 SQL> set serveroutput off;
16:32:30 SQL> set timing on;
16:32:40 SQL> set time on;
16:32:45 SQL> set timing off;
16:32:50 SQL> set time off;
SQL>
SQL>
SQL>
SQL> @lhrsql20150515.sql_bk2;
Session altered.
Session altered.
Session altered.
。。。。。。。。。。。。。。。。。。。。。。。。。。。 省略
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
impdp继续导入
[oracle@rhel6_lhr dpdump]$ impdp lhr/lhr directory=DATA_PUMP_DIR dumpfile=lhrsql20150515.dmp logfile=lhrsql20150515_imp.log parallel=4;
Import: Release 11.2.0.3.0 - Production on Fri May 15 19:05:29 2015
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "LHR"."SYS_IMPORT_FULL_02" successfully loaded/unloaded
Starting "LHR"."SYS_IMPORT_FULL_02": lhr/******** directory=DATA_PUMP_DIR dumpfile=lhrsql20150515.dmp logfile=lhrsql20150515_imp.log parallel=4
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"CNY_ETL" already exists
ORA-31684: Object type USER:"LCM2" already exists
ORA-31684: Object type USER:"FXDM_ETL" already exists
ORA-31684: Object type USER:"NRDM_ETL" already exists
ORA-31684: Object type USER:"NRDM" already exists
ORA-31684: Object type USER:"DW_ETL" already exists
ORA-31684: Object type USER:"SOR" already exists
ORA-31684: Object type USER:"DWUSER1" already exists
ORA-31684: Object type USER:"DWII_ETL" already exists
ORA-31684: Object type USER:"FXDM" already exists
ORA-31684: Object type USER:"TEST1" already exists
ORA-31684: Object type USER:"LHR" already exists
ORA-31684: Object type USER:"ONL1" already exists
ORA-31684: Object type USER:"CNYDM" already exists
ORA-31684: Object type USER:"DPA" already exists
ORA-31684: Object type USER:"DWUSER" already exists
ORA-31684: Object type USER:"SQCHECK" already exists
ORA-31684: Object type USER:"TEST" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/DB_LINK
ORA-31684: Object type DB_LINK:"SOR"."COG_DB" already exists
ORA-31684: Object type DB_LINK:"DPA"."COG_DB" already exists
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
ORA-31684: Object type SEQUENCE:"SOR"."SEQ_BDS_CL_IP_RL_ID" already exists
ORA-31684: Object type SEQUENCE:"SOR"."SEQ_GOLD_RATE_INFO" already exists
ORA-31684: Object type SEQUENCE:"SOR"."SEQ_MBR_API_ELGBLTY" already exists
ORA-31684: Object type SEQUENCE:"SOR"."SEQ_MBR_MKT_ELGBLTY" already exists
ORA-31684: Object type SEQUENCE:"SOR"."SEQ_MBR_MMKNG_ROLE" already exists
ORA-31684: Object type SEQUENCE:"SOR"."SEQ_ORG_CD_HSTRY" already exists
ORA-31684: Object type SEQUENCE:"SOR"."SQ_DW_IP_IP_RL" already exists
ORA-31684: Object type SEQUENCE:"SOR"."SQ_MKT_CLNDR_ID" already exists
ORA-31684: Object type SEQUENCE:"FXDM"."SEQ_GOLD_MBR_MMKNG_ROLE_D" already exists
ORA-31684: Object type SEQUENCE:"FXDM"."SEQ_GOLD_MINAMNT_CONFIG" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SEQ_BDS_MBR_D_ID" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SEQ_DL_MKT_INFO" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SEQ_GOLD_RATE_INFO" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SEQ_MBR_AUTH_BY_API_D" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SEQ_MBR_MMKNG_ROLE_D" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SEQ_ORG_D" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SQ_DW_BEST_QUOTE" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SQ_DW_BOND_MID_QUOTE_ID" already exists
ORA-31684: Object type SEQUENCE:"DPA"."SQ_DW_MEMBER_D_ID" already exists
Processing object type SCHEMA_EXPORT/TABLE/TABLE
ORA-39151: Table "DPA"."FX_DL_MKT_INFO" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_FWD_CPI_QT" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_FWD_DL_BY_SF_CP" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_FWD_QT_BY_DIR" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_SPOT_CPI_QT" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_SPOT_DL_BY_SF_CP" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_SPOT_QT_BY_DIR" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_SWAP_CPI_QT" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_SWAP_DL_BY_SF_CP" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_SWAP_DL_LEG_MKT_INFO" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."FX_SWAP_QT_BY_DIR" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."BOND_TURNOVER_RATE_F" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."MONI_TORT_CRCLTN_AMNT_F" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "DPA"."MONI_TURNOVER_RATE_F" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "SOR"."BST_QT_HIST" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "SOR"."BST_QT" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
。。。。。。。。。。。。。。。。。。。。。。。。。省略
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_BOND_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_FX_FWD_SWAP_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_CCS_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_CREPO_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_FCO_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_FRA_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_IBO_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_IRS_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."QUE_RIBD_DTL" creation failed
ORA-39112: Dependent object type TABLE_STATISTICS skipped, base object type TABLE:"SOR"."FX_BM_FXO_DL_DAY_HIST" creation failed
Job "LHR"."SYS_IMPORT_FULL_02" completed with 2165 error(s) at 19:16:19
[oracle@rhel6_lhr dpdump]$
[oracle@rhel6_lhr dpdump]$
[oracle@rhel6_lhr dpdump]$
删除错误用户下的包、存过和函数
执行如下的脚本来删除相应的错误对象:
BEGIN
FOR CUR IN (SELECT 'DROP ' || D.OBJECT_TYPE || ' ' || D.OBJECT_NAME SQLT
FROM dba_objects d
WHERE d.OWNER = 'LHR'
AND d.CREATED >=
to_date('2015-05-15 09:25:27',
'YYYY-MM-DD HH24:MI:SS')
AND D.OBJECT_TYPE IN ('FUNCTION', 'PROCEDURE', 'PACKAGE')) LOOP
EXECUTE IMMEDIATE CUR.SQLT;
END LOOP;
END;
数据校验
执行如下脚本和源库作比较,查看数据是否完整。
SELECT d.OWNER,count(1)
FROM dba_objects d
WHERE d.OWNER in (SELECT a.username
FROM DBA_USERS A
WHERE A.account_status = 'OPEN'
AND A.username NOT IN
('SYS', 'SYSTEM', 'MGMT_VIEW', 'SYSMAN', 'DBSNMP'))
group by d.OWNER;
SELECT d.OWNER,
d.status,count(1)
FROM dba_objects d
WHERE d.OWNER in (SELECT a.username
FROM DBA_USERS A
WHERE A.account_status = 'OPEN'
AND A.username NOT IN
('SYS', 'SYSTEM', 'MGMT_VIEW', 'SYSMAN', 'DBSNMP'))
group by d.OWNER, d.status;
SELECT d.OWNER,
d.OBJECT_NAME,
d.OBJECT_TYPE,
d.status
FROM dba_objects d
WHERE d.status = 'INVALID'
and d.owner in (SELECT a.username
FROM DBA_USERS A
WHERE A.account_status = 'OPEN'
AND A.username NOT IN
('SYS', 'SYSTEM', 'MGMT_VIEW', 'SYSMAN', 'DBSNMP'));
总结
到此所有的处理算是基本完毕,过程很简单,但是不同的场景处理方式有很多种,我们应该学会灵活变通,核心即expdp和impdp但是需要做很多的处理。
aboout me
...........................................................................................................................................................................................
本文作者:小麦苗,只专注于数据库的技术,更注重技术的运用
ITPUB BLOG:http://blog.itpub.net/26736162
本文地址:http://blog.itpub.net/26736162/viewspace-1657828/
本文pdf版:http://yunpan.cn/QCwUAI9bn7g7w 提取码:af2d
QQ:642808185 若加QQ请注明你所正在读的文章标题
创作时间地点:2015-05-15 10:00~ 2015-05-16 19:00 于唐镇金唐公寓宿舍
<版权所有,文章允许转载,但须以链接方式注明源地址,否则追究法律责任!>
...........................................................................................................................................................................................