分享

oracle根据已有表及数据创建表分区并导入数据

 姑苏慕容凡 2015-07-10
oracle根据已有表及数据创建表分区并导入数据
 
假设情景: 
现有System.Test表,数据量过千万,处于ts_Test表空间中。 
表中有列A,将A=6与A小于6的数据进行分区 
  www.2cto.com  
确保不会有外部程序修改需要建表分区的表 
 
1. 对需要重建表分区的表进行备份,导出dmp,防止数据丢失 
Sql代码  
exp 用户名/密码@tns名 file=c:/test.dmp log=c:/test.log full=n rows=y buffer=10240000 tables=System.Test  
 
2. 创建临时表,用来回导数据 
Sql代码  
create table system.Test_Bak  
tablespace ts_Test  
as  
select * from System.Test;  
 
3. 校验数据行数   www.2cto.com  
Sql代码  
select count('x') c1 from System.Test;  
select count('x') c2 from System.Test_Bak;  
 
如果行数不一致需查找原因 
 
4. 重建表 
 
Sql代码  
truncate table System.Test;  
drop table System.Test;  
 
 
Sql代码  
create table System.Test  
tablespace ts_Test  
PARTITION BY RANGE(A)  
(  
  PARTITION P1 VALUES LESS THAN ('6')  
      TABLESPACE TS_TEST  
   ,  
  PARTITION P2 VALUES LESS THAN ('7')  
      TABLESPACE TS_TEST,  
  PARTITION P3 VALUES LESS THAN (MAXVALUE)  
      TABLESPACE TS_TEST  
)  
as  
select   
  from System.Test_Bak;  
 
   第4步执行完之后,表里的数据就分散到了P1和P2分区中 
  www.2cto.com  
5. 重建索引,将原有表中的索引再建到System.Test表中。 
 
6. 检查分区 
Sql代码  
   
  select decode(A,'1','1','2','1','3','1','4','1','5','1',a),  
count('x') n  
  from System.Test  
 group by decode(A,'1','1','2','1','3','1','4','1','5','1',a)  
 order by decode(A,'1','1','2','1','3','1','4','1','5','1',a);  
 
select count('x')  n1 from System.Test partition (p1);  
  
select count('x')  n2 from System.Test partition (p2);  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多