如何快速的复制一个用户的数据到另外一个用户(这个用户可能在不同的数据库中)?
一般答案:使用EXP(EXPDP)与IMP(IMPDP)相结合完成用户数据的导入和导出
高级方法:IMPDP工具提供的NETWORK_LINK参数可以一步到位的完成此项艰巨的任务。
这种方法非常类似于使用CTAS方法在表复制中的应用,只不过这里我们实现的是用户间的数据复制。
使用这种方法的一般步骤如下:
a.创建database link;
b.使用IMPDP的network_link、schemas和remap_schema相结合完成用户的数据的迁移;
真实的感受一下此项技术带给我们的快乐。
实现案例:同一个实例中不同用户间的迁移复制。
1.创建指向自身的DATABASE LINK
1)确认tnsnames.ora文件中的连接串
[email protected] /home/oracle$ cat $ORACLE_HOME/network/admin/tnsnames.ora
# tnsnames.ora Network Configuration File: /oracle/ora11gR2/product/11.2.0/dbhome_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
SECOOLER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = secDB)(PORT = 1526))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = secooler)
)
)
2)在数据库系统中创建一个指向自身的DATABASE LINK
[email protected]> create public database link dblink_to_myself connect to system identified by sys using ‘SECOOLER‘;
Database link created.
2.复制sec用户的数据到secooler用户
1)确认sec用户下的T表及其数据量
[email protected]> conn sec/sec
Connected.
[email protected]> select * From cat;
TABLE_NAME TABLE_TYPE
------------------------------ -----------
T TABLE
[email protected]> select count(*) from t;
COUNT(*)
----------
71325
2)查看secooler用户
[email protected]> conn secooler/secooler
Connected.
[email protected]> select * from cat;
no rows selected
此时secooler用户中不包含任何T表信息。
3)使用IMPDP工具实现数据从sec用户向secooler用户复制的功能
[email protected] /home/oracle$ impdp system/sys network_link=dblink_to_myself schemas=sec remap_schema=sec:secooler
Import: Release 11.2.0.1.0 - Production on Thu Apr 8 10:01:16 2010
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** network_link=dblink_to_myself schemas=sec remap_schema=sec:secooler
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 9 MB
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"SECOOLER" 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/TABLE/TABLE
. . imported "SECOOLER"."T" 71325 rows
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" completed with 1 error(s) at 10:02:45
3.查看secooler用户数据,验收迁移成果
[email protected] /home/oracle$ sqlplus secooler/secooler
SQL*Plus: Release 11.2.0.1.0 Production on Thu Apr 8 10:04:55 2010
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
[email protected]> select * from cat;
TABLE_NAME TABLE_TYPE
------------------------------ -----------
T TABLE
[email protected]> select count(*) from t;
COUNT(*)
----------
71325
令人激动地时刻到了,sec用户下的T表及其数据已经成功的“复制”到了secooler用户中。
4.小结
使用IMPDP工具完成用户数据复制的优点:
1)节省了大量的磁盘空间,因为不用生成中间的dump文件;
2)操作简便,步骤精简;
3)因为操作环节的减少,整个操作过程不易出错。