C/C++教程

oracle 分区交换

本文主要是介绍oracle 分区交换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

需求

        一个几亿条数据的分区表,要把一些老数据分出来,放一个新建的表上。按时间划分,指定时间以前的放在老数据表,指定时间以后的数据保留在原表,然后把备份老数据表空间truncate掉。按照分区操作分区交换是最好的解决办法。

分区交换技术可以实现数据快速转移,所以在数据加载提速,历史数据清理等方面特别有用。分区交换技术实际上只修改了数据字典中的数据物理段位置,而不是实际的移动数据,所以速度很快。

交换分区的操作步骤如下

        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), 
  PARTITION p2 VALUES LESS THAN (10000), 
  PARTITION p3  VALUES LESS THAN (maxvalue) 
); 

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

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

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

-- 2 造数据
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;  

-- 2.1 核对数据
select count(*) from t1;
select count(*) from t11; 
select count(*) from t12; 
select count(*) from t13; 

-- 3 分区交换
alter table t1 exchange partition p1 with table t11; 
alter table t1 exchange partition p2 with table t12; 
alter table t1 exchange partition p3 with table t13; 

-- 3.1 根据分区核对数据
select count(*) from t1 partition (p1);
select count(*) from t1 partition (p2);
select count(*) from t1 partition (p3);


 

这篇关于oracle 分区交换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!