Oracle並行添加主鍵的方法

環境:Oracle 11.2.0.3
需求:生產一張表因爲前期設計不當,沒有主鍵。現須要添加主鍵,數據量很大,想並行創建。
 
1.直接添加,提示ora-3001:未實施的功能;只能單線程創建主鍵
SQL> alter table t add constraint pk_t primary key (object_id) using index online parallel 2;

alter table t add constraint pk_t primary key (object_id) using index online parallel 2

ORA-03001: 未實施的功能

SQL> alter table t add constraint pk_t primary key (object_id) using index online;

Table altered

SQL> alter table t drop primary key;

Table altered
 
2.考慮先並行添加唯一性索引,再添加主鍵
SQL> create unique index pk_t on t(object_id) parallel 2 online;

Index created

SQL> alter table t add constraint pk_t primary key (object_id);

Table altered

SQL> alter index pk_t noparallel;

Index altered
 
3.對比主鍵和唯一性索引的區別
SQL> desc t
Name           Type          Nullable Default Comments
-------------- ------------- -------- ------- --------
OWNER          VARCHAR2(30)  Y                        
OBJECT_NAME    VARCHAR2(128) Y                        
SUBOBJECT_NAME VARCHAR2(30)  Y                        
OBJECT_ID      NUMBER                                 
DATA_OBJECT_ID NUMBER        Y                        
OBJECT_TYPE    VARCHAR2(19)  Y                        
CREATED        DATE          Y                        
LAST_DDL_TIME  DATE          Y                        
TIMESTAMP      VARCHAR2(19)  Y                        
STATUS         VARCHAR2(7)   Y                        
TEMPORARY      VARCHAR2(1)   Y                        
GENERATED      VARCHAR2(1)   Y                        
SECONDARY      VARCHAR2(1)   Y                        
NAMESPACE      NUMBER        Y                        
EDITION_NAME   VARCHAR2(30)  Y                        

SQL> alter table t drop primary key;

Table altered

SQL> desc t
Name           Type          Nullable Default Comments
-------------- ------------- -------- ------- --------
OWNER          VARCHAR2(30)  Y                        
OBJECT_NAME    VARCHAR2(128) Y                        
SUBOBJECT_NAME VARCHAR2(30)  Y                        
OBJECT_ID      NUMBER        Y                        
DATA_OBJECT_ID NUMBER        Y                        
OBJECT_TYPE    VARCHAR2(19)  Y                        
CREATED        DATE          Y                        
LAST_DDL_TIME  DATE          Y                        
TIMESTAMP      VARCHAR2(19)  Y                        
STATUS         VARCHAR2(7)   Y                        
TEMPORARY      VARCHAR2(1)   Y                        
GENERATED      VARCHAR2(1)   Y                        
SECONDARY      VARCHAR2(1)   Y                        
NAMESPACE      NUMBER        Y                        
EDITION_NAME   VARCHAR2(30)  Y                        
總結:
對於已有大量數據的表沒法並行創建主鍵,能夠先並行創建唯一性索引而後再加上主鍵。
主鍵不能爲空,唯一性索引能夠爲空。
相關文章
相關標籤/搜索