Bitmap index
One bit array per distinct value of a column, with bit i set when row i has that value, so a filter is a bitwise AND or OR over arrays instead of a scan over rows. It exists because B-tree indexes on low-cardinality columns such as status or country barely narrow the rows, while a bitmap answers "status = failed AND channel = sms" in a few million CPU-friendly bit operations.
What it costs
| Operation | Cost | Note |
|---|---|---|
| Filter on one value | O(n/64) word reads | Read the value’s bitmap; each 64-bit word covers 64 rows. Compressed bitmaps skip empty runs. |
| Combine filters (AND / OR / NOT) | O(n/64) per operator | Word-at-a-time bitwise ops; SIMD does 4–8 words per instruction. |
| Count matches | O(n/64) popcount | A hardware instruction counts set bits per word; no row is touched. |
| Insert / update a row | O(1) per bitmap touched, but | Appending is cheap; updating clears a bit in one bitmap and sets it in another, which fights with compression. |
Memory: Uncompressed: one bit per row per distinct value, so 250M rows × 4 statuses = 125 MB. Compressed (Roaring), bitmaps for rare values shrink to the size of their set bits: the same column is usually under 20 MB.
One bitmap per value; a filter is a bitwise AND
Each row sets one bit in the status bitmaps and one in the channel bitmaps. The query ANDs "failed" with "sms" word by word; the surviving bits are the matching row numbers, counted with popcount.
How it works
- For a column with few distinct values, create one bit array per value, each as long as the table has rows. Row i has value v exactly when bit i of v’s bitmap is 1. A row sets exactly one bit across the column’s bitmaps.
- To filter status = failed, read the "failed" bitmap: the set bits are the matching row numbers. To filter status = failed AND channel = sms, AND the two bitmaps word by word; the result’s set bits are the rows matching both. OR and NOT work the same way.
- To count matches, run popcount over the result words; the CPU counts set bits 64 at a time without materialising rows. To fetch rows, iterate set bits and read those row positions.
- Compress each bitmap because most are sparse. Roaring bitmaps split the row space into 65,536-row chunks and store each chunk as a sorted array of 16-bit offsets when it has few set bits, a plain 8 KB bitmap when it has many, or a run list when bits come in long runs. Operations work chunk by chunk and skip chunks that are empty on either side.
- Updates are the weak point: changing a row’s status clears a bit in one bitmap and sets it in another, which may split a compressed run. Bitmap indexes therefore live on append-only or batch-updated data, rebuilt per partition.
Why bits beat a B-tree here
- A B-tree on status points at 60 million "failed" rows through 60 million leaf entries; the database may decide a table scan is cheaper. The bitmap is 250M bits = 31 MB, and intersecting two of them is 4M word operations, about a millisecond.
- Boolean combinations of many filters compose for free: five filters are four AND operations, not a five-way index merge.
Worked example
Finding failed SMS deliveries in the notification system
- The delivery table’s daily partition has 250M rows (2,900 inserts per second). Columns with few values: status (queued, sent, delivered, failed), channel (push, sms, email), provider (6 values), region (12 values). A query: how many deliveries failed on sms via provider Twilio in region EU today, and list the first 1,000.
- B-tree approach: an index on status finds 7.5M failed rows; the planner then checks channel, provider and region on each, or merges three more indexes. Several seconds.
- Bitmaps: "failed" has 7.5M set bits of 250M (3%), "sms" 25M, "twilio" 20M, "eu" 50M. Roaring sizes: roughly 15 MB, 30 MB, 25 MB, 40 MB. AND them: the result is driven by the sparsest bitmap, so the engine iterates "failed"’s 7.5M bits in chunks and tests them against the others, skipping chunks where "failed" is empty. Around 20 ms.
- Result: 184,220 set bits. popcount gives the count with no row reads. For the listing, iterate the first 1,000 set bits and fetch those 1,000 rows by position.
- Writes: deliveries append at 2,900 rows per second, each setting one bit in four bitmaps at the end of the arrays, which compresses well as a growing run. Status changes (queued → sent → delivered) are applied in a nightly rebuild of the day’s partition rather than bit by bit, which is why this lives in the analytics store, not the transactional one.
When it is the wrong choice
- High-cardinality columns (user id, timestamp): one bitmap per distinct value means millions of mostly-empty bitmaps. Use a B-tree.
- Rows are updated in place at high rates: every change clears and sets bits and breaks compression. Keep bitmaps on append-only or batch-rebuilt partitions.
- Point lookups of a single row by key: a bitmap answers "which rows match a value", not "where is row K". That is a hash or B-tree job.
- Transactional isolation per row: bitmap updates lock large ranges of rows at once, which is why OLTP databases rarely offer them.
Interviewer follow-ups
Answer out loud before you open each one.
Why is a B-tree a poor index for a status column?
With four values, each value matches a quarter of the table. The index narrows nothing: the database reads a quarter of the leaf entries and then a quarter of the table pages in random order, which is slower than scanning the whole table sequentially. The planner will usually ignore the index.
How does Roaring keep bitmaps small?
It splits the row range into 65,536-row chunks and picks a representation per chunk: a sorted list of 16-bit offsets when fewer than 4,096 bits are set, a raw 8 KB bitmap when more are set, and a list of runs when bits come in long stretches. Operations convert and combine chunk by chunk, skipping chunks that are empty on either side.
How do you count matches without reading rows?
The result of the AND is itself a bitmap. The popcount instruction counts set bits in a 64-bit word in one cycle, so counting 250M rows is about 4M instructions, a millisecond, with no row or page touched.
Where would you use a bitmap index in a system you have designed?
In the analytics store behind dashboards: notification deliveries by status, channel and provider; clicks by country and device; videos by status and visibility. The data is appended in batches and queried with boolean combinations of low-cardinality filters, which is exactly the shape bitmaps are built for.