日期:2014-05-16  浏览次数:20409 次

oracle根据已有表及数据创建表分区并导入数据
假设情景:
现有System.Test表,数据量过千万,处于ts_Test表空间中。
表中有列A,将A=6与A小于6的数据进行分区


确保不会有外部程序修改需要建表分区的表


1. 对需要重建表分区的表进行备份,导出dmp,防止数据丢失
exp 用户名/密码@tns名 file=c:/test.dmp log=c:/test.log full=n rows=y buffer=10240000 tables=System.Test

2. 创建临时表,用来回导数据
    create table system.Test_Bak
    tablespace ts_Test
    as
    select * from System.Test;


3. 校验数据行数
  select count('x') c1 from System.Test;
  select count('x') c2 from System.Test_Bak;

如果行数不一致需查找原因

4. 重建表
 
  truncate table System.Test;
  drop table System.Test;
  

 
  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分区中

5. 重建索引,将原有表中的索引再建到System.Test表中。

6. 检查分区
 
  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);