doclist 阅读(42) 评论(0)

使用Oracle的在线重定义技术,可以将Oracle的普通表改为分区表。操作如下:

STEP1:测试表是否可以在线重定义,这里以unixdev数据库的LIJIAMAN.BSTEST为例

 EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE('LIJIAMAN','BSTEST', DBMS_REDEFINITION.CONS_USE_PK);

如果表上没有主键,则会报错:
SQL> exec dbms_redefinition.start_redef_table('LIJIAMAN', 'BSTEST', 'BSTEST_TMP');
ORA-12089: 不能联机重新定义无主键的表 "LIJIAMAN"."BSTEST"
ORA-06512: 在 "SYS.DBMS_REDEFINITION", line 56
ORA-06512: 在 "SYS.DBMS_REDEFINITION", line 1498
ORA-06512: 在 line 1

STEP2:创建中间表
除了表名称外,该中间表的形式需要与我们最终的分区表的形式一模一样,如列名,列的数据类型、分区字段,分区方法等。

create table BSTEST_tmp
( TIMEKEY VARCHAR2(40), 
ITEMNAME VARCHAR2(40), 
SITENAME VARCHAR2(40), 
SITEVALUE VARCHAR2(40) 
) 
partition by range (timekey) 
( partition part_201711 values less than('201712010000000000000'), 
partition part_201712 values less than('201801010000000000000'), 
partition part_201801 values less than('201802010000000000000'), 
partition part_201802 values less than('201803010000000000000'),
partition part_201803 values less than('201804010000000000000'), 
partition part_201804 values less than('201805010000000000000'), 
partition part_201805 values less than('201806010000000000000'), 
partition part_201806 values less than('201807010000000000000'), 
partition part_201807 values less than('201808010000000000000'), 
partition part_201808 values less than('201809010000000000000'), 
partition part_201809 values less than('201810010000000000000'), 
partition part_201810 values less than('201811010000000000000'), 
partition part_201811 values less than('201812010000000000000'), 
partition part_201812 values less than('201901010000000000000'), 
partition part_201901 values less than('201902010000000000000'), 
partition part_201902 values less than('201903010000000000000'), 
partition part_201903 values less than('201904010000000000000'), 
partition part_201904 values less than('201905010000000000000'), 
partition part_201905 values less than('201906010000000000000'), 
partition part_201906 values less than('201907010000000000000'), 
partition part_201907 values less than('201908010000000000000'), 
partition part_201908 values less than('201909010000000000000'), 
partition part_201909 values less than('201910010000000000000'), 
partition part_201910 values less than('201911010000000000000'), 
partition part_201911 values less than('201912010000000000000'), 
partition part_201912 values less than('202001010000000000000'), 
partition part_202001 values less than('202002010000000000000'), 
partition part_202002 values less than('202003010000000000000'), 
partition part_202003 values less than('202004010000000000000'), 
partition part_202004 values less than('202005010000000000000'), 
partition part_max values less than(maxvalue) 
);

 

STEP3:执行在线重定义

exec dbms_redefinition.start_redef_table('LIJIAMAN', 'BSTEST', 'BSTEST_TMP');

备注:该步骤执行时间较长,3.7GB的表执行了60s

 

STEP4:将中间表的内容与数据源同步

execute dbms_redefinition.sync_interim_table('LIJIAMAN', 'BSTEST', 'BSTEST_TMP');

 

STEP5:结束在线重定义

exec dbms_redefinition.finish_redef_table('LIJIAMAN', 'BSTEST', 'BSTEST_TMP');

 

STEP6:创建index、创建约束,两种方法创建速度差不多

alter session set db_file_multiblock_read_count=128; 
alter table BSTEST add constraint BSPANELPROCESSDATAITEM_PKS primary key(timekey,itemname,sitename) using index local tablespace users;

 

STEP7:收集统计信息

exec dbms_stats.gather_table_stats(ownname=>'LIJIAMAN', tabname=>'BSTEST', method_opt=> 'FOR ALL INDEXED COLUMNS', estimate_percent => 10, cascade=>true);

备注:该步骤执行时间较长,3.7GB的表执行了36s,如果嫌慢,可以并行收集

 

STEP8:确认无误之后,删除STEP2创建的中间表

drop table BSTEST_TMP;

 



 

如果要了解在线重定义的详细用法及使用注意事项,可参阅官方文档:
1. https://docs.oracle.com/cd/E11882_01/server.112/e25494/tables.htm#ADMIN11677
2. How To Partition Existing Table Using DBMS_REDEFINITION (文档 ID 472449.1)