# 位图索引

位图索引是一种[数据库索引](https://www.edgepedia.org/zh/database-index)，它为列的每一个不同值存储一个位图（一串二进制位），每个表行对应一位，当该行持有该值时该位被置为1。位图使查询引擎能够在接触表本身之前，直接对位序列施加布尔 AND、OR 和 NOT 运算，从而回答涉及多个列的条件。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 结构 | 每个不同的列值对应每行一位；在 Oracle 中位图以压缩形式存储，无需用户干预<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup> |
| 最佳适用场景 | 具有大量 SELECT 活动、INSERT、UPDATE 和 DELETE 操作不频繁的数据仓库和报表工作负载<sup>[2](https://github.com/oracle/skills/blob/main/db/performance/index-strategy.md)</sup> |
| 基数经验法则 | 事实表列在满足以下条件时是候选：每个不同值对应100行或以上，且该列在查询中受到限制或是维度表外键<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup> |
| 查询机制 | AND 和 OR 谓词通过对位图进行按位运算来解析，然后转换为行标识；COUNT 查询通过计数1来回答，无需访问表<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup><sup> • </sup><sup>[3](https://oracle.readthedocs.io/en/latest/sql/indexes/bitmap-indexes.html)</sup> |
| 主要弱点 | 并发 DML 会锁定整个位图，导致死锁和严重争用，这使得位图索引不适合 OLTP<sup>[3](https://oracle.readthedocs.io/en/latest/sql/indexes/bitmap-indexes.html)</sup><sup> • </sup><sup>[2](https://github.com/oracle/skills/blob/main/db/performance/index-strategy.md)</sup> |
| 星型转换 | 在 Oracle 中，事实表外键上的位图索引是使用星型转换的必要条件<sup>[4](https://docs.oracle.com/cd/B12037_01/server.101/b10736.pdf)</sup> |
| 现代压缩 | Roaring 位图比基于 RLE 的 WAH 和 Concise 方案压缩效果好约2倍，交集运算速度快达900倍<sup>[5](https://arxiv.org/abs/1402.6407)</sup> |

## 位图索引的工作原理

对于一个有 k 个不同值的列，索引保留 k 个位图，每个位图与表等长。第 i 行的位在该行所含值对应的位图中被置位。由于位图按行位置对齐，诸如 `region = 'WEST'` 这样的谓词通过读取一个位图来选择行，而诸如 `region = 'WEST' AND status = 'SHIPPED'` 这样的复合谓词则通过对两个位图进行 AND 运算来回答。Oracle 直接描述了这一点：WHERE 子句中的 AND 和 OR 条件通过对位图执行相应的布尔运算来解析，然后才将结果位图转换为行标识以访问表。如果结果集很小，查询就避免了全表扫描；COUNT 可以仅通过计数位图中的1来回答，完全不需要行标识转换。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup>

压缩很重要，因为稀有值的位图大部分是零。Oracle 自动以压缩形式存储位图索引，压缩意味着可以用极少的 I/O 生成许多行标识。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup><sup> • </sup><sup>[3](https://oracle.readthedocs.io/en/latest/sql/indexes/bitmap-indexes.html)</sup> 研究中的压缩方案旨在保持布尔运算快速的同时缩小存储空间：Wu、Otoo 和 Shoshani 的方案比字节对齐位图编码快12倍，而仅多用50%的空间，且使用的空间不到未压缩方案的30%。<sup>[6](https://escholarship.org/uc/item/8k22w7q2)</sup> 一项 VLDB 1999 年的研究发现，最佳压缩技术和布尔运算算法在很大程度上取决于所操作的位图，这推动了自适应设计。<sup>[7](https://www.sigmod.org/publications/dblp/db/conf/vldb/Johnson99.html)</sup>

## 基数与何时选择位图

经典指导将位图索引与低基数列联系起来，即不同值与行之比很小的列。一个只有两个不同值的性别列是教科书式的案例。但这一指导有局限。Oracle 指出，在一张有一百万行的表上，一个有10,000个不同值的列仍然是候选，并且当该列经常与其他已索引列一起查询时，这种列上的位图索引可以胜过 B 树索引。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup>

**决定因素在应用，而非基数。** Oracle 性能专家 Vivek Sharma 在2005年的一篇技术文章中论证，位图索引的适用性取决于应用而非基数：对于数据不经常被许多并发进程更新的系统，建议使用位图索引，他的实验表明，在这样一个以读为主的系统中，一个值完全不重复的列上的位图索引可以像 B 树一样高效。代价是大小，因为近乎唯一的列上的位图索引比同一列上的 B 树大得多，而低基数列（如 GENDER）上的位图则比相应的 B 树小。<sup>[8](https://www.oracle.com/technical-resources/articles/sharma-indexes.html)</sup> [劳伦斯伯克利国家实验室](https://www.edgepedia.org/zh/lawrence-berkeley-national-laboratory)的研究从研究侧得出了类似结论：压缩位图索引对高基数属性也同样高效，这与它们只适合低基数数据的传统看法相反。<sup>[9](https://sdm.lbl.gov/~kewu/ps/LBNL-59952.html)</sup>

Oracle 自己的索引策略指导仍然按工作负载划出实际界限：对非常低基数的列（如 Y/N 标志或性别）使用位图索引，对具有大量 SELECT 且 INSERT、UPDATE、DELETE 不频繁的数据仓库或报表环境使用位图索引；在 OLTP 中避免使用，因为每条 DML 语句都会锁定持有相同值的所有行的位图。<sup>[2](https://github.com/oracle/skills/blob/main/db/performance/index-strategy.md)</sup>

## 数据仓库中的位图索引

在星型模式中，大的事实表与较小的维度表连接，事实表外键列上的位图索引使 Oracle 的星型转换成为可能。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup><sup> • </sup><sup>[4](https://docs.oracle.com/cd/B12037_01/server.101/b10736.pdf)</sup> Oracle 对事实表列的候选规则结合了大小和使用：每个不同值对应100行或以上，这使位图索引比常规索引小得多且构建更快，加上出现在 WHERE 子句中或作为维度外键的角色。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup>

**位图连接索引**将这一思想扩展到跨表。位图连接索引是针对两个或多个表的连接建立的位图索引，被索引表的位图由被连接表的值构建；在数据仓库中，连接条件是维度主键与事实外键之间的等值内连接。通过存储连接结果，该索引可以在符合条件的查询中完全避免执行连接，Oracle 报告性能提升达一个数量级。限制源于存储连接结果：并行 DML 仅在事实表上受支持，只有一个表可以被不同事务并发更新，任何表不能在连接中出现两次，维度连接列必须是主键或唯一约束列。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup>

另外两个特性适合分析工作负载。位图索引包含空值，因此对空值的查询不需要基于函数的索引；在所有常用查询列上建立位图索引，无论查询使用其中多少列都能为分析师服务，这在没有单一列是 B 树的明显前导列时很重要。<sup>[3](https://oracle.readthedocs.io/en/latest/sql/indexes/bitmap-indexes.html)</sup> 在分区表上，Oracle 位图索引始终是局部的。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup>

## 索引之外的位图技术

位图与组合的思想也出现在不将位图索引存储在磁盘上的引擎中。PostgreSQL 的查询规划器可以通过扫描每个需要的索引、构建匹配行位置的内存位图、并根据查询需要对这些位图进行 AND 或 OR 运算来组合多个 B 树索引；然后按物理顺序访问行，这意味着任何索引顺序都丢失了，对于 ORDER BY 查询需要排序。<sup>[10](https://www.postgresql.org/docs/current/indexes-bitmap-scans.html)</sup>

## 压缩位图格式与 Roaring

由于原始位图在稀疏数据上浪费空间，大量研究投入到压缩格式上。追随 Oracle 的脚步，位图长期使用游程编码（RLE）压缩，产生了 WAH（字对齐混合）和 Concise 等方案。Roaring 格式由 Samy Chambi、Daniel Lemire、Owen Kaser 和 Robert Godin 于2014年提出，在两级结构内用紧凑数组取代 RLE，经测量比基于 RLE 的替代方案压缩效果好约2倍，交集运算速度快达900倍。<sup>[5](https://arxiv.org/abs/1402.6407)</sup> 后续的实现论文报告 Roaring 比 WAH、Concise 和 EWAH 快达两个数量级，同时压缩效果更好，并列出生产用户包括 [Elasticsearch](https://www.edgepedia.org/zh/elasticsearch)、Apache Spark、Netflix 的 Atlas、LinkedIn 的 Pinot、Metamarkets 的 Druid、Pilosa、Apache Hive、Apache Tez、Microsoft Visual Studio Team Services 和 [Apache Kylin](https://www.edgepedia.org/zh/apache-kylin)。<sup>[11](https://arxiv.org/abs/1709.07821)</sup>

压缩位图索引也被推向持续到达的数据。IBM 研究院的一个网络流归档系统将压缩位图索引与基于局部敏感哈希的流重排序相结合，持续插入速率达到每秒50万到100万条记录，而典型的商业网络流解决方案为每秒2万到6万条流，同时提供交互式查询响应。<sup>[12](https://research.ibm.com/publications/real-time-creation-of-bitmap-indexes-on-streaming-network-data)</sup>

## 局限性

决定性的限制是并发修改。位图索引对于有许多并发 DML 语句的在线应用是有问题的，因为死锁和维护位图的开销，Oracle 的指导指出每条 DML 都会锁定具有相同值的所有行的位图，在 OLTP 中造成严重争用。<sup>[3](https://oracle.readthedocs.io/en/latest/sql/indexes/bitmap-indexes.html)</sup><sup> • </sup><sup>[2](https://github.com/oracle/skills/blob/main/db/performance/index-strategy.md)</sup> Oracle 也不支持位图主键索引，因此主键列无论如何都会得到 B 树。<sup>[8](https://www.oracle.com/technical-resources/articles/sharma-indexes.html)</sup> 在分析领域内，权衡较为温和但确实存在：B 树索引仍然是唯一或近乎唯一列的工具，而根据 Oracle 的指导，数据仓库中的大多数索引仍应是位图索引。<sup>[1](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)</sup>

## References

1. [Data Warehousing Optimizations and Techniques, Oracle Database 19c Data Warehousing Guide](https://docs.oracle.com/en/database/oracle/oracle-database/19/dwhsg/data-warehouse-optimizations-techniques.html)
2. [Index Strategy, oracle/skills database performance guide, GitHub](https://github.com/oracle/skills/blob/main/db/performance/index-strategy.md)
3. [Beyond B-Trees: Bitmap Indexes, Oracle SQL & PL/SQL Optimization for Developers](https://oracle.readthedocs.io/en/latest/sql/indexes/bitmap-indexes.html)
4. [Using Bitmap Indexes in Data Warehouses, Oracle Data Warehousing Guide 10g Release 1](https://docs.oracle.com/cd/B12037_01/server.101/b10736.pdf)
5. [Chambi, S., Lemire, D., Kaser, O., Godin, R. (2014). Better bitmap performance with Roaring bitmaps. arXiv:1402.6407](https://arxiv.org/abs/1402.6407)
6. [Wu, K., Otoo, E., Shoshani, A. (2001). Compressed bitmap indices for efficient query processing](https://escholarship.org/uc/item/8k22w7q2)
7. [Johnson, T. et al. (1999). VLDB 1999, pages 278-289](https://www.sigmod.org/publications/dblp/db/conf/vldb/Johnson99.html)
8. [Sharma, V. (2005). Bitmap Index vs. B-tree Index: Which and When? Oracle Technology Network](https://www.oracle.com/technical-resources/articles/sharma-indexes.html)
9. [Wu, K. et al. Bitmap Indices for Data Warehouses, LBNL-59952](https://sdm.lbl.gov/~kewu/ps/LBNL-59952.html)
10. [PostgreSQL Documentation, 11.5. Combining Multiple Indexes](https://www.postgresql.org/docs/current/indexes-bitmap-scans.html)
11. [Lemire, D. et al. (2017). Roaring Bitmaps: Implementation of an Optimized Software Library. arXiv:1709.07821](https://arxiv.org/abs/1709.07821)
12. [Real-time creation of bitmap indexes on streaming network data, IBM Research / VLDB Journal](https://research.ibm.com/publications/real-time-creation-of-bitmap-indexes-on-streaming-network-data)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database theory and data modeling › Indexing and physical data organization

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/bitmap-index

English original: https://www.edgepedia.org/bitmap-index
