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

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. python基础===包的导入和__init__.py的介绍

    转自:https://www.cnblogs.com/botoo/p/8241522.html 调用同级目录: – src |– mod.py |– test.py 若在程序test.py中导入模块m ...

  2. Qt:QPushButton 单击、双击响应区分

    开发环境:win10+vs2015+qt5.9.1 背景:QPushButton的双击事件虽然一直有,但是在双击完成之前,总会响应到单击的事件处理或者连接槽,使用很不方便.自己子类化了一个QPushB ...

  3. code sandbox & mlflow

    https://codesandbox.io/ https://www.jianshu.com/p/d70b25bf3cf4 https://my.oschina.net/u/2306127/blog ...

  4. Hive与ES整合

    https://www.elastic.co/guide/en/elasticsearch/hadoop/current/hive.html 注:添加的第三方jar必须位于namenode下,否则依然 ...

  5. ReactiveCocoa 初学者使用

    skip 跳过几个信号,不接受 filter :过滤 ignore:忽略某一个值 take:从开始一共取N次的信号 ignoreValues 这个比较极端,忽略所有值,只关心Signal结束,也就是只 ...

  6. C#之父

    来自为知笔记(Wiz)

  7. 禁止复制操作 --《C++必知必会》条款32

    class NoCopy{ private: //声明为私有的,则外部不可访问,即:不可复制 NoCopy(const NoCopy & );//复制构造函数 NoCopy & ope ...

  8. Codeforces Round #533 (Div. 2) Solution

    A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...

  9. 20145311 《Java程序设计》第七周学习总结

    20145311 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章 Lambda Lambda表达式会使程序更加地简洁,在平行设计的时候,能够进行并行处理. 第十三章 时间与 ...

  10. STRIDE 和 DREAD

    目录 STRIDE 和 DREAD 背景 STRIDE DREAD 注释 STRIDE 和 DREAD 背景 STRIDE 和 DREAD 是最常用也是最好用的安全模型 STRIDE 主要负责对安全风 ...