对于全局索引,可以选择分区或者不分区,而且索引分区可以和表分区不对应。只能是B_TREE索引,只支持有前缀的全局索引,ORACLE不会自动维护全局
索引,当我们对表分区维护以后,如果维护时不使用UPDATE GLOBAL INDEXES的话,通过全局索引会失效,必须REBUILD.
全局索引特点:
1.可以分区,也可以不分区,必须是前缀索引,即全局索引的索引列必须是以索引分区键作为前几列。
2.全局索引可以依附于分区表和非分区表。
3.全局分区索引的索引条件可以指向若干个分区,因此,即使只TRUNCATE其中一个分区数据,也要REBUIDL若干个分区或全部索引。
4.全局索引多用于OLTP系统。
5.全局索引只能按范围或散列分区,散列分区10G以后支持。
6.oracle9i之后对分区表MOVE或者TRUNCATE时可用UPDATE GLOBAL INDEXED同步全局分区索引,消耗一定资源换取可用性。
7.如表用A列作分区,索引用B作局部分区索引,若WHERE条件用B来查询,则会扫描所有的表和索引分区,成本会比分区更高,此时可用B做全局索引。
一、创建测试表
SQL> create table range_partition (id number, time date) partition by range (time)
2 (
3 partition p1 values less than (to_date('2013-10-01','yyyy-mm-dd')),
4 partition p2 values less than (to_date('2013-11-01','yyyy-mm-dd')),
5 partition p3 values less than (to_date('2013-12-01','yyyy-mm-dd')),
6 partition p4 values less than (maxvalue)
7 );
create table range_partition (id number, time date) partition by range (time)
(
partition p1 values less than (to_date('2013-10-01','yyyy-mm-dd')),
partition p2 values less than (to_date('2013-11-01','yyyy-mm-dd')),
partition p3 values less than (to_date('2013-12-01','yyyy-mm-dd')),
partition p4 values less than (maxvalue)
)
ORA-01950: 对表空间 'JERRY' 无权限
二、授权
SQL> conn / as sysdba;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as SYS
SQL> alter user jerry default tablespace jerry;
User altered
SQL> grant unlimited tablespace to jerry;
Grant succeeded
三、建两个测试表,一个RANGE分区,一个HASH分区
SQL> create table range_partition (id number, time date) partition by range (time)
2 (
3 partition p1 values less than (to_date('2013-10-01','yyyy-mm-dd')),
4 partition p2 values less than (to_date('2013-11-01','yyyy-mm-dd')),
5 partition p3 values less than (to_date('2013-12-01','yyyy-mm-dd')),
6 partition p4 values less than (maxvalue)
7 );
Table created
SQL> create table hash_partition (id number primary key ,item_id number(8) not null)
2 partition by hash(id)
3 (
4 partition part_01,
5 partition part_02,
6 partition part_03
7 );
Table created
四、建有前缀的全局索引,只要索引的引导列是包含分区键,就是有前缀的分区索引
SQL> create index i_id_global on range_partition(id) global partition by range(id)
2 (
3 partition p1 values less than (200),
4 partition p2 values less than (maxvalue)
5 );
Index created
SQL> create index i_time_global on range_partition(id) global
2 partition by range(time)
3 (
4 partition p1 values less than (to_date('2013-10-01','yyyy-mm-dd')),
5 partition p2 values less than (maxvalue)
6 );
create index i_time_global on range_partition(id) global
partition by range(time)
(
partition p1 values less than (to_date('2013-10-01','yyyy-mm-dd')),
partition p2 values less than (maxvalue)
)
ORA-14038: GLOBAL 分区索引必须加上前缀
SQL>
SQL> create index i_time_global on range_partition(time) global
2 partition by range(time)
3 (
4 partition p1 values less than (to_date('2013-10-01','yyyy-mm-dd')),
5 partition p2 values less than (maxvalue)
6 );
Index created
SQL> select index_name,table_name,partitioning_type,locality,alignment from user_part_indexes where table_name='RANGE_PARTITION';
INDEX_NAME TABLE_NAME PARTITIONING_TYPE LOCALITY ALIGNMENT
------------------------------ ------------------------------ ----------------- -------- ------------
I_ID_GLOBAL RANGE_PARTITION RANGE GLOBAL PREFIXED
I_TIME_GLOBAL RANGE_PARTITION RANGE GLOBAL PREFIXED
SQL> create index idx_hash on RANGE_PARTITION(id,time) global
2 partition by hash(id)
3 (
4 partition p1,
5 partition p2,
6 partition p3,
7 partition p4
8 );
Index created
SQL> select index_name,table_name,partitioning_type,locality,alignment from user_part_indexes where table_name='RANGE_PARTITION';
INDEX_NAME TABLE_NAME PARTITIONING_TYPE LOCALITY ALIGNMENT
------------------------------ ------------------------------ ----------------- -------- ------------
I_ID_GLOBAL RANGE_PARTITION RANGE GLOBAL PREFIXED
I_TIME_GLOBAL RANGE_PARTITION RANGE GLOBAL PREFIXED
IDX_HASH RANGE_PARTITION HASH GLOBAL PREFIXED
五、分区索引重建
SQL> select index_name,partition_name from user_ind_partitions where index_name='I_ID_GLOBAL';
INDEX_NAME PARTITION_NAME
------------------------------ ------------------------------
I_ID_GLOBAL P1
I_ID_GLOBAL P2
SQL> alter index I_ID_GLOBAL rebuild partition P1 online nologging;
Index altered
SQL> alter index I_ID_GLOBAL rebuild partition P2 online nologging;
Index altered
阅读(1577) | 评论(0) | 转发(0) |