Oracle在線重定義(online redefinition)--將普通表改成分區表

使用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 1session

STEP2:建立中間表
除了表名稱外,該中間表的形式須要與咱們最終的分區表的形式如出一轍,如列名,列的數據類型、分區字段,分區方法等。oracle

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:執行在線重定義less

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

備註:該步驟執行時間較長,3.7GB的表執行了60s測試

 

STEP4:將中間表的內容與數據源同步spa

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

 

STEP5:結束在線重定義unix

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

 

STEP6:建立index、建立約束,兩種方法建立速度差很少code

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:收集統計信息server

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

備註:該步驟執行時間較長,3.7GB的表執行了36s,若是嫌慢,能夠並行收集htm

 

STEP8:確認無誤以後,刪除STEP2建立的中間表

drop table BSTEST_TMP;

 



 

若是要了解在線重定義的詳細用法及使用注意事項,可參閱官方文檔:1. https://docs.oracle.com/cd/E11882_01/server.112/e25494/tables.htm#ADMIN116772. How To Partition Existing Table Using DBMS_REDEFINITION (文檔 ID 472449.1)

相關文章
相關標籤/搜索