反向索引

来源:互联网 发布:网络公司简介模板图片 编辑:程序博客网 时间:2024/05/17 07:28
反向索引就是将正常的键值头尾调换后再进行存储,比如原值是“1234”,将会以“4321”形式进行存储,这样做可以高效地打散正常的索引键值在索引叶块中的分布 位置。

1. 反向索引应用场合
1)发现索引叶块成为热点块时使用
通常,使用数据时(常见于批量插入操作)都比较集中在一个连续的数据范围内,那么在使用正常的索引时就很容易发生索引叶子块过热的现象,严重 时将会导致系统性能下降。
2)在
RAC环境中使用
当RAC环境中几个节点访问数据的特点是集中和密集,索引热点块发生的几率就会很 高。如果系统对范围检索要求不是很高的情况下可以考虑使用反向索引技术来提高系统的性能。因此该技术多见于RAC环境,它可以显著的降低索引块的争用。

2.使用反向索引的优点
最大的优点莫过于降低索引叶子块的争用,减少热点块,提高系统性能。

3.使用反向索引的缺点
由于反向索引结构自身的特点,如果系统中经常使用范围扫描进行读取数据的话(例如在where子句中使用“between and”语句或比较运算符“>”“<”等),那么反向索引将不适用,因为此时会出现大量的全表扫描的现象,反而会降低系统的性能。

4.通过一个小
实验简单演示一下反向索引的创建及修改
1)创建一个实验表T
sec@ora10g> create table t (x int);

Table created.

2)创建反向索引,在正常创建索引的语句后面简单的加上“reverse”关键 字即可。
sec@ora10g> create index idx_t on t(x) reverse;

Index created.

3)使用user_indexes视图查看一下索引的类型,“NORMAL/REV”表示该索引类型为反向索引。
sec@ora10g> col TABLE_NAME for a10
sec@ora10g> col INDEX_NAME for a10
sec@ora10g> select table_name, index_name, index_type from user_indexes where table_name = 'T';

TABLE_NAME INDEX_NAME INDEX_TYPE
---------- ---------- ---------------
T          IDX_T      NORMAL/REV

4)修改反向索引为正常索引
当反向索引无法满足我们的需求的时候,我们可能会考虑修改反向索引为正常的索引结构。
除了删除索引重新创建这种方法外,可以直接使用noreverse选项重新rebuild索引。
sec@ora10g> alter index idx_t rebuild noreverse;

Index altered.

5)最后确认一下,该索引已经修改为正常的索引。
sec@ora10g> select table_name, index_name, index_type from user_indexes where table_name = 'T';

TABLE_NAME INDEX_NAME INDEX_TYPE
---------- ---------- ---------------
T          IDX_T      NORMAL

5.Oracle 10gR2官方文档有关反向索引的描述。可谓“清澈见底”。
http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/schema.htm#sthref998

Reverse Key Indexes

Creating a reverse key index, compared to a standard index, reverses the bytes of each column indexed (except the rowid) while keeping the column order. Such an arrangement can help avoid performance degradation with Real Application Clusters where modifications to the index are concentrated on a small set of leaf blocks. By reversing the keys of the index, the insertions become distributed across all leaf keys in the index.

Using the reverse key arrangement eliminates the ability to run an index range scanning query on the index. Because lexically adjacent keys are not stored next to each other in a reverse-key index, only fetch-by-key or full-index (table) scans can be performed.

Sometimes, using a reverse-key index can make an OLTP Real Application Clusters application faster. For example, keeping the index of mail messages in an e-mail application: some users keep old messages, and the index must maintain pointers to these as well as to the most recent.

The REVERSE keyword provides a simple mechanism for creating a reverse key index. You can specify the keyword REVERSE along with the optional index specifications in a CREATE INDEX statement:

CREATE INDEX i ON t (a,b,c) REVERSE;


You can specify the keyword NOREVERSE to REBUILD a reverse-key index into one that is not reverse keyed:

ALTER INDEX i REBUILD NOREVERSE;


Rebuilding a reverse-key index without the NOREVERSE keyword produces a rebuilt, reverse-key index.

6.小结
可以充分发挥反向索引减少索引热点块的优势对系统进行调优,不过反向索引本身的结构特点也限定了它的应用范围。因此需要具体问题具体分析,实施前做好充分的
测试
0 0
原创粉丝点击