使用exchange 和split partition 来将一个非分区表转换为分区表

     在线重定义表可以将一个非分区表转换成一个分区表。使用exchange 和split partition 也可以将一个非分区表转换为分区表。后者相比较前者,因为不受要转换的数据量影响,因此也更方便、快捷一些。
   
     exchange partition 操作能将一个分区表的一个分区和另一张表的数据互换,这里的互换是segment 头部信息的修改,数据block 并不做改动,因此,和数据量无关,速度会非常快;完成后,再可以按照需要将分区具体split 成其他多个分区。

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

  
 举例如下:
   
-- 建立空的分区表
create  table  test_part 
    partition by range ( created )
    ( partition all_data values less than (MAXVALUE))
    as select owner,object_name,object_type,created  from test2 where  1=2;   
   
   


-- 执行exchange partition 操作
SQL> alter  table test_part exchange  partition all_data with table test2 ;

Table altered


SQL> select *  from  test_part where rownum<4;                --分区表中有数据了,原表没有了数据

OWNER                          OBJECT_NAME                                                                      OBJECT_TYPE        CREATED
------------------------------ -------------------------------------------------------------------------------- ------------------ -----------
SYS                            I_CDEF1                                                                          INDEX              2000-5-12 1
SYS                            IND$                                                                             TABLE              2000-5-12 1
SYS                            FILE$                                                                            TABLE              2000-5-12 1


SQL> select *  from  test2 where rownum<4;

OWNER                          OBJECT_NAME                                                                      OBJECT_TYPE        CREATED
------------------------------ -------------------------------------------------------------------------------- ------------------ -----------

SQL>



-- split range 分区
SQL> alter table  test_part  split  partition all_data AT (to_date('20070101','YYYYMMDD'))  into ( partition before_2007 , partition after_2007  ) ;

Table altered

SQL>



SQL> select table_name,partition_name,high_value,composite,subpartition_count  from  user_tab_partitions;

TABLE_NAME                     PARTITION_NAME                 HIGH_VALUE                                                                       COMPOSITE SUBPARTITION_COUNT
------------------------------ ------------------------------ -------------------------------------------------------------------------------- --------- ------------------
TEST_PART                      AFTER_2007                     MAXVALUE                                                                         NO                         0
TEST_PART                      BEFORE_2007                    TO_DATE(' 2007-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA NO                         0

SQL>




--对 exchange partition with table 操作做了10046 trace,分析trace 可以看出,这个ddl 操作实际上递归出了若干个dml,正是这些dml ,直接修改了某些头部信息,完成了所谓的数据互换工作


oratest@cwdata:/u01/oracle/testdb/9.2.0/admin/test_cwdata/udump/test> grep where  *.trc |  grep -v select |  grep -v "WAIT #"
delete from superobj$ where subobj# = :1
update tab$ set ts#=:2,file#=:3,block#=:4,bobj#=decode(:5,0,null,:5),tab#=decode(:6,0,null,:6),intcols=:7,kernelcols=:8,clucols=decode(:9,0,null,:9),audit$=:10,flags=:11,pctfree$=:12,pctused$=:13,initrans=:14,maxtrans=:15,rowcnt=:16,blkcnt=:17,empcnt=:18,avgspc=:19,chncnt=:20,avgrln=:21,analyzetime=:22,samplesize=:23,cols=:24,property=:25,degree=decode(:26,1,null,:26),instances=decode(:27,1,null,:27),dataobj#=:28,avgspc_flb=:29,flbcnt=:30,trigflag=:31,spare1=:32,spare2=decode(:33,0,null,:33),spare4=:34,spare6=:35 where obj#=:1
delete from superobj$ where subobj# = :1
update tab$ set ts#=:2,file#=:3,block#=:4,bobj#=decode(:5,0,null,:5),tab#=decode(:6,0,null,:6),intcols=:7,kernelcols=:8,clucols=decode(:9,0,null,:9),audit$=:10,flags=:11,pctfree$=:12,pctused$=:13,initrans=:14,maxtrans=:15,rowcnt=:16,blkcnt=:17,empcnt=:18,avgspc=:19,chncnt=:20,avgrln=:21,analyzetime=:22,samplesize=:23,cols=:24,property=:25,degree=decode(:26,1,null,:26),instances=decode(:27,1,null,:27),dataobj#=:28,avgspc_flb=:29,flbcnt=:30,trigflag=:31,spare1=:32,spare2=decode(:33,0,null,:33),spare4=:34,spare6=:35 where obj#=:1
update tabpart$ set dataobj# = :1, part# = :2, ts# = :3, file# = :4, block# = :5, pctfree$ = :6, pctused$ = :7, initrans = :8, maxtrans = :9, flags = :10, analyzetime = :11, samplesize = :12, rowcnt = :13, blkcnt = :14, empcnt = :15, avgspc = :16, chncnt = :17, avgrln = :18 where obj# = :19 and bo# = :20
update obj$ set obj#=:6,type#=:7,ctime=:8,mtime=:9,stime=:10,status=:11,dataobj#=:13,flags=:14,oid$=:15,spare1=:16, spare2=:17 where owner#=:1 and name=:2 and namespace=:3 and(remoteowner=:4 or remoteowner is null and :4 is null)and(linkname=:5 or linkname is null and :5 is null)and(subname=:12 or subname is null and :12 is null)
update obj$ set obj#=:6,type#=:7,ctime=:8,mtime=:9,stime=:10,status=:11,dataobj#=:13,flags=:14,oid$=:15,spare1=:16, spare2=:17 where owner#=:1 and name=:2 and namespace=:3 and(remoteowner=:4 or remoteowner is null and :4 is null)and(linkname=:5 or linkname is null and :5 is null)and(subname=:12 or subname is null and :12 is null)
update obj$ set obj#=:6,type#=:7,ctime=:8,mtime=:9,stime=:10,status=:11,dataobj#=:13,flags=:14,oid$=:15,spare1=:16, spare2=:17 where owner#=:1 and name=:2 and namespace=:3 and(remoteowner=:4 or remoteowner is null and :4 is null)and(linkname=:5 or linkname is null and :5 is null)and(subname=:12 or subname is null and :12 is null)
oratest@cwdata:/u01/oracle/testdb/9.2.0/admin/test_cwdata/udump/test>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值