B树索引和位图索引

前言

众所周知建立索引是为了提高数据库查询效率。正解的索引确实能够数倍的提高数据库查询效率,但一个错误的索引将会把数据库拖慢,甚至拖死。

本文意在探讨如何选择索引类型。

正文

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=的查询条件,位图索引查询效率很高,计算机善于处理01数据。

什么时候适合使用位图索引:引用一下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)        列值很多,可选范围很大的表;

猜你喜欢

转载自wengn.iteye.com/blog/1840377