设为首页 收藏本站
查看: 1217|回复: 0

[经验分享] oracle分区交换技术

[复制链接]

尚未签到

发表于 2017-7-5 11:16:51 | 显示全部楼层 |阅读模式
  交换分区的操作步骤如下:

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-390825-1-1.html 上篇帖子: Day11-协程/异步IO/RabbitMQ 下篇帖子: ASP.NET WebAPI HTTPS
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表