在线重定义表可以将一个非分区表转换成一个分区表。使用exchange 和split partition 也可以将一个非分区表转换为分区表。后者相比较前者,因为不受要转换的数据量影响,因此也更方便、快捷一些。

     exchange partition 操作能将一个分区表的一个分区和另一张表的数据互换,这里的互换是segment 头部信息的修改,数据block 并不做改动,因此,和数据量无关,速度会非常快;完成后,再可以按照需要将分区具体split 成多个分区。

步骤:
1.建立一个空的,只有一个分区的同构分区表
2.exchange partition with table 来互换数据
3.split 分区表的分区为合适的个数

-- 建立非分区表
create table test_objects as select * from user_objects;

-- 建立空的分区表
 create table test_part_objects
 partition by range(object_id)
 (partition max_data values less than (maxvalue))
 as select * from test_objects where 1=2; select count(*) from test_objects;
 1579
 select count(*) from test_part_objects;
 0-- 执行exchange partition操作
 alter table test_part_objects exchange partition max_data with table test_objects;--分区表中有数据了,原表没有了数据
 select count(*) from test_objects;
 0
 select count(*) from test_part_objects;
 1579
 select count(*) from test_part_objects partition(max_data);
 1579--split分拆分区
 alter table test_part_objects split partition max_data
 at (54000) into (partition part_54000,partition max_data);select count(*) from test_part_objects partition(max_data);
 172-- 再次执行exchange partition,将max_data分区中的数据交换到test_objects表
 alter table test_part_objects exchange partition max_data with table test_objects;