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)        列值很多,可选范围很大的表;

### Bitmap 数据结构的学术论文推荐 Bitmap 是一种高效的数据存储查询技术,在许多领域得到了广泛应用,尤其是在大规模数据集中的快速查找、集合操作等方面表现出色。以下是针对 bitmap 数据结构的一些经典学术论文推荐: #### 1. **BitMap 的基础理论** - 文章《Representation of Sets by Bit Strings》提供了 bit string 表示法的基础理论[^5]。该文章详细描述了如何使用位图来表示集合,并分析了其空间效率时间复杂度。 #### 2. **压缩 Bitmap 技术** - 论文《Compressed Bitmap Indexes: Beyond Inverted Files and Column Stores》探讨了压缩 bitmap 索引的技术细节[^6]。文中介绍了多种压缩算法(如 Roaring Bitmap),并对比了它们在性能上的差异。 - 另一篇重要文献《Roaring Bitmaps: Implementation of an Optimized Data Structure for Fast Set Operations》专注于 roaring bitmap 的实现与优化[^7]。此研究展示了如何通过分层结构显著提升 bitmap 的计算速度。 #### 3. **实际应用案例** - 在数据库领域,《Bitmap Index Design Choices and Their Impact on Query Performance》深入分析了 bitmap 索引的设计选择及其对查询性能的影响[^8]。作者指出,合理设计 bitmap 结构能够极大提高多维数据分析的速度。 - 对于分布式系统,《Scalable Bloom Filters with Controlled Error Rates Using Compressed Bitmaps》提出了基于压缩 bitmap 的可扩展布隆过滤器方案[^9]。这种方法不仅减少了内存占用,还保持了较低的误报率。 #### 示例代码:简单的 Bitmap 实现 以下是一个 Python 中的简单 bitmap 实现示例: ```python class SimpleBitmap: def __init__(self, size): self.size = size self.bitmap = [False] * size def set_bit(self, index): if 0 <= index < self.size: self.bitmap[index] = True def clear_bit(self, index): if 0 <= index < self.size: self.bitmap[index] = False def get_bit(self, index): if 0 <= index < self.size: return self.bitmap[index] return None # 测试 bm = SimpleBitmap(10) bm.set_bit(3) print(bm.get_bit(3)) # 输出 True ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值