12c以前非分區表需要轉換為分區, 如果不停業務的話可以使用線上重定義,隻有在表進行切換的時候會有短暫的鎖表。 12c 中alter table online clause 實作了表上現有的索引有效,又不停業務。
測試一:非分區表轉分區表,索引轉換為oracle内部規則。
-- 建非分區表
create table andy_clause as select * from user_objects where object_id is not null;
--建立非分區表索引
create index idx_oid on andy_clause( object_id );
create index idx_ctime_oname on andy_clause( created, object_name );
create bitmap index idx_b_otype on andy_clause(object_type);
--檢視表索引
SQL>
col column_name for a40
SELECT index_name, column_name, descend,COLUMN_POSITION FROM user_ind_columns WHERE table_name = 'ANDY_CLAUSE';
INDEX_NAME COLUMN_NAME DESC COLUMN_POSITION
------------------------- ---------------------------------------- ---- ---------------
IDX_OID OBJECT_ID ASC 1
IDX_CTIME_ONAME CREATED ASC 1
IDX_CTIME_ONAME OBJECT_NAME ASC 2
IDX_B_OTYPE OBJECT_TYPE ASC 1
-- 檢視索引狀态
col index_name for a25
select table_name,index_name,status,blevel,leaf_blocks from user_Indexes where table_name ='ANDY_CLAUSE';
TABLE_NAME INDEX_NAME STATUS BLEVEL LEAF_BLOCKS
------------------------- ------------------------- -------- ---------- -----------
ANDY_CLAUSE IDX_OID VALID 0 1
ANDY_CLAUSE IDX_CTIME_ONAME VALID 0 1
ANDY_CLAUSE IDX_B_OTYPE VALID 0 1
--轉換目前表為分區以 object_id 字段, interval分區表,直接 update index,使用ORACLE有自己的索引轉換規則。
SQL>
alter table andy_clause modify
partition by range (object_id) interval (5)
(
partition p1 values less than (73527)
) online
update indexes;
Table altered.
說明:update index ,沒有指定寫local 或global選項, ORACLE有自己的索引轉換規則
col TABLE_NAME for a25
select table_name,index_name,INDEX_TYPE,status,blevel,leaf_blocks from user_Indexes where table_name ='ANDY_CLAUSE';
ANDY_CLAUSE IDX_OID N/A 0 4
ANDY_CLAUSE IDX_B_OTYPE N/A 0 4
SQL> COL INDEX_OWNER FOR A20;
SQL> select INDEX_OWNER,index_name,status,blevel,leaf_blocks from dba_ind_partitions where INDEX_OWNER='C##ANDY'and Index_name='IDX_OID';
INDEX_OWNER INDEX_NAME STATUS BLEVEL LEAF_BLOCKS
-------------------- ------------------------- -------- ---------- -----------
C##ANDY IDX_OID USABLE 0 1
COL INDEX_OWNER FOR A20;
select INDEX_OWNER,index_name,status,blevel,leaf_blocks from dba_ind_partitions where INDEX_OWNER='C##ANDY'and Index_name='IDX_B_OTYPE';
C##ANDY IDX_B_OTYPE USABLE 0 1
說明:user_Indexes 是主要計量一個index的相關資訊的,如果分區索引為local index 它不能記錄狀态,就為 N/A ,分區 local index要用視圖 dba_ind_partitions 查狀态。
-- 檢視分區情況
select table_name,partition_name,PARTITION_POSITION,tablespace_name,HIGH_VALUE from user_tab_partitions where table_name='ANDY_CLAUSE';
TABLE_NAME PARTITION_NAME PARTITION_POSITION TABLESPACE_NAME HIGH_VALUE
------------------------- ------------------------- ------------------ ------------------------------ -----------
ANDY_CLAUSE P1 1 USERS 73527
ANDY_CLAUSE SYS_P341 2 USERS 73532
ANDY_CLAUSE SYS_P343 3 USERS 73592
ANDY_CLAUSE SYS_P342 4 USERS 73597
測試二:非分區表轉分區表,索引轉換為自己設定規則。
SQL> create table andy_clause02 as select * from user_objects where object_id is not null;
SQL>
create index idx_oid on andy_clause02( object_id );
create index idx_ctime_oname on andy_clause02( created, object_name );
create bitmap index idx_b_otype on andy_clause02(object_type);
--轉換目前表為分區以 object_id 字段, interval分區表,update indexes 時,自己指定local 或global選項
alter table andy_clause02 modify
update indexes
( idx_oid local,
idx_ctime_oname global partition by range (created)
partition ix2_p1 values less than (date '2017-05-01'),
partition ix2_p2 values less than (maxvalue)
)
);
col COLUMN_NAME for a25
SELECT index_name, column_name, descend,COLUMN_POSITION FROM user_ind_columns WHERE table_name = 'ANDY_CLAUSE02';
INDEX_NAME COLUMN_NAME DESC COLUMN_POSITION
------------------------- ------------------------- ---- ---------------
IDX_OID OBJECT_ID ASC 1
IDX_CTIME_ONAME CREATED ASC 1
IDX_CTIME_ONAME OBJECT_NAME ASC 2
IDX_B_OTYPE OBJECT_TYPE ASC 1
select table_name,index_name,status,blevel,leaf_blocks from user_Indexes where table_name ='ANDY_CLAUSE02';
ANDY_CLAUSE02 IDX_OID N/A 1 113
ANDY_CLAUSE02 IDX_CTIME_ONAME N/A 2 432
ANDY_CLAUSE02 IDX_B_OTYPE N/A 1 3
文章可以轉載,必須以連結形式标明出處。
本文轉自 張沖andy 部落格園部落格,原文連結: http://www.cnblogs.com/andy6/p/6855283.html ,如需轉載請自行聯系原作者