一。

说明:

OLTP库中有些表数据量大,且每月有持续的大量数据添加。因为历史数据在此库中不再做訪问,而是在另1个OLAP库中做分析。所以会对历史数据迁移至OLAP库中。对这样的历史数据迁移的操作。较好的办法是该表採用分区表。按时间分区后,能够对分区进行迁移。通过分区交换和表空间传输会非常easy完毕。并且性能上影响非常小。





关于分区表很多其它内容:    http://blog.csdn.net/tanqingru/article/category/1397435 

关于表空间传很多其它内容: http://blog.csdn.net/tanqingru/article/category/1138527

二。

实例:

整个过程是在OLTP库做分区交换。然后通过表空间传输迁移至OLAP库。最后再做一次分区交换就可以。

1.创造须要的环境。

OLTP库环境准备:

SQL> conn /as sysdba
Connected.
SQL> select * from V$version; BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
SQL> show parameter db_create_file_dest NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_create_file_dest string /data01/qing create tablespace tan_2013_9 datafile size 5m autoextend on;
create tablespace tan_2013_10 datafile size 5m autoextend on;
create tablespace tan_2013_11 datafile size 5m autoextend on;
create tablespace tan_2013_12 datafile size 5m autoextend on;
create tablespace tan_2014_1 datafile size 5m autoextend on;
create tablespace tan_2014_2 datafile size 5m autoextend on;
create tablespace tan_2014_3 datafile size 5m autoextend on;
create tablespace tan_2014_4 datafile size 5m autoextend on;
create tablespace tan_2014_5 datafile size 5m autoextend on;
create tablespace tan_2014_6 datafile size 5m autoextend on;
create tablespace tan_2014_7 datafile size 5m autoextend on;
create tablespace tan_2014_8 datafile size 5m autoextend on; conn tan/tan SQL> create table tan
(t_id number(10),
t_name varchar2(100),
t_date date )
partition by range(t_date)
(partition tan_2013_9 values less than(to_date('2013-10-01','yyyy-mm-dd')) tablespace tan_2013_9,
partition tan_2013_10 values less than(to_date('2013-11-01','yyyy-mm-dd')) tablespace tan_2013_10,
partition tan_2013_11 values less than(to_date('2013-12-01','yyyy-mm-dd')) tablespace tan_2013_11,
partition tan_2013_12 values less than(to_date('2014-01-01','yyyy-mm-dd')) tablespace tan_2013_12,
partition tan_2014_1 values less than(to_date('2014-02-01','yyyy-mm-dd')) tablespace tan_2014_1,
partition tan_2014_2 values less than(to_date('2014-03-01','yyyy-mm-dd')) tablespace tan_2014_2,
partition tan_2014_3 values less than(to_date('2014-04-01','yyyy-mm-dd')) tablespace tan_2014_3,
partition tan_2014_4 values less than(to_date('2014-05-01','yyyy-mm-dd')) tablespace tan_2014_4,
partition tan_2014_5 values less than(to_date('2014-06-01','yyyy-mm-dd')) tablespace tan_2014_5,
partition tan_2014_6 values less than(to_date('2014-07-01','yyyy-mm-dd')) tablespace tan_2014_6,
partition tan_2014_7 values less than(to_date('2014-08-01','yyyy-mm-dd')) tablespace tan_2014_7,
partition tan_2014_8 values less than(to_date('2014-09-01','yyyy-mm-dd')) tablespace tan_2014_8
); create index ind_tan on tan(t_date) local
(
partition ind_tan_2013_9 tablespace tan_2013_9,
partition ind_tan_2013_10 tablespace tan_2013_10,
partition ind_tan_2013_11 tablespace tan_2013_11,
partition ind_tan_2013_12 tablespace tan_2013_12,
partition ind_tan_2014_1 tablespace tan_2014_1,
partition ind_tan_2014_2 tablespace tan_2014_2,
partition ind_tan_2014_3 tablespace tan_2014_3,
partition ind_tan_2014_4 tablespace tan_2014_4,
partition ind_tan_2014_5 tablespace tan_2014_5,
partition ind_tan_2014_6 tablespace tan_2014_6,
partition ind_tan_2014_7 tablespace tan_2014_7,
partition ind_tan_2014_8 tablespace tan_2014_8
); begin
for i in 1.. 10000 loop
if( mod(i,12)+1 <=8) then
insert into tan values(i,'tan'||i,to_date('2014-'||(mod(i,12)+1)||'-01','yyyy-mm-dd'));
else
insert into tan values(i,'tan'||i,to_date('2013-'||(mod(i,12)+1)||'-01','yyyy-mm-dd'));
end if;
end loop;
commit;
end;
/ SQL> select count(*) from tan partition(tan_2013_12) ; COUNT(*)
----------
833 SQL> select count(*) from tan partition(tan_2013_9) ; COUNT(*)
----------
833 SQL> select count(*) from tan partition(tan_2014_8) ; COUNT(*)
----------
833 SQL> select count(*) from tan partition(tan_2014_1) ; COUNT(*)
----------
833 SQL> select partition_name,tablespace_name from dba_segments
where segment_name in ('TAN','IND_TAN'); PARTITION_NAME TABLESPACE_NAME
------------------------------ ------------------------------
TAN_2014_8 TAN_2014_8
TAN_2014_7 TAN_2014_7
TAN_2014_6 TAN_2014_6
TAN_2014_5 TAN_2014_5
TAN_2014_4 TAN_2014_4
TAN_2014_3 TAN_2014_3
TAN_2014_2 TAN_2014_2
TAN_2014_1 TAN_2014_1
TAN_2013_9 TAN_2013_9
TAN_2013_12 TAN_2013_12
TAN_2013_11 TAN_2013_11
TAN_2013_10 TAN_2013_10
IND_TAN_2014_8 TAN_2014_8
IND_TAN_2014_7 TAN_2014_7
IND_TAN_2014_6 TAN_2014_6
IND_TAN_2014_5 TAN_2014_5
IND_TAN_2014_4 TAN_2014_4
IND_TAN_2014_3 TAN_2014_3
IND_TAN_2014_2 TAN_2014_2
IND_TAN_2014_1 TAN_2014_1
IND_TAN_2013_9 TAN_2013_9
IND_TAN_2013_12 TAN_2013_12
IND_TAN_2013_11 TAN_2013_11
IND_TAN_2013_10 TAN_2013_10
24 rows selected.

OLAP库环境准备

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">create tablespace tan_2013_7 datafile size 5m autoextend on;</span>
create tablespace tan_2013_8 datafile size 5m autoextend on;
create table tan
(t_id number(10),
t_name varchar2(100),
t_date date )
partition by range(t_date)
(partition tan_2013_7 values less than(to_date('2013-08-01','yyyy-mm-dd')) tablespace tan_2013_7,
partition tan_2013_8 values less than(to_date('2013-09-01','yyyy-mm-dd')) tablespace tan_2013_8
); create index ind_tan on tan(t_date) local
(
partition ind_tan_2013_7 tablespace tan_2013_7,
partition ind_tan_2013_8 tablespace tan_2013_8
); begin
for i in 1.. 10000 loop
insert into tan values(i,'tan'||i,to_date('2013-'||(mod(i,2)+7)||'-01','yyyy-mm-dd'));
end loop;
commit;
end;
/ SQL> select count(*) from tan partition(tan_2013_8); COUNT(*)
----------
5000 SQL> select count(*) from tan partition(tan_2013_7); COUNT(*)
----------
5000

2.分区交换

如今要做的事是迁移2013年9月份数据。

创建个暂时表:

SQL> create table tmp_tan_2013_9 as select * from tan where 1=2;

SQL> create index ind_tmp_tan_2013_9 on tmp_tan_2013_9(t_date);





分区交换。

SQL> alter table tan exchange partition tan_2013_9

with table tmp_tan_2013_9 including indexes with validation;

验证:

SQL> select count(*) from tan partition(tan_2013_9) ;

  COUNT(*)
----------
0
SQL> select count(*) from tmp_tan_2013_9;
COUNT(*)
----------
833
SQL> select partition_name,tablespace_name from dba_segments
where segment_name in ('TAN','IND_TAN'); PARTITION_NAME TABLESPACE_NAME
------------------------------ ------------------------------
TAN_2014_8 TAN_2014_8
TAN_2014_7 TAN_2014_7
TAN_2014_6 TAN_2014_6
TAN_2014_5 TAN_2014_5
TAN_2014_4 TAN_2014_4
TAN_2014_3 TAN_2014_3
TAN_2014_2 TAN_2014_2
TAN_2014_1 TAN_2014_1
TAN_2013_12 TAN_2013_12
TAN_2013_11 TAN_2013_11
TAN_2013_10 TAN_2013_10
IND_TAN_2014_8 TAN_2014_8
IND_TAN_2014_7 TAN_2014_7
IND_TAN_2014_6 TAN_2014_6
IND_TAN_2014_5 TAN_2014_5
IND_TAN_2014_4 TAN_2014_4
IND_TAN_2014_3 TAN_2014_3
IND_TAN_2014_2 TAN_2014_2
IND_TAN_2014_1 TAN_2014_1
IND_TAN_2013_12 TAN_2013_12
IND_TAN_2013_11 TAN_2013_11
IND_TAN_2013_10 TAN_2013_10 22 rows selected.

3. 表空间传输:

很多其它关于表空间传输的内容:http://blog.csdn.net/bamuta/article/category/1138527





验证表空间的自包括:

SQL> exec dbms_tts.transport_set_check('TAN_2013_9',TRUE);





PL/SQL procedure successfully completed.





SQL> select * from transport_set_violations;

no rows selected

另外表空间传输须要两端库的字符集一致。

导出无数据:

SQL> alter tablespace tan_2013_9 read only;

[oracle@OEL63 ~]$ exp \'sys/oracle as sysdba\' tablespaces=tan_2013_9 transport_tablespace=y file=exp_tan_2013_9.dmp 



复制文件

[oracle@OEL63 ~]$ scp exp_tan_2013_9.dmp 192.168.114.174:/home/oracle/

[oracle@OEL63 ~]$ scp /data01/qing/QING/datafile/o1_mf_tan_2013_9tht2cgh_.dbf 192.168.114.174:/data01/vm603/VM603/datafile/



在目标库导入元数据

[oracle@vm603 ~]$ imp \'sys/oracle as sysdba\' transport_tablespace=y file=exp_tan_2013_9.dmp log=imp.log tablespaces=tan_2013_9 datafiles='/data01/vm603/VM603/datafile/o1_mf_tan_2013_9tht2cgh_.dbf'





将两端库的该表空间read write 

SQL>  alter tablespace tan_2013_9 read write;

4.目标库再做分区交换

目标库对就表添加分区:

SQL> alter table tan add partition tan_2013_9 values less than (to_date('2013-10-01','yyyy-mm-dd')) tablespace tan_2013_9;





Table altered.





SQL> select count(*) from tan partition(tan_2013_9);





  COUNT(*)

----------

         0





在目标库做一次分区交换。



SQL> alter table tan exchange partition tan_2013_9 with table tmp_tan_2013_9 including indexes with validation;





Table altered.





检查

SQL>  select count(*) from tan partition(tan_2013_9);

  COUNT(*)
----------
833 SQL> select table_name,partition_name,tablespace_name from user_tab_partitions;
TABLE_NAME PARTITION_NAME TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
TAN TAN_2013_8 TAN_2013_8
TAN TAN_2013_7 TAN_2013_7
TAN TAN_2013_9 TAN_2013_9 SQL> select index_name,partition_name,tablespace_name from user_ind_partitions;
INDEX_NAME PARTITION_NAME TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
IND_TAN IND_TAN_2013_8 TAN_2013_8
IND_TAN IND_TAN_2013_7 TAN_2013_7
IND_TAN TAN_2013_9 TAN_2013_9

5.源库删掉已经迁移走的分区:

SQL> select table_name,partition_name,tablespace_name from user_tab_partitions;

TABLE_NAME                     PARTITION_NAME                 TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
TAN TAN_2014_3 TAN_2014_3
TAN TAN_2014_2 TAN_2014_2
TAN TAN_2014_4 TAN_2014_4
TAN TAN_2014_5 TAN_2014_5
TAN TAN_2014_6 TAN_2014_6
TAN TAN_2014_7 TAN_2014_7
TAN TAN_2014_8 TAN_2014_8
TAN TAN_2013_10 TAN_2013_10
TAN TAN_2013_11 TAN_2013_11
TAN TAN_2013_12 TAN_2013_12
TAN TAN_2014_1 TAN_2014_1
TAN TAN_2013_9 USERS 12 rows selected. SQL> <strong>alter table tan drop partition tan_2013_9;</strong> Table altered. SQL> select table_name,partition_name,tablespace_name from user_tab_partitions; TABLE_NAME PARTITION_NAME TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
TAN TAN_2014_3 TAN_2014_3
TAN TAN_2014_2 TAN_2014_2
TAN TAN_2014_4 TAN_2014_4
TAN TAN_2014_5 TAN_2014_5
TAN TAN_2014_6 TAN_2014_6
TAN TAN_2014_7 TAN_2014_7
TAN TAN_2014_8 TAN_2014_8
TAN TAN_2013_10 TAN_2013_10
TAN TAN_2013_11 TAN_2013_11
TAN TAN_2013_12 TAN_2013_12
TAN TAN_2014_1 TAN_2014_1

Oracle用分区表分区交换做历史数据迁移的更多相关文章

  1. ORACLE 查看分区表分区大小

    SELECT *  FROM dba_segments t WHERE t.segment_name ='table_name'; pratition_name : 分区名 bytes : 分区大小( ...

  2. 使用SQL SERVER存储过程实现历史数据迁移

    今天讲下软件开发中最常见的历史数据迁移方式.在讲迁移之前,先简单介绍下几个基本概念. 1.什么是历史数据迁移? 简单直白地说:就是将一些创建时间比较久而且不常用的历史数据,存储到另一个地方(可以是另一 ...

  3. oracle 入门笔记---分区表的分区交换

    本文参考来自作者:蓝紫 详细内容请阅读原文 : http://www.cnblogs.com/lanzi/archive/2013/01/24/2875838.html 在oracle 11.2环境下 ...

  4. oracle分区交换技术

    交换分区的操作步骤如下: 1. 创建分区表t1,假设有2个分区,P1,P2.2. 创建基表t11存放P1规则的数据.3. 创建基表t12 存放P2规则的数据.4. 用基表t11和分区表T1的P1分区交 ...

  5. 详解Oracle partition分区表

    随着表中行数的增多,管理和性能性能影响也将随之增加.备份将要花费更多时间,恢复也将 要花费更说的时间,对整个数据表的查询也将花费更多时间.通过把一个表中的行分为几个部分,可以减少大型表的管理和性能问题 ...

  6. 什么是Oracle的分区表 (转 作者 陈字文)

    假设我们现在正在酝酿经营一家图书馆,最初,我们只有十本书提供给大家来阅读和购买.对于十本书而言,我们可能只需要一个书架格子将其作为保存这十本书的容器就足够了,因为任何一个人都可以很轻松的扫一眼就可以将 ...

  7. 分区表分区字段的update操作

    默认情况下,oracle的分区表对于分区字段是不允许进行update操作的,如果有对分区字段行进update,就会报错——ORA-14402: 更新分区关键字列将导致分区的更改.但是可以通过打开表的r ...

  8. 浅谈Oracle数据库分区表

    Oracle数据库分区是作为Oracle数据库性能优化的一种重要的手段和方法,之前,只听过分区的大名,却总未用过,最近简单学习了一下,总结如下,不对之处,还希望朋友们多多指点,交流! 1.表空间及分区 ...

  9. oracle的表分区

    (1.) 表空间及分区表的概念 表空间: 是一个或多个数据文件的集合,所有的数据对象都存放在指定的表空间中,但主要存放的是表, 所以称作表空间.   分区表: 当表中的数据量不断增大,查询数据的速度就 ...

随机推荐

  1. 基于libnids的TCP数据流的还原(多线程实现) .

    我们知道,libnids本身可以实现TCP数据流的重组,但是如果一个TCP流数据量比较大的时候,就会分成好多个TCP报文段,这些报文段在网络中的传播可能是乱序的,利用libnids可以帮助我们按顺序接 ...

  2. 57. 三数之和 &&

    题目 57. 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的 ...

  3. VS单元测试中Assert类的用法

    首先说介绍一下,Assert类所在的命名空间为Microsoft.VisualStudio.TestTools.UnitTesting 在工程文件中只要引用Microsoft.VisualStudio ...

  4. SQL SERVER CXPACKET-Parallelism Wait Type 的惯用解决方案

    最近我的两个库出现,出现较多的CXPACKET等待,在网上找了一下资料.其中有篇一个SQL Server专栏作家的文章不错,也解决了我的一些疑问,就翻译在这里. 翻译整理仅用于传播资讯之目的. 原文出 ...

  5. python os.path模块常用方法详解 ZZ

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...

  6. jdbc操作数据库并自动获取字段类型

    //获取改功能编码的关联功能 public void getLinkdb(String gnbianma){ PreparedStatement pstmt = null; ResultSet rs ...

  7. 关于ThinkPhp中getField方法存在的问题

    在ThinkPhp中我们可以通过以下方式获取数据库数据 query:直接执行SQL查询操作 find:查询单选数据集 getField查询字段值 select:查询数据集 其他......   但今天 ...

  8. linux里tmpfs文件系统

    linux里tmpfs文件系统 是一个虚拟内存文件系统,它不同于传统的用块设备形式来实现的Ramdisk,也不同于针对物理内存的Ramfs.Tmpfs可以使用物理内存,也可以使用交换分区. umoun ...

  9. javascript 将treeNode 转换id和pid的Array

    function treeTolist(treeNodes, opt) { if (!opt) { opt = {}; opt.key = "id"; opt.parent = & ...

  10. redis 五种数据结构详解(string,list,set,zset,hash),各种问题综合

    redis 五种数据结构详解(string,list,set,zset,hash) https://www.cnblogs.com/sdgf/p/6244937.html redis 与 spring ...