這篇文章主要介紹了Oracle如何創(chuàng)建分區(qū)索引,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元賓縣做網(wǎng)站,已為上家服務(wù),為賓縣各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
1、global,它必定是Prefix的。不存在non-prefix的
2、local,它又分成2類:
2.1、prefix:索引的第一個(gè)列等于表的分區(qū)列。
2.2、non-prefix:索引的第一個(gè)列不等于表的分區(qū)列。
LOCAL的索引只能是表的分區(qū)方式,不能自己寫分區(qū)方式。他們是EQUI-Partition的。
GLOBAL索引可以不分區(qū),這個(gè)時(shí)候就是普通的一個(gè)索引。同一個(gè)列只能只有一個(gè)索引,這個(gè)列可以是GLOBAL或者是LOCAL的索引。如果唯一索引所在的列不是表的分區(qū)列,只能建立GLOBAL索引。
例如:分區(qū)表
create table test (id number,data varchar2(100))
partition by RANGE (id)
(
partition p1 values less than (10000) ,
partition p2 values less than (20000) ,
partition p3 values less than (maxvalue)
);
--在ID列上創(chuàng)建一個(gè)LOCAL的索引
SQL>create index id_local on test(id) local;
Index created.
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where index_name='ID_LOCAL';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------- --------
ID_LOCAL P1 10000 USABLE
ID_LOCAL P2 20000 USABLE
ID_LOCAL P3 MAXVALUE USABLE
從上面可以看出索引的分區(qū)和表一樣,即是EQUI-PARTITION
--如果我在表上增加個(gè)分區(qū),則Oracle會(huì)自動(dòng)維護(hù)分區(qū)的索引,注意此時(shí)加分區(qū)必須是用split,直接加會(huì)出錯(cuò)的。例如:
SQL> alter table test add partition p4 values less than (30000);
alter table test add partition p4 values less than (30000)
*
ERROR at line 1:
ORA-14074: partition bound must collate higher than that of the last partition
SQL> alter table test split partition p3 at (30000) into (partition p3, partition p4);
Table altered.
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where index_name='ID_LOCAL';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------- --------
ID_LOCAL P1 10000 USABLE
ID_LOCAL P2 20000 USABLE
ID_LOCAL P3 30000 USABLE
ID_LOCAL P4 MAXVALUE USABLE
SQL> select INDEX_NAME,INDEX_TYPE,TABLE_NAME from dba_indexes where index_name='ID_LOCAL';
INDEX_NAME INDEX_TYPE TABLE_NAME
------------------------------ --------------------------- ------------------------------
ID_LOCAL NORMAL TEST
--刪除id_local索引
SQL>drop index id_local;
Index dropped.
--重新在ID列上創(chuàng)建一個(gè)GLOBAL的索引
SQL> create index id_global on test(id) global;
Index created.
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where index_name='ID_GLOBAL';
no rows selected
SQL> select INDEX_NAME,INDEX_TYPE,TABLE_NAME from dba_indexes where index_name='ID_GLOBAL';
INDEX_NAME INDEX_TYPE TABLE_NAME
------------------------------ --------------------------- ------------------------------
ID_GLOBAL NORMAL TEST
從上面可以看出,它此時(shí)是個(gè)普通索引。dba_ind_partitions里根本就沒(méi)有記錄。
---刪除索引
SQL> drop index id_global;
Index dropped.
注意:不刪會(huì)報(bào):ORA-01408: such column list already indexed
--創(chuàng)建全局索引
SQL> create index i_id_global on test(data) global
partition by range(id)
( partition p1 values less than (10000) ,
partition p2 values less than (MAXVALUE)
);
partition by range(id)
*
ERROR at line 2:
ORA-14038: GLOBAL partitioned index must be prefixed
此錯(cuò)誤表示GLOBAL的索引必須是prefixed,即索引分區(qū)的列,必須是其基表的分區(qū)列。
SQL>create index id_global on test(id) global
partition by range(id)
( partition p1 values less than (10000) ,
partition p2 values less than (MAXVALUE)
);
Index created.
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where index_name='ID_GLOBAL';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------- --------
ID_GLOBAL P1 10000 USABLE
ID_GLOBAL P2 MAXVALUE USABLE
SQL> select INDEX_NAME,INDEX_TYPE,TABLE_NAME from dba_indexes where index_name='ID_GLOBAL';
INDEX_NAME INDEX_TYPE TABLE_NAME
------------------------------ --------------------------- ------------------------------
ID_GLOBAL NORMAL TEST
從上面可以看出,它此時(shí)是個(gè)GLOBAL的索引了。dba_ind_partitions里有記錄。請(qǐng)和上面的做個(gè)比較,加深印象。
我將用下面的例子來(lái)分析到底需要?jiǎng)?chuàng)建什么類型索引好。
create table TT(id number,createdate date)
partition by range(createdate)
(
partition Q1 VALUES LESS THAN (TO_DATE('2012-03-30','YYYY-MM-DD')),
partition Q2 VALUES LESS THAN (TO_DATE('2012-06-30','YYYY-MM-DD')),
partition Q3 VALUES LESS THAN (TO_DATE('2012-09-30','YYYY-MM-DD')),
partition Q4 VALUES LESS THAN (TO_DATE('2012-12-31','YYYY-MM-DD')),
partition Q_OTHERS VALUES LESS THAN (MAXVALUE)
);
注意:只能是to_date,其他的任何函數(shù)都不行,maxvalue必須在最后,他可以包括NULL值。
可以使用id列上的索引。這個(gè)時(shí)候可以在ID列上建立個(gè)local nonprofiex索引
create index index_tt1_local on TT(id) local
( partition p1,
partition p2,
partition p3,
partition p4,
partition p5
);
注意:索引分區(qū)的數(shù)量和其基本的分區(qū)數(shù)量要一樣。
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where index_name='INDEX_TT1_LOCAL';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------- --------
INDEX_TT1_LOCAL P1 TO_DATE(' 2012-03-30 USABLE
00:00:00', 'SYYYY-M
M-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIA
INDEX_TT1_LOCAL P2 TO_DATE(' 2012-06-30 USABLE
00:00:00', 'SYYYY-M
M-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIA
INDEX_TT1_LOCAL P3 TO_DATE(' 2012-09-30 USABLE
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------- --------
00:00:00', 'SYYYY-M
M-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIA
INDEX_TT1_LOCAL P4 TO_DATE(' 2012-12-31 USABLE
00:00:00', 'SYYYY-M
M-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIA
INDEX_TT1_LOCAL P5 MAXVALUE USABLE
如果查詢的語(yǔ)句條件只有一個(gè)createdate,如where createdate='2010-10-19',則這種情況就在createdate上建立一個(gè)local profiex索引
SQL> create index index_TT2_local on TT(createdate) local;
Index created.
SQL> select INDEX_NAME,PARTITION_NAME,HIGH_VALUE,STATUS from dba_ind_partitions where index_name='INDEX_TT2_LOCAL';
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------- --------
INDEX_TT2_LOCAL Q1 TO_DATE(' 2012-03-30 USABLE
00:00:00', 'SYYYY-M
M-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIA
INDEX_TT2_LOCAL Q2 TO_DATE(' 2012-06-30 USABLE
00:00:00', 'SYYYY-M
M-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIA
INDEX_TT2_LOCAL Q3 TO_DATE(' 2012-09-30 USABLE
INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS
------------------------------ ------------------------------ -------------------- --------
00:00:00', 'SYYYY-M
M-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIA
INDEX_TT2_LOCAL Q4 TO_DATE(' 2012-12-31 USABLE
00:00:00', 'SYYYY-M
M-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIA
INDEX_TT2_LOCAL Q_OTHERS MAXVALUE USABLE
從上面查詢可以看出他和表是equi-partitioned.
如果查詢根本就沒(méi)有createdate,而是有像where id>100的條件,則就只能在ID列上建立GLOBAL索引了
SQL> drop index index_tt1_local;
Index dropped.
注意:不刪報(bào)ORA-01408: such column list already indexed
SQL>create index index_tt3_global on TT(id)
global partition by range(id)
(
partition p1 values less than (100000),
partition p2 values less than (200000),
partition p3 values less than (MAXVALUE)
);
從上面可以看出,GLOBAL的索引的分區(qū)數(shù)和其基表是沒(méi)有關(guān)系的。他甚至可以像如下建立索引,即一個(gè)普通索引。但是LOCAL的必須和其基本分區(qū)數(shù)一致。
-創(chuàng)建需先刪索引index_tt3_global
SQL> create index index_tt3_global on TT(id) global;
Index created.
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Oracle如何創(chuàng)建分區(qū)索引”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!