交换分区的操作步骤如下:

1. 创建分区表t1,假设有2个分区,P1,P2.
2. 创建基表t11存放P1规则的数据。
3. 创建基表t12 存放P2规则的数据。
4. 用基表t11和分区表T1的P1分区交换。 把表t11的数据放到到P1分区
5. 用基表t12 和分区表T1p2 分区交换。 把表t12的数据存放到P2分区。

----1.未分区表和分区表中一个分区交换
create table t1
(
sid int not null primary key,
sname  varchar2(50)
)
PARTITION BY range(sid)
( PARTITION p1 VALUES LESS THAN (5000) tablespace test,
  PARTITION p2 VALUES LESS THAN (10000) tablespace test,
  PARTITION p3  VALUES LESS THAN (maxvalue) tablespace test
) tablespace test;

SQL> select count(*) from t1;

COUNT(*)
----------
         0

create table t11
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;

create table t12
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;

create table t13
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;

--循环导入数据
declare
        maxrecords constant int:=4999;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t11 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/

declare
        maxrecords constant int:=9999;
        i int :=5000;
    begin
        for i in 5000..maxrecords loop
          insert into t12 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/

declare
        maxrecords constant int:=70000;
        i int :=10000;
    begin
        for i in 10000..maxrecords loop
          insert into t13 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/
commit;

SQL> select count(*) from t11;

COUNT(*)
----------
      4999

SQL> select count(*) from t12;

COUNT(*)
----------
      5000

SQL> select count(*) from t13;

COUNT(*)
----------
     60001

--交换分区

alter table t1 exchange partition p1 with table t11;

SQL> select count(*) from t11;   --基表t11数据为0

COUNT(*)
----------
         0

SQL> select count(*) from t1 partition (p1);  --分区表的P1分区数据位基表t11的数据

COUNT(*)
----------
      4999

alter table t1 exchange partition p2 with table t12;

select count(*) from t12;

select count(*) from t1 partition (p2);

alter table t1 exchange partition p3 with table t13;

select count(*) from t13;

select count(*) from t1 partition (p3);

-----2.分区表和分区表交换

/*
EXCHANGE PARTITION WITH TABLE的方式不支持分区表与分区表的交换,只能通过中间表中转.
*/

--2.1源表

create tablespace jinrilog
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\jinrilog01.DBF'
size 200M  autoextend on next 20M maxsize unlimited
extent management local autoallocate
segment space management auto
;

create tablespace jinrilogindex
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\jinrilogindex01.DBF'
size 200M  autoextend on next 20M maxsize unlimited
extent management local autoallocate
segment space management auto
;

create table t1
(
sid int not null ,
sname  varchar2(50) not null,
createtime date default sysdate   not null
)
PARTITION BY range(createtime)

PARTITION p1 VALUES LESS THAN ('2013-06-01 00:00:00') tablespace jinrilog,
PARTITION p2 VALUES LESS THAN ('2013-07-01 00:00:00') tablespace jinrilog,
PARTITION p3 VALUES LESS THAN ('2013-08-01 00:00:00') tablespace jinrilog,
PARTITION p4  VALUES LESS THAN (maxvalue) tablespace jinrilog
) tablespace jinrilog;

create unique index un_t1_01 on t1(sid,createtime)
tablespace jinrilogindex
local;

alter table t1 add constraint pk_t1 primary key(sid,createtime);

create index index_t1_01
on t1 (sname  asc)
tablespace jinrilogindex
local
(
partition index_sname_01 tablespace jinrilogindex,
partition index_sname_02 tablespace jinrilogindex,
partition index_sname_03 tablespace jinrilogindex,
partition index_sname_04 tablespace jinrilogindex
);

--循环导入数据
declare
        maxrecords constant int:=1000;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t1 values(i,'ocpyang','2013-06-11 00:00:00');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/

declare
        maxrecords constant int:=2000;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t1 values(i,'ocpyang','2013-07-11 00:00:00');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/

declare
        maxrecords constant int:=3000;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t1 values(i,'ocpyang','2013-08-11 00:00:00');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/

SQL> select count(*) from t1;

COUNT(*)
----------
     6000

SQL> select count(*) from  t1 partition(p1) ;

COUNT(*)
----------
         0

SQL>
SQL> select count(*) from  t1 partition(p2) ;

COUNT(*)
----------
      1000

SQL> select count(*) from  t1 partition(p3) ;

COUNT(*)
----------
      2000

SQL> select count(*) from  t1 partition(p4) ;

COUNT(*)
----------
      3000

---查看表数据分区情况

select utp.table_name,utp.partition_name,utp.tablespace_name from user_tab_partitions utp 
where utp.table_name='T1';

--查看分区索引分布情况

col index_name for a20
col partition_name for a20
col tablespace_name for a20
col status for a10
select index_name,null partition_name,tablespace_name,status
from user_indexes
where table_name='T1'
and partitioned='NO'
union 
select index_name,partition_name,tablespace_name,status from user_ind_partitions
where index_name in
(
select index_name from user_indexes
where table_name='T1'
)
order by 1,2,3
;
--2.2 和中间表交换数据

create table t11
(
sid int not null ,
sname  varchar2(50)  not null,
createtime date default sysdate   not null
)tablespace jason;

select count(*) from t11;

alter table t1 exchange partition p2 with table t11;

--查看无效的索引并重建

col index_name for a20
col partition_name for a20
col tablespace_name for a20
col status for a10
select index_name,null partition_name,status
from user_indexes
where table_name='T1'
and partitioned='NO'
union 
select index_name,partition_name,status from user_ind_partitions
where index_name in
(
select index_name from user_indexes
where table_name='T1'
)
order by 1,2,3
;

INDEX_NAME                     PARTITION_NAME                 STATUS
------------------------------ ------------------------------ --------
INDEX_T1_01                    INDEX_SNAME_01                 USABLE
INDEX_T1_01                    INDEX_SNAME_02                 UNUSABLE
INDEX_T1_01                    INDEX_SNAME_03                 USABLE
INDEX_T1_01                    INDEX_SNAME_04                 USABLE
UN_T1_01                       P1                             USABLE
UN_T1_01                       P2                             UNUSABLE
UN_T1_01                       P3                             USABLE
UN_T1_01                       P4                             USABLE

alter index INDEX_T1_01  rebuild partition INDEX_SNAME_02;

alter index UN_T1_01  rebuild partition P2;

col index_name for a20
col partition_name for a20
col tablespace_name for a20
col status for a10
select index_name,null partition_name,status
from user_indexes
where table_name='T1'
and partitioned='NO'
union 
select index_name,partition_name,status from user_ind_partitions
where index_name in
(
select index_name from user_indexes
where table_name='T1'
)
order by 1,2,3
;

INDEX_NAME                     PARTITION_NAME                 STATUS
------------------------------ ------------------------------ --------
INDEX_T1_01                    INDEX_SNAME_01                 USABLE
INDEX_T1_01                    INDEX_SNAME_02                 USABLE
INDEX_T1_01                    INDEX_SNAME_03                 USABLE
INDEX_T1_01                    INDEX_SNAME_04                 USABLE
UN_T1_01                       P1                             USABLE
UN_T1_01                       P2                             USABLE
UN_T1_01                       P3                             USABLE
UN_T1_01                       P4                             USABLE

select count(*) from t1 partition (p2);

COUNT(*)
----------
         0

select count(*) from t11;

COUNT(*)
---------
     1000

--确定数据是否已经切换到新的表空间

SELECT TABLESPACE_NAME 
FROM USER_TAB_PARTITIONS 
WHERE TABLE_NAME='T1' AND PARTITION_NAME='P2';

TABLESPACE_NAME
------------------------------
JASON

---2.3中间表和归档表再次交换数据

create tablespace archive01
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\archive01.DBF'
size 200M  autoextend on next 20M maxsize unlimited
extent management local autoallocate
segment space management auto
;

create tablespace archive02
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\archive02.DBF'
size 200M  autoextend on next 20M maxsize unlimited
extent management local autoallocate
segment space management auto
;

create table t2
(
sid int not null ,
sname  varchar2(50)  not null,
createtime date default sysdate   not null
)
PARTITION BY range(createtime)

PARTITION p1 VALUES LESS THAN ('2013-06-01 00:00:00') tablespace archive01,
PARTITION p2 VALUES LESS THAN ('2013-07-01 00:00:00') tablespace archive01,
PARTITION p3 VALUES LESS THAN ('2013-08-01 00:00:00') tablespace archive01,
PARTITION p4  VALUES LESS THAN (maxvalue) tablespace archive01
) tablespace archive01;

create unique index un_t2_01 on t2(sid,createtime)
tablespace archive02
local;

alter table t2 add constraint pk_t2 primary key(sid,createtime);

select up.table_name,up.partition_name,up.tablespace_name from user_tab_partitions up 
where up.table_name='T2';

--查看分区索引分布情况

col index_name for a20
col partition_name for a20
col tablespace_name for a20
col status for a10
select index_name,null partition_name,tablespace_name,status
from user_indexes
where table_name='T2'
and partitioned='NO'
union 
select index_name,partition_name,tablespace_name,status from user_ind_partitions
where index_name in
(
select index_name from user_indexes
where table_name='T2'
)
order by 1,2,3
;

INDEX_NAME           PARTITION_NAME       TABLESPACE_NAME      STATUS
-------------------- -------------------- -------------------- ----------
UN_T2_01             P1                   ARCHIVE02            USABLE
UN_T2_01             P2                   ARCHIVE02            USABLE
UN_T2_01             P3                   ARCHIVE02            USABLE
UN_T2_01             P4                   ARCHIVE02            USABLE

select count(*) from t2;

COUNT(*)
---------
        0

--交换数据

alter table t2 exchange partition p2 with table t11 ;

select count(*) from t2;

select count(*) from t11;

以上内容转自http://blog.csdn.NET/yangzhawen/article/details/8768943

oracle分区交换技术的更多相关文章

  1. 柯南君 :Oracle 分区技术 之 怎样支撑大数据操作?

    前段时间.看了罗女士( 资深技术顾问 - Oracle 中国 顾问咨询部)关于<大批量数据处理技术的演讲>视频.感觉受益良多,结合多年的知识积累,柯南君给大家分享一下: 交流内容: 一.O ...

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

    一. 说明: OLTP库中有些表数据量大,且每月有持续的大量数据添加.因为历史数据在此库中不再做訪问,而是在另1个OLAP库中做分析.所以会对历史数据迁移至OLAP库中.对这样的历史数据迁移的操作.较 ...

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

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

  4. oracle分区提高篇

      一. 分区表理论知识 Oracle提供了分区技术以支持VLDB(Very Large DataBase).分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中.分区完全对应用透明. Or ...

  5. Oracle安全之 Oracle 11g flashback技术详解

    Oracle11g提供的闪回技术用于对抗人为错误,主要有以下7种技术组成: 闪回查询-(闪回时间查询.闪回版本查询): 闪回数据归档: 闪回事务查询: 闪回事务: 闪回表: 闪回删表: 闪回数据库. ...

  6. linux下安装Oracle时交换空间不足的解决方法

    摘:linux下安装Oracle时交换空间不足的解决方法 linux上安装Oracle时交换空间不足的解决办法 增加交换空间有两种方法: 严格的说,在系统安装完后只有一种方法可以增加swap,那就是本 ...

  7. Oracle体系结构之Oracle分区

    目录 Oracle分区 0 一.Oracle分区理论知识 1 二.分区表的实现方式 1 1.范围分区(range partition table) 1 2.列表分区(list partitioning ...

  8. 01 Oracle分区索引

    Oracle分区索引   索引与表类似,也可以分区: 分区索引分为两类: Locally partitioned index(局部分区索引) Globally partitioned index(全局 ...

  9. Oracle内存管理技术

    1.Oracle内存管理技术 2.配置自动内存管理(AMM) 3.监视自动内存管理(AMM) 4.配置自动共享内存管理(ASMM) 5.配置自动PGA内存管理 Reference 1.Oracle内存 ...

随机推荐

  1. 理解本真的REST架构风格(转,解释的最清楚)

    add by zhj start: Fielding在批判性继承前人研究成果的基础上,建立起来一整套研究和评价软件架构的方法论.这套方法论的核心是“架构风格”这个概念.架构风格是一种研究和评价软件架构 ...

  2. IOS使用Core-Plot画折线图

    关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...

  3. Log表新的RowKey设计,预Split

    1 目前Rawlog表的问题 region数量庞大,空region 率大 共有12791个region 11409空region, 比例为89.19% 剩余的region大小也是极度不均衡,最大的re ...

  4. 201-React顶级API

    一.概述 React是React库的入口点.如果您从<script>标记加载React,则这些顶级API可在React全局中使用.如果你使用npm的ES6,你可以写:import Reac ...

  5. Linux下编译安装MySQL5.6

    [准备工作] 所有操作需要在root用户下 本机测试案例系统信息:centos7.3 安装路径:/usr/local/mysql [安装MySQL] 先安装如下依赖包: $ yum -y instal ...

  6. pssh批量远程管理工具

    Linux下批量管理工具pssh使用记录   pssh是一款开源的软件,使用python实现,用于批量ssh操作大批量机器:pssh是一个可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具 ...

  7. JOJ1202。重新操刀ACM,一天一练!做个简单的题目温习。

    http://ac.jobdu.com/problem.php?pid=1202 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100).   ...

  8. python selenium webdriver方法封装(find_element_by)

    下面是对find_element_by_就行了封装,封装之后的高级方法就是getElement() 下面是具体的代码: def getElement(self, selector): "&q ...

  9. cocos进阶教程(5)CC_CALLBACK_X系列的使用技巧

    CC_CALLBACK_1,CC_CALLBACK_2,CC_CALLBACK_3 这些都是std::bind的宏,数字1,2,3主要表示要占位的数量,也是将来传递参数的数量. // new call ...

  10. rails 数据验证

    validates :money,      :presence => true, :numericality => {:only_integer => true}