天天看点

海量数据迁移之传输表空间(一)

在自己接触的很多的数据迁移工作中,使用外部表在一定程度上达到了系统的预期,对于增量,批量的数据迁移效果还是不错的,但是也不能停步不前,在很多限定的场景中,有很多物理迁移中使用传统方法还是相当不错的,传输表空间就是一个样例。

最近的有一个数据迁移任务是需要把一些全新的数据表迁移到另外一个库中,因为这些表在目标库中不存在,所以使用逻辑迁移就显得有些力不从心了。尽管在速度可以接受的情况下,最大的痛处就是大量的归档文件了。

因为需要在原有的schema下增加一些全新的数据表,不是很肯定传输表空间的校验是否能够完全支持。所以在给出方案之前还是做了做测试,达到了预期的想法。

为了对比清晰,我创建了两个全新的表空间,然后创建一个用户,创建两个表,制定到两个不同的表空间下,然后使用exp使用传输表空间模式导出,然后拷贝数据文件,导入,为了简单验证,就在同一个实例下做了测试。唯一多出来的步骤就是做一些简单的清理。

--数据准备

创建两个表空间

 create tablespace test_new datafile '/u02/ora11g/oradata/TEST11G/test_new01.dbf' size 10M;

 create tablespace test_old datafile '/u02/ora11g/oradata/TEST11G/test_old01.dbf' size 10M;

创建一个用户

create user test_tts identified by oracle default tablespace test_old;

grant connect,resource to test_tts;

然后创建两个表制定不同的表空间

create table test1 tablespace test_new as select *from all_objects where rownum

create table test2 tablespace test_old as select *from all_objects where rownum

可以简单验证一下数据情况。

select count(*)from test1;

select count(*)from test2;

然后查看user_tables简单验证一下表所处的表空间

select tablespace_name,table_name from user_tables;

TABLESPACE_NAME                TABLE_NAME

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

TEST_NEW                       TEST1

TEST_OLD                       TEST2

----表空间传输检查

在导出之前,使用dbms_tts做一下检查,在这个例子中是没有问题的。

exec dbms_tts.transport_set_check('TEST_NEW',TRUE);

使用给定的视图来查看是否有传输的限制。

select *from transport_set_violations;

--表空间传输导出

导出时需要指定表空间为只读模式alter tablespace test_new read only;

[ora11g@oel1 ~]$ exp \'sys/oracle as sysdba\' file=test_new.dmp transport_tablespace=y tablespaces=test_new log=test_new_tts.log

Export: Release 11.2.0.1.0 - Production on Wed Jun 17 18:26:17 2015

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 - Production

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

Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set

Note: table data (rows) will not be exported

About to export transportable tablespace metadata...

For tablespace TEST_NEW ...

. exporting cluster definitions

. exporting table definitions

. . exporting table                          TEST1

. exporting referential integrity constraints

. exporting triggers

. end transportable tablespace metadata export

Export terminated successfully without warnings.

 --额外的步骤,做一下简单的备份和数据清理。因为在同一个实例中实验,所以需要备份一下,然后把数据删除。

    备份:

cp /u02/ora11g/oradata/TEST11G/test_new01.dbf /u02/ora11g/oradata/TEST11G/test_new01.dbf1

    清理

drop table TEST1 purge;

drop tablespace test_new including contents and datafiles cascade constraint

简单验证是否数据文件存在,需要确定数据文件的句柄已经释放。

sys@TEST11G> !ls -l /u02/ora11g/oradata/TEST11G/test_new01.dbf

ls: /u02/ora11g/oradata/TEST11G/test_new01.dbf: No such file or directory

然后重命名数据文件,把原有的备份恢复。这个时候数据文件就回来了。

!mv /u02/ora11g/oradata/TEST11G/test_new01.dbf1 /u02/ora11g/oradata/TEST11G/test_new01.dbf

!ls -l /u02/ora11g/oradata/TEST11G/test_new01.dbf

--表空间导入

 imp \'sys/oracle as sysdba\' file=test_new.dmp transport_tablespace=y tablespaces=test_new datafiles=/u02/ora11g/oradata/TEST11G/test_new01.dbf

Import: Release 11.2.0.1.0 - Production on Wed Jun 17 18:42:47 2015

Export file created by EXPORT:V11.02.00 via conventional path

About to import transportable tablespace(s) metadata...

import done in AL32UTF8 character set and AL16UTF16 NCHAR character set

. importing SYS's objects into SYS

. importing TEST_TTS's objects into TEST_TTS

. . importing table                        "TEST1"

Import terminated successfully without warnings.

--迁移后的补充

迁移后需要把表空间设置为read,write模式alter tablespace test_new read write;

--数据检查

淡然了上面的步骤只是简单的一个常规步骤,其实还是有不少的细节考虑的,后面继续补充。