反向索引

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.小結
可以充分發揮反向索引減少索引熱點塊的優勢對系統進行調優,不過反向索引本身的結構特點也限定了它的應用範圍。因此需要具體問題具體分析,實施前做好充分的
測試

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章