OCM實驗-使用線上重定義方式遷移表
要求使用線上重定義方式遷移表
優點:支援線上讀/寫,不影響大查詢,對海量資料的表進行操作效率非常好,實質只更新資料字典,不移動資料
缺點:線上重定義後表上的主鍵、索引不會同步過來,必須重建,只變換表名.在finish轉換過程中原表是鎖定狀態
官方文件: PL/SQL Packages and Types Reference -> 搜尋線上重定義dbms_redefinition
1.建立環境
SYS@testdb>create tablespace mssm datafile '/oracle/ora10g/oradata/mssm01.dbf' size 20M extent management local segment space management manual;
Tablespace created.
SYS@testdb>create tablespace assm datafile '/oracle/ora10g/oradata/assm01.dbf' size 20m extent management local segment space management auto;
Tablespace created.
SYS@testdb>select segment_space_management,tablespace_name from dba_tablespaces where tablespace_name in ('MSSM','ASSM');
SEGMEN TABLESPACE_NAME
------ ------------------------------
AUTO ASSM
MANUAL MSSM
在mssm表空間上,建立測試表t,並插入測試資料
LEO1@testdb>create table t (id int constraint pk_t primary key) tablespace mssm;
Table created.
LEO1@testdb>insert into t values(1);
1 row created.
LEO1@testdb>insert into t values(2);
1 row created.
LEO1@testdb>insert into t values(3);
1 row created.
LEO1@testdb>commit;
Commit complete.
LEO1@testdb>select * from t;
ID
----------
1
2
3
LEO1@testdb>
LEO1@testdb>select table_name,tablespace_name from user_tables;
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
T MSSM
2.使用線上重定義方式將t表從mssm表空間遷移到assm表空間
1)驗證是否可以基於主鍵方式遷移
LEO1@testdb>exec dbms_redefinition.can_redef_table(uname=>'leo1',tname=>'t',options_flag=>dbms_redefinition.cons_use_pk);
PL/SQL procedure successfully completed.
2)建立線上重定義中間表
LEO1@testdb>create table t_interim (id int) tablespace assm;
Table created.
LEO1@testdb>select table_name,tablespace_name from user_tables where table_name in ('T','T_INTERIM');
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
T MSSM
T_INTERIM ASSM
3)線上重定義表
LEO1@testdb>exec dbms_redefinition.start_redef_table(uname=>'leo1',orig_table=>'t',int_table=>'t_interim');
PL/SQL procedure successfully completed.
LEO1@testdb>select * from t;
ID
----------
1
2
3
LEO1@testdb>select * from t_interim;
ID
----------
1
2
3
手動同步
LEO1@testdb>exec dbms_redefinition.sync_interim_table(uname=>'leo1',orig_table=>'t',int_table=>'t_interim');
PL/SQL procedure successfully completed.
完成線上重定義
******************************************************************
我在這之前做了一個drop 表的動作,證明在遷移過程中,源表是可以刪除的。
LEO1@testdb>drop table t_interim;
drop table t_interim
*
ERROR at line 1:
ORA-12083: must use DROP MATERIALIZED VIEW to drop "LEO1"."T_INTERIM"
LEO1@testdb>drop table t;
Table dropped.
LEO1@testdb>
LEO1@testdb>select table_name from user_tables;
TABLE_NAME
------------------------------
T_INTERIM
這時,完成線上遷移將無法進行
LEO1@testdb>exec dbms_redefinition.finish_redef_table(uname=>'leo1',orig_table=>'t',int_table=>'t_interim');
BEGIN dbms_redefinition.finish_redef_table(uname=>'leo1',orig_table=>'t',int_table=>'t_interim'); END;
*
ERROR at line 1:
ORA-23540: Redefinition not defined or initiated
ORA-06512: at "SYS.DBMS_REDEFINITION", line 76
ORA-06512: at "SYS.DBMS_REDEFINITION", line 1377
ORA-06512: at line 1
****************************************************8
重做上面的操作後,可以完成線上重定義操作了
LEO1@testdb>exec dbms_redefinition.finish_redef_table(uname=>'leo1',orig_table=>'t',int_table=>'t_interim');
PL/SQL procedure successfully completed.
檢查表所在的表空間。
LEO1@testdb>select table_name,tablespace_name from user_tables where table_name in ('T','T_INTERIM');
TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------
T ASSM
T_INTERIM MSSM
檢查索引。
LEO1@testdb>select index_name,table_name,tablespace_name,status from user_indexes where index_name='PK_T';
INDEX_NAME TABLE_NAME TABLESPACE_NAME STATUS
------------------------------ ------------------------------ ------------------------------ --------
PK_T T_INTERIM MSSM VALID
LEO1@testdb>alter index pk_t rebuild tablespace assm online;
Index altered.
LEO1@testdb>select index_name,table_name,tablespace_name,status from user_indexes where index_name='PK_T';
INDEX_NAME TABLE_NAME TABLESPACE_NAME STATUS
------------------------------ ------------------------------ ------------------------------ --------
PK_T T_INTERIM ASSM VALID
額,pk_t主鍵在中間表t_interim上。因此不能使用rebuild重建
需要給t表新建主鍵。
LEO1@testdb>alter table t add constraint pk_t_id primary key(id);
Table altered.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11590946/viewspace-1082278/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 線上重定義方式將普通表修改為分割槽表
- 線上重定義與普通表改為分割槽表
- 在oracle 9i下線上重定義表Oracle
- 線上重定義的操作步驟__普通錶轉換為分割槽表的實驗案例
- 壓縮錶轉非壓縮表(線上重定義)
- 指令碼:線上重定義,從普通表到分割槽表,redefinition_table.sh指令碼
- 線上redis遷移思路Redis
- 05-自己建立mapmodel自定義遷移方式
- 【Redis 技術探索】「資料遷移實戰」手把手教你如何實現線上 + 離線模式進行遷移 Redis 資料實戰指南(scan模式遷移)Redis模式
- 線上資料遷移,數字化時代的必修課 —— 京東雲資料遷移實踐
- Microsoft SQL Server 遷移利器,Babelfish for Aurora PostgreSQL 上線!ROSSQLServerBabel
- 【BUILD_ORACLE】使用Oracle資料泵線上不停機克隆/遷移PDBUIOracle
- 【Redis 技術探索】「資料遷移實戰」手把手教你如何實現線上 + 離線模式進行遷移Redis資料實戰指南(離線同步資料)Redis模式
- 雲端IDE如何重定義開發體驗IDE
- 同事使用 insert into select 遷移資料,開開心心上線,上線後被公司開除!
- db2匯出資料庫定義及遷移資料DB2資料庫
- Redis Cluster高可用叢集線上遷移操作記錄Redis
- 鐵威馬NAS如何進行線上陣列遷移陣列
- mysql 大表mysqldump遷移方案MySql
- 容器和映象轉化、遷移方式【轉】
- xtts遷移實踐TTS
- 達達埋點遷移京東子午線實踐
- MySQL 遷移表空間,備份單表MySql
- Django實踐(二)——使用模型類定義資料表,實現表單頁面跳轉Django模型
- 實戰程式碼(二):Springboot Batch實現定時資料遷移Spring BootBAT
- 聯機重定義表在10g的改進
- 資料遷移(1)——通過資料泵表結構批量遷移
- 1.2 使用OGG實現全量和增量遷移
- table/index/LOBINDEX遷移表空間Index
- 在Sequelize中使用遷移
- 利用offline datafile檔案方式遷移資料
- 用begin backup的方式遷移資料庫資料庫
- mongodb資料遷移2種方式比較MongoDB
- 有贊大資料離線叢集遷移實戰大資料
- 從 SQL Server 到 MySQL(二):線上遷移,空中換髮動機ServerMySql
- 資料表結構更新後,遷移檔案怎麼使用?
- 【資料遷移】XTTS跨平臺傳輸表空間(1.傳統方式)TTS
- .net6中使用EF core多個上下文遷移方式
- 資料庫上雲實踐:使用Ora2pg進行資料庫遷移資料庫