Oracle用分区表分区交换做历史数据迁移

一。

说明:

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;

[[email protected] ~]$ exp \‘sys/oracle as sysdba\‘ tablespaces=tan_2013_9 transport_tablespace=y file=exp_tan_2013_9.dmp

复制文件

[[email protected] ~]$ scp exp_tan_2013_9.dmp 192.168.114.174:/home/oracle/

[[email protected] ~]$ scp /data01/qing/QING/datafile/o1_mf_tan_2013_9tht2cgh_.dbf 192.168.114.174:/data01/vm603/VM603/datafile/

在目标库导入元数据

[[email protected] ~]$ 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
时间: 2024-07-30 19:52:35

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

用分区表分区交换做历史数据迁移

一.说明: OLTP库中有些表数据量大,且每月有持续的大量数据增加,由于历史数据在此库中不再做访问,而是在另1个OLAP库中做分析,所以会对历史数据迁移至OLAP库中.对这种历史数据迁移的操作,较好的办法是该表采用分区表.按时间分区后,可以对分区进行迁移.通过分区交换和表空间传输会很容易完成,而且性能上影响很小. 关于分区表更多内容:    http://blog.csdn.net/tanqingru/article/category/1397435 关于表空间传更多内容: http://blo

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

本文参考来自作者:蓝紫 详细内容请阅读原文 : http://www.cnblogs.com/lanzi/archive/2013/01/24/2875838.html 在oracle 11.2环境下测试 --drop table tab_a purge; --创建分区表 create table tab_a ( r_id number(19) primary key, r_name varchar2(300), r_pat  integer ) partition by list (r_pat

Oracle分区交换

从Oracle8开始,提供了从分区交换的功能,如一个分区或子分区与一个非分区表交换.一个hash分区与另一个表的hash子分区交换等等,详细的交换方式可以参考官方文档. 基本语法:ALTER TABLE...EXCHANGE PARTITION 实验环境:11.2.0.4 [email protected]>select * from v$version; BANNER ---------------------------------------------------------------

记一次从oracle到hive的历史数据迁移(一)

介绍一下具体场景 需要从现有在用的oracle中将历史数据迁移出来并导入到hive中,存在几个问题:1.现有oracle数据库在使用无法整个库导出,数据库服务器没有权限:2.由于各个数据提供方的各次升级,oracle数据库采取添加字段而不删除字段的方式更新,而hive中建立的表数据结构要按新的来,也就是说oracle导出的数据到hive表中需要做字段对应. 决定先将oracle导入到另一个oracle中,将oracle中表字段和hive中的表字段进行比对得到各个的对应关系,之后写对应的sqoop

详解Oracle partition分区表

随着表中行数的增多,管理和性能性能影响也将随之增加.备份将要花费更多时间,恢复也将 要花费更说的时间,对整个数据表的查询也将花费更多时间.通过把一个表中的行分为几个部分,可以减少大型表的管理和性能问题,以这种方式划分发表数据的方法称为对表的分区.分区表的优势: (1)改善查询性能:对分区对象的查询可以仅搜索自己关心的分区,提高检索速度: (2)方便数据管理:因为分区表的数据存储在多个部分中,所以按分区加载和删除数据比在大表中加载和删除数据更容易: (3)方便备份恢复:因为分区比被分区的表要小,所

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

假设我们现在正在酝酿经营一家图书馆,最初,我们只有十本书提供给大家来阅读和购买.对于十本书而言,我们可能只需要一个书架格子将其作为保存这十本书的容器就足够了,因为任何一个人都可以很轻松的扫一眼就可以将这十本书的书名大概记住,并且挑选出合适自己的书籍来看. 但是随着我们书籍越来越多,我们需要更大的容器来保存我们的科教类.IT类.历史类.人文类等等书籍,大家都知道的一个生活常识就是,我们肯定不能够将所有类型的书籍都扔到一个书架容器上摆着,最科学的,就是分区,将不同的书籍放到不同的地方去,这样,假如我

浅谈Oracle数据库分区表

Oracle数据库分区是作为Oracle数据库性能优化的一种重要的手段和方法,之前,只听过分区的大名,却总未用过,最近简单学习了一下,总结如下,不对之处,还希望朋友们多多指点,交流! 1.表空间及分区表的概念    2.表分区的具体作用    3.表分区的优缺点    4.表分区的几种类型及操作方法    5.对表分区的维护性操作. ( 1.) 表空间及分区表的概念    表空间:       是一个或多个数据文件的集合,所有的数据对象都存放在指定的表空间中,但主要存放的是表, 所以称作表空间.

写脚本的一些心得-----------------------历史数据迁移到分表

历史数据迁移到分表的.(以前单表几十G的表,需要做优化分表) 背景: 之前项目因为历史原因使用的是mssql,其中有大量的各种log表,需要完整地迁移到mysql的按照日期分表的分表里.由于数据量大和表当初设计的一些缺陷,所以在迁移的时候要考虑查询效率和执行效率问题. 我采用的方式如下: 每一个表一个function去处理.其实写这种导数据的应该对传入参数减少.我当初写的是开始时间和天数的传入,然后根据时间戳算出连续时间的字符串,用来去生成对应的天表表面中的字符串.其实后来想想,完全可以传入2个

Timestamp 与 Date 变量绑定与Oracle的自动分区

好久没有更新博客了,其实是工作中遇到的很多问题在Google上都能找到答案,也就没有记录下来的必要了.今天主要想聊一下在实际的系统中遇到的Oracle数据库的问题,希望对大家有一点点帮助就好. 我首先描述一下我所遇到的场景:我们的数据库用的是Oracle 11g,我想大家立马就对它的自动分区(Interval)有了基本的认识了,这是一个非常棒的功能,免除了在建表时弄一大堆建Range分区的代码,也免除了以后对数据库进行分区扩充的麻烦.当然利用JOB也是可以完成分区扩展的,但是既然Oracle提供