全局索引和本地索引分析
有同行问到了全局索引和本地索引如何选择,全局索引可以分区也可以不分区,而本地索引只包含各自分区的数据。 本地索引是分区的,而且是根据分区表的分区键值来对应,就是分区表的每个分区都有对应的分区索引,而全局索引可以分区也可以不分区,全局索引对应
有同行问到了全局索引和本地索引如何选择,全局索引可以分区也可以不分区,而本地索引只包含各自分区的数据。
本地索引是分区的,而且是根据分区表的分区键值来对应,就是分区表的每个分区都有对应的分区索引,而全局索引可以分区也可以不分区,全局索引对应的表可以是分区表也可以不是分区表。
比如这里t_global01是heap table并不是partition table
SQL> create table t_global01 as select * from dba_objects;
Table created.
SQL> CREATE INDEX index_t_objid
2 ON t_global01 (object_id) global
3 PARTITION BY RANGE(object_id)
4 (PARTITION p1 VALUES LESS THAN(10000),
5 PARTITION p2 VALUES LESS THAN(20000),
6 PARTITION pmax VALUES LESS THAN(MAXVALUE));
Index created.
索引又可以分为前缀索引和非前缀索引,前缀索引是指索引的分区键包含在索引中,并且是索引的前导列,而非前缀索引则是分区键不在索引中或者不是索引的前导列,本地索引可以建立前缀索引和非前缀索引,而全局索引只能建立前缀索引。
SQL> create table t_local01 partition by range(object_id)
2 (partition p1 values less than(10000),
3 partition p2 values less than(20000),
4 partition p3 values less than(30000),
5 partition p4 values less than(40000),
6 partition p5 values less than(maxvalue))
7 as select * from dba_objects;
Table created.
建立本地的前缀索引
SQL> CREATE INDEX index_t_pre01 on t_local01(object_id,object_name) local;
Index created.
建立本地的非前缀索引
SQL> CREATE INDEX index_t_nonpre01 on t_local01(object_name) local;
Index created.
全局索引不允许建立非前缀索引
SQL> create index ind_t_objid_nonpre on t_local01(object_id) global
2 partition by range(data_object_id)
3 (partition p1 values less than(10000),
4 partition pmax values less than(maxvalue));
partition by range(data_object_id)
*
ERROR at line 2:
ORA-14038: GLOBAL partitioned index must be prefixed
分区表的全局索引可能会因为分区表的ddl而导致全局索引失效,这个需要我们特别注意,一般来说oltp建立全局索引,而在olap系统建立本地索引。
Partitioned Indexes: Global, Local, Prefixed and Non-Prefixed (文档 ID 69374.1)
To illustrate the usefulness of global indexes, imagine that we have a large
fact table partitioned on a DATE column. We frequently need to search the table
on a VARCHAR2 column (VCOL) which is not part of the table's partition key.
Assume that there are currently 12 partitions in the table.
We could use 2 possible methods:
A local non-prefixed index on VCOL:
| |
------- -------
| | (10 more | |
Values: A.. Z.. partitions here) A.. Z..
Or a global prefixed index on VCOL:
| |
------- -------
| | (10 more | |
Values: A.. D.. partitions here) T.. Z..
A global prefixed index would usually be the best choice for a unique index on
our example VCOL column. For nonunique indexes, the issue is whether we can use
parallel index searches (local non-prefixed) or whether we need a serial search,
even at the expense of the greater maintenance problems of global indexes.
这里提出了对于唯一列建立全局索引较合适
Common Questions on Indexes on Partitioned Table (文档 ID 1481609.1)记录了local index和global index的适用特点
What are the performance implications of local indexes
Partition elimination/pruning during SQLs against the partitioned table with predicate on the partition key (prefixed more often allows for partition elimination than non prefixed).
这里提出了如果查询条件中有分区键,建立本地索引可以让分区裁剪生效(前缀索引通常比非前缀索引更容易发生分区裁剪)
Non prefixed local index is useful if it is important to have quick access according to a column which is not the partition key (e.g. look up for value account_number column, hence the account_number is placed as a leading column of the index), while it is also important to have the index equipartitioned with the table e.g. to support the time interval for rolling out old data and rolling in new data (e.g. partition key is time_id column, rolling out/in data is done by partition maintenance commands). This scenario often happens in historical databases.
而非前缀索引通常在查询中没有分区键过滤时比较适用。
下面来通过测试来看看上面文章提供的结论:
SQL> create table tab01
2 partition by range(object_id)
3 (partition p1 values less than(10000),
4 partition p2 values less than(20000),
5 partition p3 values less than(30000),
6 partition p4 values less than(40000),
7 partition p5 values less than(maxvalue))
8 as
9 select * from dba_objects;
Table created.
SQL> create index ind_type_local_pre on tab01(object_id,object_type) local;
Index created.
SQL> create index ind_type_local_nonpre on tab01(object_type) local;
Index created.
SQL> analyze table tab01 compute statistics;
Table analyzed.
上面已经建立了前缀和非前缀的本地索引,然后如果我们的查询中没有分区键,那么看看两个索引的实用性
SQL> select /*+index(tab01 ind_type_local_nonpre)*/* from tab01 where object_typ
e='INDEX';
1726 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 4022647995
--------------------------------------------------------------------------------
--------------------------------------------
| Id | Operation | Name | Rows | Byt
es | Cost (%CPU)| Time | Pstart| Pstop |
--------------------------------------------------------------------------------
--------------------------------------------
| 0 | SELECT STATEMENT | | 1481 | 1
27K| 74 (0)| 00:00:01 | | |
| 1 | PARTITION RANGE ALL | | 1481 | 1
27K| 74 (0)| 00:00:01 | 1 | 5 |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| TAB01 | 1481 | 1
27K| 74 (0)| 00:00:01 | 1 | 5 |
|* 3 | INDEX RANGE SCAN | IND_TYPE_LOCAL_NONPRE | 1481 |
| 10 (0)| 00:00:01 | 1 | 5 |
--------------------------------------------------------------------------------
--------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("OBJECT_TYPE"='INDEX')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
353 consistent gets
0 physical reads
0 redo size
87737 bytes sent via SQL*Net to client
1757 bytes received via SQL*Net from client
117 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1726 rows processed
SQL> select /*+index(tab01 ind_type_local_pre)*/* from tab01 where object_type='
INDEX';
1726 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1706313756
--------------------------------------------------------------------------------
-----------------------------------------
| Id | Operation | Name | Rows | Bytes
| Cost (%CPU)| Time | Pstart| Pstop |
--------------------------------------------------------------------------------
-----------------------------------------
| 0 | SELECT STATEMENT | | 1481 | 127K
| 198 (1)| 00:00:03 | | |
| 1 | PARTITION RANGE ALL | | 1481 | 127K
| 198 (1)| 00:00:03 | 1 | 5 |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| TAB01 | 1481 | 127K
| 198 (1)| 00:00:03 | 1 | 5 |
|* 3 | INDEX FULL SCAN | IND_TYPE_LOCAL_PRE | 1481 |
| 176 (1)| 00:00:03 | 1 | 5 |
--------------------------------------------------------------------------------
-----------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("OBJECT_TYPE"='INDEX')
filter("OBJECT_TYPE"='INDEX')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
521 consistent gets
174 physical reads
0 redo size
87699 bytes sent via SQL*Net to client
1757 bytes received via SQL*Net from client
117 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1726 rows processed
这里看来对于本地索引,在查询条件中没有分区键时非前缀索引比较实用。
而如果有分区键的查询,本地索引是可以走分区裁剪的
SQL> select /*+index(tab01 ind_type_local_pre)*/* from tab01 where object_type='
INDEX' and object_id
920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 4238522555
--------------------------------------------------------------------------------
-----------------------------------------
| Id | Operation | Name | Rows | Bytes
| Cost (%CPU)| Time | Pstart| Pstop |
--------------------------------------------------------------------------------
-----------------------------------------
| 0 | SELECT STATEMENT | | 281 | 21075
| 34 (0)| 00:00:01 | | |
| 1 | PARTITION RANGE SINGLE | | 281 | 21075
| 34 (0)| 00:00:01 | 1 | 1 |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| TAB01 | 281 | 21075
| 34 (0)| 00:00:01 | 1 | 1 |
|* 3 | INDEX RANGE SCAN | IND_TYPE_LOCAL_PRE | 281 |
| 30 (0)| 00:00:01 | 1 | 1 |
--------------------------------------------------------------------------------
-----------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("OBJECT_TYPE"='INDEX' AND "OBJECT_ID" filter("OBJECT_TYPE"='INDEX')
Statistics
----------------------------------------------------------
244 recursive calls
0 db block gets
244 consistent gets
0 physical reads
0 redo size
45241 bytes sent via SQL*Net to client
1163 bytes received via SQL*Net from client
63 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
920 rows processed
SQL> select /*+index(tab01 ind_type_local_nonpre)*/* from tab01 where object_typ
e='INDEX' and object_id
920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1322437935
--------------------------------------------------------------------------------
--------------------------------------------
| Id | Operation | Name | Rows | Byt
es | Cost (%CPU)| Time | Pstart| Pstop |
--------------------------------------------------------------------------------
--------------------------------------------
| 0 | SELECT STATEMENT | | 281 | 210
75 | 21 (0)| 00:00:01 | | |
| 1 | PARTITION RANGE SINGLE | | 281 | 210
75 | 21 (0)| 00:00:01 | 1 | 1 |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| TAB01 | 281 | 210
75 | 21 (0)| 00:00:01 | 1 | 1 |
|* 3 | INDEX RANGE SCAN | IND_TYPE_LOCAL_NONPRE | 281 |
| 1 (0)| 00:00:01 | 1 | 1 |
--------------------------------------------------------------------------------
--------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("OBJECT_TYPE"='INDEX')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
182 consistent gets
0 physical reads
0 redo size
45241 bytes sent via SQL*Net to client
1163 bytes received via SQL*Net from client
63 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
920 rows processed
这里我们看出执行计划中都出现了PARTITION RANGE SINGLE单个分区扫描(pstart和pstop都是1),这个是因为执行计划的INDEX RANGE SCAN索引范围扫描时pstart 1和pstop 1,此时索引扫描就只会扫描指定的索引分区,这个也就是索引的分区裁剪,当然还有表的分区裁剪,关于分区裁剪的内容小鱼后面有时间会列出来单独讨论。
而如果是全局索引,索引默认不分区,所以也就无法发生索引的分区裁剪:
SQL> drop index ind_type_local_nonpre;
Index dropped.
SQL> create index ind_type_global on tab01(object_type) global;
Index created.
SQL> select /*+index(tab01 ind_type_global)*/* from tab01 where object_type='IND
EX' and object_id
920 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3954671853
--------------------------------------------------------------------------------
--------------------------------------
| Id | Operation | Name | Rows | Bytes | C
ost (%CPU)| Time | Pstart| Pstop |
--------------------------------------------------------------------------------
--------------------------------------
| 0 | SELECT STATEMENT | | 281 | 21075 |
69 (0)| 00:00:01 | | |
|* 1 | TABLE ACCESS BY GLOBAL INDEX ROWID| TAB01 | 281 | 21075 |
69 (0)| 00:00:01 | 1 | 1 |
|* 2 | INDEX RANGE SCAN | IND_TYPE_GLOBAL | 1481 | |
5 (0)| 00:00:01 | | |
--------------------------------------------------------------------------------
--------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID" 2 - access("OBJECT_TYPE"='INDEX')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
185 consistent gets
6 physical reads
0 redo size
45241 bytes sent via SQL*Net to client
1163 bytes received via SQL*Net from client
63 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
920 rows processed
What are the performance implications of global indexes?
Global index can be useful where rapid access, data integrity, and availability are important. In an OLTP system, a table may be partitioned by one key, for example, the employees.department_id column, but an application may need to access the data with many different keys, for example, by employee_id or job_id. Global indexes can be useful in this scenario as global indexes are prefixed and can provide better performance than local nonprefixed indexes because they minimize the number of index partition probes (cf. local prefixed more often allows for partition elimination than non prefixed mentioned in the previous section).
全局索引多用于OLTP系统,可以快速的返回查询的数据,特别适用于查询条件中不包含分区键的查询,这种情况全局索引相比本地索引更加高效。
Global indexes are harder to manage than local indexes. At partition maintenance of the table, all partitions of a global index are affected.
这里提出全局索引难以维护,如果分区修改了,所有分区的索引都会影响
Partition elimination/pruning during SQLs against the partitioned table: prefixed - always allows for partition elimination.
同样全局索引也是可以发生分区裁剪的
SQL> create table t_global01 as select * from dba_objects;
Table created.
SQL> CREATE INDEX index_t_objid
2 ON t_global01 (object_id) global
3 PARTITION BY RANGE(object_id)
4 (PARTITION p1 VALUES LESS THAN(10000),
5 PARTITION p2 VALUES LESS THAN(20000),
6 PARTITION pmax VALUES LESS THAN(MAXVALUE));
Index created.
SQL> select * from t_global01 where object_id
9568 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1223163610
--------------------------------------------------------------------------------
------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CP
U)| Time | Pstart| Pstop |
--------------------------------------------------------------------------------
------------------------------
| 0 | SELECT STATEMENT | | 9194 | 897K| 177 (
0)| 00:00:03 | | |
| 1 | PARTITION RANGE SINGLE | | 9194 | 897K| 177 (
0)| 00:00:03 | 1 | 1 |
| 2 | TABLE ACCESS BY INDEX ROWID| T_GLOBAL01 | 9194 | 897K| 177 (
0)| 00:00:03 | | |
|* 3 | INDEX RANGE SCAN | INDEX_T_OBJID | 9194 | | 43 (
0)| 00:00:01 | 1 | 1 |
--------------------------------------------------------------------------------
------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("OBJECT_ID"
这里看出也发生了所谓的分区裁剪,index range scan的pstart和pstop都是1,说明是扫描了索引的一个分区,这也和上面partition range single相对应(出现partition range single并不一定表示该表是分区表,有可能有分区的索引)
The hash index partitioning can improve performance of indexes where a small number leaf blocks in the index have high contention in multiuser OLTP environment. In some OLTP applications, index insertions happen only at the right edge of the index. This situation could occur when the index is defined on monotonically increasing columns (e.g. column value is populated by a sequence). In such situations, the right edge of the index becomes a hotspot because of contention for index pages, buffers, latches for update, and additional index maintenance activity, which results in performance degradation.
这里提出了一个hash index partition,在高并发情况下,索引的数据会不停往右边倾斜(比如列是序列填充时),这种情况下索引右边叶块会成为热点块,造成大量的buffer latches竞争和额外的维护(比如索引分裂)而导致性能下降。
关于本地索引和全局索引小鱼也没有较多的实战案例,个人而言小鱼维护的大多是OLTP系统,所以一般都是建立的全局索引,可以参考以下建立:
Global index和local index适用范围
non-prefixed Local indexes特别适用于基于历史数据查询分析的数据库,在这样的数据库中,历史数据一般都是根据时间来分区的。
prefixed Local index适用于对分区主键进行索引,可以明显减少查询所搜索到的分区数目,极大的加快查询速度。
Global prefixed index适用于对非分区主键进行索引,特别对于唯一列的查询是比较适合建立全局索引的,但是Global pre- fixed index难以维护,任何对基表的分区信息的修改都会不可避免的导致索引的失效。
原文地址:全局索引和本地索引分析, 感谢原作者分享。

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











uniapp에서 데이터 통계 및 분석을 구현하는 방법 1. 배경 소개 데이터 통계 및 분석은 사용자 행동에 대한 통계 및 분석을 통해 모바일 애플리케이션 개발 과정에서 매우 중요한 부분입니다. 이를 통해 제품 디자인과 사용자 경험을 최적화합니다. 이 글에서는 uniapp에서 데이터 통계 및 분석 기능을 구현하는 방법을 소개하고 몇 가지 구체적인 코드 예제를 제공합니다. 2. 적절한 데이터 통계 및 분석 도구 선택 uniapp에서 데이터 통계 및 분석을 구현하는 첫 번째 단계는 적절한 데이터 통계 및 분석 도구를 선택하는 것입니다.

Soda Music에 지역 음악을 추가하는 방법 Soda Music APP에 좋아하는 지역 음악을 추가할 수 있지만 대부분의 친구들은 지역 음악을 추가하는 방법을 모릅니다. 다음은 Soda Music에 지역 음악을 추가하는 방법에 대한 그래픽 튜토리얼입니다. 편집자 여러분, 관심 있는 사용자가 와서 살펴보세요! 소다 음악 사용에 대한 튜토리얼 소다 음악에 로컬 음악을 추가하는 방법 1. 먼저 소다 음악 앱을 열고 메인 페이지 하단의 [음악] 기능 영역을 클릭합니다. 오른쪽 하단에 있는 [점 3개] 아이콘 3. 마지막으로 아래 기능 표시줄을 확장하고 [다운로드] 버튼을 선택하여 로컬 음악에 추가합니다.

Oracle 인덱스 유형은 다음과 같습니다. 1. B-트리 인덱스, 3. 함수 인덱스, 5. 역방향 키 인덱스, 7. 도메인 인덱스, 비트맵 연결 인덱스 10. 복합 인덱스. 세부 소개: 1. B-트리 인덱스는 동시 작업을 효율적으로 지원할 수 있는 자체 균형 트리 데이터 구조입니다. Oracle 데이터베이스에서 B-트리 인덱스는 가장 일반적으로 사용되는 인덱스 유형입니다. 2. 비트 그래프 인덱스는 인덱스 유형 기반입니다. 비트맵 알고리즘 등에 관한 것입니다.

제목: DreamWeaver CMS의 보조 디렉터리를 열 수 없는 이유와 해결 방법 분석 Dreamweaver CMS(DedeCMS)는 다양한 웹 사이트 구축에 널리 사용되는 강력한 오픈 소스 콘텐츠 관리 시스템입니다. 그러나 때로는 웹사이트를 구축하는 과정에서 보조 디렉토리를 열 수 없는 상황이 발생할 수 있으며, 이로 인해 웹사이트의 정상적인 작동에 문제가 발생할 수 있습니다. 이 기사에서는 보조 디렉터리를 열 수 없는 가능한 이유를 분석하고 이 문제를 해결하기 위한 구체적인 코드 예제를 제공합니다. 1. 예상 원인 분석: 의사 정적 규칙 구성 문제: 사용 중

제목: Tencent의 주요 프로그래밍 언어는 Go: 심층 분석 중국 최고의 기술 회사로서 Tencent는 프로그래밍 언어 선택에 있어 항상 많은 관심을 받아 왔습니다. 최근 몇 년 동안 일부 사람들은 Tencent가 주로 Go를 주요 프로그래밍 언어로 채택했다고 믿고 있습니다. 이 기사에서는 Tencent의 주요 프로그래밍 언어가 Go인지에 대한 심층 분석을 수행하고 이러한 관점을 뒷받침하는 구체적인 코드 예제를 제공합니다. 1. Tencent에 Go 언어 적용 Go는 Google에서 개발한 오픈 소스 프로그래밍 언어로 효율성, 동시성 및 단순성으로 인해 많은 개발자에게 사랑을 받고 있습니다.

해결 방법은 다음과 같습니다. 1. 인덱스 값이 올바른지 확인합니다. 먼저 인덱스 값이 배열의 길이 범위를 초과하는지 확인합니다. 배열의 인덱스는 0부터 시작하므로 최대 인덱스 값은 배열 길이에서 1을 뺀 값이어야 합니다. 2. 루프 경계 조건을 확인하세요. 루프에서 배열 액세스에 인덱스를 사용하는 경우 루프 경계 조건이 올바른지 확인하세요. 3. 배열 초기화: 배열을 사용하기 전에 배열이 올바르게 초기화되었는지 확인하십시오. 4. 예외 처리 사용: 프로그램의 예외 처리 메커니즘을 사용하여 인덱스가 배열 범위를 초과하는 오류를 잡을 수 있습니다. 그에 따라 처리하십시오.

정적 측위 기술의 장점과 한계 분석 현대 과학 기술의 발달로 측위 기술은 우리 삶에 없어서는 안 될 부분이 되었습니다. 그 중 하나로서 정적 측위 기술에는 고유한 장점과 한계가 있습니다. 이 기사에서는 현재 적용 상태와 향후 개발 동향을 더 잘 이해하기 위해 정적 측위 기술에 대한 심층 분석을 수행합니다. 먼저, 정적 측위 기술의 장점을 살펴보겠습니다. 정적 측위 기술은 위치 지정하려는 물체를 관찰, 측정 및 계산하여 위치 정보를 결정합니다. 다른 측위 기술과 비교하여,

이 글은 PHP가 다른 문자열에서 문자열의 시작 위치부터 끝 위치까지 문자열을 반환하는 방법을 자세히 설명합니다. 편집자는 이것이 꽤 실용적이라고 생각하므로 참고용으로 공유하겠습니다. 이 기사에서 뭔가를 얻을 수 있습니다. PHP에서 substr() 함수를 사용하여 문자열에서 부분 문자열을 추출합니다. substr() 함수는 문자열에서 지정된 범위 내의 문자를 추출할 수 있습니다. 구문은 다음과 같습니다. substr(string,start,length) 여기서: string: 하위 문자열을 추출할 원래 문자열입니다. start: 하위 문자열의 시작 위치에 대한 인덱스입니다(0부터 시작). 길이(선택 사항): 하위 문자열의 길이입니다. 지정하지 않은 경우
