全局分区索引实质上意味着该索引与数据表有不同的分区方案,且是基于不同于数据表的一个或一组分区列进行分区的。这主要是为了提高数据库中数据查询的性能。基于用户对给定表的查询,为提高查询性能,可以在给定的查询列上创建全局分区的索引。
可以创建以下几种类型的全局分区索引:
.范围
.哈希
为了说明基于范围的全局分区索引,假定你发现有很多的查询要访问employees表中的manager_id列,那么就可以在manager_id列上创建分区索引,它的分区完全独立于表。查看如下范围分区索引的例子:
SQL> create table employees_jy as select * from employees; Table created. SQL> create index employees_gi1 2 on employees_jy(manager_id) 3 global 4 partition by range(manager_id) 5 ( 6 partition manager_100 values less than(100), 7 partition manager_200 values less than(200), 8 partition manager_300 values less than(300), 9 partition manager_400 values less than(400), 10 partition manager_500 values less than(500), 11 partition manager_600 values less than(600), 12 partition manager_700 values less than(700), 13 partition manager_800 values less than(800), 14 partition manager_900 values less than(900), 15 partition manager_max values less than(maxvalue) 16 ); Index created.
你可以在分区表或非分区表上创建全局分区索引。换句话说,创建全局分区索引并不需要对表进行分区。正如本章后面所讨论的,因为分区表的全局分区索引维护起来不太方便,全局分区索引并不常用,所以通常很少甚至不用它来提高查询性能,特别是在非分区表中。
基于范围的全局分区索引,必须始终为索引指定一个最大值,即用maxvalue作为最大值。这保证了新插入到相应表的任何数据将在分局分区索引中有地方保存。换句话说,对于全局分区索引,它永远不会有“越界”的情况。如果不在全局分区索引中指定上限分区,Oracle将不允许创建索引,若执行下列语句,就会出现以下错误提示信息:
SQL> create index employees_gi2 2 on employees_jy(manager_id) 3 global 4 partition by range(manager_id) 5 ( 6 partition manager_100 values less than(100), 7 partition manager_200 values less than(200), 8 partition manager_300 values less than(300), 9 partition manager_400 values less than(400), 10 partition manager_500 values less than(500), 11 partition manager_600 values less than(600), 12 partition manager_700 values less than(700), 13 partition manager_800 values less than(800), 14 partition manager_900 values less than(900) 15 ); ) * ERROR at line 15: ORA-14021: MAXVALUE must be specified for all columns
由于每个全局分区索引吕必须包含一个上限分区,因此把分区添加到全局分区索引的唯一方式就是,使用alter index … split分区命令。例如:
SQL> alter index employees_gi1 split partition manager_max at(1000) into (partition manager_max,partition manager_1000); Index altered. SQL> select partition_name,status from user_ind_partitions where index_name='EMPLOYEES_GI1'; PARTITION_NAME STATUS -------------------------------------------------------------------------------------------------------------------------------- -------- MANAGER_100 USABLE MANAGER_1000 USABLE MANAGER_200 USABLE MANAGER_300 USABLE MANAGER_400 USABLE MANAGER_500 USABLE MANAGER_600 USABLE MANAGER_700 USABLE MANAGER_800 USABLE MANAGER_900 USABLE MANAGER_MAX USABLE 11 rows selected. SQL>
还可以删除全局分区索引的部分分区。然而,这样做时,如果被删除的分区包含索引条目,上限比它高的相邻分区就被标记为不可用。在下面的代码中,查询了删除分区manager_200前该索引各分区的状态:
SQL> select partition_name,status from user_ind_partitions where index_name='EMPLOYEES_GI1'; PARTITION_NAME STATUS -------------------------------------------------------------------------------------------------------------------------------- -------- MANAGER_100 USABLE MANAGER_1000 USABLE MANAGER_200 USABLE MANAGER_300 USABLE MANAGER_400 USABLE MANAGER_500 USABLE MANAGER_600 USABLE MANAGER_700 USABLE MANAGER_800 USABLE MANAGER_900 USABLE MANAGER_MAX USABLE 11 rows selected.
然后删除分区manager_200
SQL> alter index employees_gi1 drop partition manager_200; Index altered.
当再次查询user_ind_partitions时,你可以看到数据库把上限更高的相邻分区标记为unusable状态。
SQL> select partition_name,status from user_ind_partitions where index_name='EMPLOYEES_GI1'; PARTITION_NAME STATUS -------------------------------------------------------------------------------------------------------------------------------- -------- MANAGER_100 USABLE MANAGER_1000 USABLE MANAGER_300 UNUSABLE MANAGER_400 USABLE MANAGER_500 USABLE MANAGER_600 USABLE MANAGER_700 USABLE MANAGER_800 USABLE MANAGER_900 USABLE MANAGER_MAX USABLE 10 rows selected.
一旦删除了全局索引分区,就必须执行alter index命令重建由于drop partition操作而被标记为unusable的分区。
SQL> alter index employees_gi1 rebuild partition manager_300; Index altered. SQL> select partition_name,status from user_ind_partitions where index_name='EMPLOYEES_GI1'; PARTITION_NAME STATUS -------------------------------------------------------------------------------------------------------------------------------- -------- MANAGER_100 USABLE MANAGER_1000 USABLE MANAGER_300 USABLE MANAGER_400 USABLE MANAGER_500 USABLE MANAGER_600 USABLE MANAGER_700 USABLE MANAGER_800 USABLE MANAGER_900 USABLE MANAGER_MAX USABLE 10 rows selected.
因为必须在全局分区索引上用maxvalue指定的一个分区,所以永远无法删除上限最高的分区。例如:
SQL> alter index employees_gi1 drop partition manager_1000; alter index employees_gi1 drop partition manager_1000 * ERROR at line 1: ORA-14078: you may not drop the highest partition of a GLOBAL index
全局分区索引可以是唯一索引,也可以是非唯一索引。本章到目前为止,只创建了非唯一索引。下面列出的是在表上创建全局唯一索引分区的语句:
SQL> create unique index employees_uk1 on employees_jy(manager_id,employee_id) 2 global 3 partition by range(manager_id) 4 ( 5 partition manager_100 values less than(100), 6 partition manager_200 values less than(200), 7 partition manager_300 values less than(300), 8 partition manager_400 values less than(400), 9 partition manager_500 values less than(500), 10 partition manager_600 values less than(600), 11 partition manager_700 values less than(700), 12 partition manager_800 values less than(800), 13 partition manager_900 values less than(900), 14 partition manager_max values less than(maxvalue) 15 ); Index created.
本地分区索引可以定义为前缀或非前缀索引,而全局分区索引必须创建为前缀索引,也就是说,分区列必须在该索引的前导端。如果你试图创建全局分区的非前缀索引,就会收到以下错误 提示信息:
SQL> create unique index employees_uk1 on employees_jy(employee_id) 2 global 3 partition by range(manager_id) 4 ( 5 partition manager_100 values less than(100), 6 partition manager_200 values less than(200), 7 partition manager_300 values less than(300), 8 partition manager_400 values less than(400), 9 partition manager_500 values less than(500), 10 partition manager_600 values less than(600), 11 partition manager_700 values less than(700), 12 partition manager_800 values less than(800), 13 partition manager_900 values less than(900), 14 partition manager_max values less than(maxvalue) 15 ); partition by range(manager_id) * ERROR at line 3: ORA-14038: GLOBAL partitioned index must be prefixed
另一种全局分区索引类型是哈希分区索引。使用哈希分区索引通常是出于性能原因,因为这使用索引在分区之间更均匀地分布。请看下面创建基于哈希的全局分区索引的例子:
SQL> create index employees_jy_ih1 on employees_jy(department_id) global partition by hash(department_id) partitions 4; Index created.
注意:全局分区索引必须在堆组织表上创建。而且,不能创建全局分区位图索引。