B樹索引和位圖索引

轉自:http://wengn.iteye.com/blog/1840377


前言

衆所周知建立索引是爲了提高數據庫查詢效率。正解的索引確實能夠數倍的提高數據庫查詢效率,但一個錯誤的索引將會把數據庫拖慢,甚至拖死。

本文意在探討如何選擇索引類型。

正文

Oracle常用的有兩種索引類型:B樹索引和位圖索引。

一、      B樹索引

B樹索引:B樹索引是最常用的索引,它的存儲結構類似於書的目錄索引結構,有分支節點和葉子節點,分支節點相當於書的大目錄,葉子節點相當於具體到頁的索引。B樹索引是oracle數據庫的默認索引類型。

 

(B樹索引結構圖)

B樹索引適用對象:

(1)        適合高基數的列(唯一值多);

(2)        適合與大量的增、刪、改(OLTP);

(3)        不能用包含OR操作符的查詢;

什麼時候不適合創建B樹索引:引用一下oracle官方文檔

Where B-Trees Should Not Be Created

Several situations are worth noting where you should not create B-Tree indexes on columns. These cases include columns which:

§ Have only a few distinct values in their domains. For example, a Type column that has only four distinct values (A, B, C, and D). The index would be said to have "low selectivity." If you have an Oracle database, then these columns of low selectivity are ideal candidates for Bitmap indexes.// 只有幾個不同的值供選擇。例如,一個“類型”列中,只有四個不同的值(A,B,C,和D)。該索引是一個低效的選擇。如果你有一個Oracle數據庫,那麼爲這些選擇範圍小的的列建立位圖索引是更好的選擇。

§ Occur in WHERE clauses but within functions other than MIN or MAX.//當在where 條件中使用了除了MIN和MAX以外的函數。

Indexes in these cases waste space and slow down the load process.

小結:

B樹索引經過大量的插入刪除操作以後一個是容易使樹不平衡,再一個是刪除後空間不回收。所以定期重建索引非常有必要。

二、      位圖索引

位圖索引:

(位圖索引結構圖)

 

位圖索引優點:

(1)        用一個位來表示一個索引的鍵值,節省了存儲空間;

(2)        對and,or或=的查詢條件,位圖索引查詢效率很高,計算機善於處理0,1數據。

什麼時候適合使用位圖索引:引用一下oracle官方文檔

Candidates for Bitmap Indexes

Bitmap indexes are most advantageous whenever the cardinality of the index is less than one percent, or lowly-selective. This criterion is nearly the opposite of the guideline for B-Tree indexes.

Look for cases where:

§ A query constrains multiple columns which have few distinct values in their domains (large number of duplicate values).// 一個查詢條件包含多個列,並且要創建索引的列只有幾個不同的值(擁有大量重複值)。

§ A large number of rows satisfy the constraints on these columns.//大量的數據符合這些列上的約束條件。

§ Bitmap indexes have been created on some or all of these columns. //位圖索引可以創建在一個、多個或全部列上。

§ The referenced table contains a large number of rows. //被引用的表包含了非常多的行。

注意:

CAUTION: Bitmap indexes should be used only for static tables and are not suited for highly volatile tables in online transaction processing systems.//位圖索引只能用在相對穩定的表,不適合用在表數據頻繁變化的聯機系統中。

什麼時候不適合創建位圖樹索引:

(1)        頻繁進行插入或更新的表;

(2)        列值很多,可選範圍很大的表;


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