Oracle非分區索引,全局分區索引和本地分區索引。

1.若是按照索引是否分區做爲劃分依據,Oracle 的索引類型能夠分爲非分區索引,全局分區索引和本地分區索引。sql

2.建立演示實例less

--建立非分區表
create
table test_partition_01( number_1 number, number_2 number, string_1 varchar2(10), string_2 varchar2(20) ); -- 寫入數據 insert into test_partition_01(number_1, number_2, string_1, string_2) select dbms_random.random() as number_1, round(dbms_random.value(0, 100000)) as number_2, dbms_random.string(opt => 'A', len => 1) as String_1, dbms_random.string(opt => 'p', len => 10) as String_2 from dual connect by rownum < 100000; commit;
--試圖建立本地分區索引 報錯
CREATE INDEX ix_test_partition_01_1 ON test_partition_01(number_1) local (PARTITION p1, PARTITION p2, PARTITION p3);---ORA-14016 ---建立普通索引 CREATE INDEX ix_test_partition_01_1 ON test_partition_01(number_1); --建立 全局分區索引 CREATE INDEX ix_test_partition_01_2 ON test_partition_01(number_2) GLOBAL PARTITION BY RANGE (number_2) (PARTITION p1 VALUES LESS THAN (10000), PARTITION p2 VALUES LESS THAN (55000), PARTITION p3 VALUES LESS THAN (MAXVALUE)); 結論:非分區表能夠建立普通索引和全局分區索引不能建立本地分區索引。 ------------------------------------------------------------------------------- create table test_partiton_02( number_1 number, number_2 number, string_1 varchar2(10), string_2 varchar2(20) ) partition by range(number_2) ( partition p1 values less than (10000), partition p2 values less than (20000), partition p3 values less than (50000), partition p4 values less than (70000), partition p5 values less than (maxvalue) );
--試圖建立本地分區索引
CREATE INDEX ix_test_partiton_02_1 ON test_partiton_02(number_1) local (PARTITION p1, PARTITION p2, PARTITION p3);--ora-14024 索引的分區數必須等於基礎表的分區數 --建立本地分區索引 CREATE INDEX ix_test_partiton_02_1 ON test_partiton_02(number_1) local (PARTITION p1, PARTITION p2, PARTITION p3, PARTITION p4, PARTITION p5 );--drop index ix_test_partiton_02_1; CREATE INDEX ix_test_partiton_02_1 ON test_partiton_02(number_1) local; --和上面的建立方式等效 --drop index ix_test_partiton_02_1; CREATE INDEX ix_test_partiton_02_1 ON test_partiton_02(number_1) ; --默認建立的是 非分區索引,分區索引才分 全局索引仍是本地索引 ; ---建立全局分區索引 CREATE INDEX ix_test_partiton_02_2 ON test_partiton_02(number_2) GLOBAL PARTITION BY RANGE (number_2) (PARTITION p1 VALUES LESS THAN (10000), PARTITION p2 VALUES LESS THAN (55000), PARTITION p3 VALUES LESS THAN (MAXVALUE)); ---寫入測試數據 insert into test_partiton_02(number_1, number_2, string_1, string_2) select dbms_random.random() as number_1, round(dbms_random.value(0, 100000)) as number_2, dbms_random.string(opt => 'A', len => 1) as String_1, dbms_random.string(opt => 'p', len => 10) as String_2 from dual connect by rownum < 100001; commit; --分析表 analyze table test_partiton_02 compute statistics; --查看 普通索引是否可用 select * from user_indexes t where t.INDEX_NAME in('IX_TEST_PARTITON_02_1','IX_TEST_PARTITON_02_2','IX_TEST_PARTITON_02_3'); ---查看分區索引是否 可用 select * from user_ind_partitions t where t.INDEX_NAME in('IX_TEST_PARTITON_02_1','IX_TEST_PARTITON_02_2','IX_TEST_PARTITON_02_3'); ---改變分區,查看普通索引和分區索引是否可用 -- 必須調整最後一個分區的大小,因此若是最後一個分區指定了最大值 必須先刪除,再添加 alter table test_partiton_02 add partition p6 values less than (90000); alter table test_partiton_02 drop partition p5; --- 結論 改變分區 普通索引和全局分區索引都會失效 只有本地分區索引好使
PS:最好用如下語句查看索引的定義語句 select dbms_metadata.get_ddl(object_type => 'INDEX', name => 'IX_TEST_PARTITON_02_1') FROM DUAL; select dbms_metadata.get_ddl(object_type => 'INDEX', name => 'IX_TEST_PARTITON_02_2') FROM DUAL;
--- 發現了 plsql developer Version 11.0.5.1790 (64 bit) 的一個 bug 在用View 查看 DDL時 沒有反應真實的狀況
相關文章
相關標籤/搜索