1. 程式人生 > >全局(Global) 與本地(Local)索引的區別

全局(Global) 與本地(Local)索引的區別

ceshi name altered 失效 from eat create 5-0 partition

一、定義說明

oracle數據庫中,存在多種對象,最常見的表和索引,索引的目的是為了加快查詢;oracle建議一個表>2g時,就建議進行分區,分區表的好處此處省略,分區表有維護操作,但是某些維護操作對整個表的索引維護造成巨大開銷,因此本地索引就是解決分區表管理的問題。

非分區索引創建方式,create index index_name on table_name(column_name);

查詢索引狀態從dba_indexes->status列查詢

全局分區索引創建,create index index_name on table_name(column_name) global partition by range(column_name) (partition p1 values less than (11104) tablespace system,partition p2 values less than (11100) tablespace users,partition p3 values less than (maxvalue) tablespace sysaux.) or partition by hash (id) partitions 8 online等;

查詢索引狀態dba_ind_partitions->STATUS列查詢

查詢分區索引類型,是全局or本地 DBA_PART_INDEXES ->LOCALITY

本地分區索引創建,create index index_name on table_name(column_name) local;

查詢索引狀態dba_ind_partitions->STATUS列查詢

二、create/selet 說明

1) 非分區索引
SQL> create index ind_1 on range_part_tab(deal_date);
Index created.
SQL
> select index_name,PARTITION_NAME,HIGH_VALUE,STATUS,TABLESPACE_NAME from dba_ind_partitions where index_owner=SYS and index_name=IND_1; no rows selected SQL> select index_name,STATUS,GLOBAL_STATS,TABLESPACE_NAME from dba_indexes where owner=SYS and index_name=IND_1; INDEX_NAME STATUS GLO TABLESPACE_NAME
------------------------------ -------- --- ------------------------------ IND_1 VALID YES SYSTEM STATUS 表示一個非分區索引是否VALID或UNUSABLE GLOBAL_STATUS 對於分區索引,指示是通過分析整個索引(YES)來收集統計信息,還是根據基礎索引分區和子分區(NO)的統計信息進行估計


2)全局分區索引

create index ind_3 on range_part_tab(area_code) global partition by range(area_code) (partition p1 values less than (11104) tablespace system,partition p2 values less than (11100) tablespace users,partition p3 values less than (maxvalue) tablespace sysaux)
*
ERROR at line 1:
ORA-14037: partition bound of partition "P1" is too high

12:39:59 SYS@ceshi>select area_code,count(*) from range_part_tab group by area_code;

AREA_CODE COUNT(*)
---------- ----------
599 11104
594 11074
596 11021
593 11113
595 10927
592 11214
598 11235
591 11169
597 11143

9 rows selected. 全局範圍分區索引,第一個分區不能比所有的值都大

SYS@ceshi>create index ind_3 on range_part_tab(area_code) global partition by range(area_code) (partition p1 values less than (592) tablespace system,partition p2 values less than (11100) tablespace users,partition p3 values less than (maxvalue) tablespace sysaux);

Index created.

select index_name,PARTITION_NAME,HIGH_VALUE,STATUS,TABLESPACE_NAME from dba_ind_partitions where index_owner=‘SYS‘ and index_name=‘IND_3‘

INDEX_NAME PARTITION_ HIGH_VALUE STATUS TABLESPACE_NAME
---------- ---------- ------------------------------ -------- ------------------------------
IND_3 P1 592 USABLE SYSTEM
IND_3 P2 11100 USABLE USERS
IND_3 P3 MAXVALUE USABLE SYSAUX

PARTITION_NAME 索引的分區名稱

HIGH_VALUE 索引分區的範圍條件

STATUS 分區索引的狀態

SYS@ceshi>select index_name,table_name,PARTITIONING_TYPE,SUBPARTITIONING_TYPE,PARTITION_COUNT,LOCALITY,DEF_TABLESPACE_NAME from DBA_PART_INDEXES where index_name=‘IND_3‘;

INDEX_NAME TABLE_NAME PARTITION SUBPARTIT PARTITION_COUNT LOCALI DEF_TABLESPACE_NAME
---------- ------------------------------ --------- --------- --------------- ------ ------------------------------
IND_3 RANGE_PART_TAB RANGE NONE 3 GLOBAL SYSTEM

hash全局分區索引,使用場景較多,例如索引塊爭用,且表為非分區表時

SYS@ceshi>create table a1 as select * from range_part_tab;

SYS@ceshi>create index ind_4 on a1(id) global partition by hash (id) partitions 8 online;

Index created.

SYS@ceshi>alter index ind_4 parallel 1;

Index altered.

select index_name,PARTITION_NAME,HIGH_VALUE,STATUS,TABLESPACE_NAME from dba_ind_partitions where index_owner=‘SYS‘ and index_name=‘IND_4‘

INDEX_NAME PARTITION_NAME HIGH_VALUE STATUS TABLESPACE_NAME
------------------------------ ------------------------------ ---------- -------- ------------------------------
IND_4 SYS_P61 USABLE SYSTEM
IND_4 SYS_P62 USABLE SYSTEM
IND_4 SYS_P63 USABLE SYSTEM
IND_4 SYS_P64 USABLE SYSTEM
IND_4 SYS_P65 USABLE SYSTEM
IND_4 SYS_P66 USABLE SYSTEM
IND_4 SYS_P67 USABLE SYSTEM
IND_4 SYS_P68 USABLE SYSTEM

8 rows selected.

SYS@ceshi>select index_name,table_name,PARTITIONING_TYPE,SUBPARTITIONING_TYPE,PARTITION_COUNT,LOCALITY,DEF_TABLESPACE_NAME from DBA_PART_INDEXES where index_name=‘IND_4‘;

INDEX_NAME TABLE_NAME PARTITION SUBPARTIT PARTITION_COUNT LOCALI DEF_TABLESPACE_NAME
------------------------------ ------------------------------ --------- --------- --------------- ------ ------------------------------
IND_4 A1 HASH NONE 8 GLOBAL SYSTEM

 3)  本地分區索引  

SQL> create index ind_2 on range_part_tab(id) local;

Index created.

select index_name,PARTITION_NAME,HIGH_VALUE,STATUS,TABLESPACE_NAME from dba_ind_partitions where index_owner=‘SYS‘ and index_name=‘IND_2‘

INDEX_NAME PARTITION_ HIGH_VALUE STATUS TABLESPACE_NAME
------------------ -----------------------------------------------------------------------------------------------------------------------
IND_2 P1 TO_DATE(‘ 2017-02-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P10 TO_DATE(‘ 2017-11-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P11 TO_DATE(‘ 2017-12-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P12 TO_DATE(‘ 2018-01-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P2 TO_DATE(‘ 2017-03-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P3 TO_DATE(‘ 2017-04-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P4 TO_DATE(‘ 2017-05-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P5 TO_DATE(‘ 2017-06-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P6 TO_DATE(‘ 2017-07-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P7 TO_DATE(‘ 2017-08-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P8 TO_DATE(‘ 2017-09-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P9 TO_DATE(‘ 2017-10-01 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_2 P_MAX MAXVALUE USABLE SYSTEM

13 rows selected.

SYS@ceshi>select index_name,table_name,PARTITIONING_TYPE,SUBPARTITIONING_TYPE,PARTITION_COUNT,LOCALITY,DEF_TABLESPACE_NAME from DBA_PART_INDEXES where index_name=‘IND_2‘;

INDEX_NAME TABLE_NAME PARTITION SUBPARTIT PARTITION_COUNT LOCALI DEF_TABLESPACE_NAME
---------- ------------------------------ --------- --------- --------------- ------ ------------------------------
IND_2 RANGE_PART_TAB RANGE NONE 13 LOCAL

三、索引的重建

非分區索引:

SQL> alter index ind_1 rebuild;

全局分區索引:

SQL> alter table range_part_tab move partition p1;

Table altered.

SQL> select TABLE_NAME,PARTITION_NAME,TABLESPACE_NAME from dba_tab_partitions
where TABLE_OWNER=SYS and TABLE_NAME=RANGE_PART_TAB;

TABLE_NAME PARTITION_ TABLESPACE_NAME

------------------------------ ---------- ------------------------------

RANGE_PART_TAB P1 SYSTEM

RANGE_PART_TAB P10 SYSTEM

RANGE_PART_TAB P11 SYSTEM

SQL> select index_name,PARTITION_NAME,HIGH_VALUE,STATUS,TABLESPACE_NAME

from dba_ind_partitions where index_owner=‘SYS‘ and index_name=‘IND_3‘;

INDEX_NAME PARTITION_ HIGH_VALUE STATUS TABLESPACE_NAME

---------- ---------- -------------------- -------- ------------------------------

IND_3 P1 592 USABLE SYSTEM

IND_3 P2 11100 USABLE USERS

IND_3 P3 MAXVALUE USABLE SYSAUX

SQL> alter index ind_3 rebuild partition p1;

Index altered.

全局分區索引,單個分區的move並不會導致全局分區索引的失效


本地分區索引

select index_name,PARTITION_NAME,STATUS,TABLESPACE_NAME
from dba_ind_partitions where index_owner=‘SYS‘ and index_name=‘IND_2‘;
INDEX_NAME PARTITION_ STATUS TABLESPACE_NAME
---------- ---------- -------- ------------------------------
IND_2 P1 USABLE SYSTEM
IND_2 P10 USABLE SYSTEM
IND_2 P11 USABLE SYSTEM
IND_2 P12 USABLE SYSTEM

SQL> alter table range_part_tab move partition p1;

Table altered

SQL> alter index ind_2 rebuild partition p1;

Index altered.

全局(Global) 與本地(Local)索引的區別