Sub topic 9
Bitmap indexes and concurrent DML
Assume you know basic SELECT and WHERE clauses, can create Oracle tables and indexes, and understand that the optimizer chooses an execution plan. This chapter focuses on why bitmap indexes can be fast for analytical queries but risky for tables that receive frequent concurrent changes.
How a bitmap index stores data
A bitmap index stores a bitmap for each distinct value in a column. Each bit represents a possible row location. A set bit means that the row has that value.
Suppose an orders.status column contains NEW, PAID, and CANCELLED. Oracle can store one bitmap for each status. A query for two values can combine the bitmaps with bitwise operations such as AND and OR.
| Column value | Row 1 | Row 2 | Row 3 | Row 4 |
|---|---|---|---|---|
| `PAID` | 1 | 0 | 1 | 0 |
This representation is compact when a column has few distinct values. It also lets Oracle identify many matching rows with relatively little index data.
Why concurrent DML causes contention
DML means data manipulation language operations such as INSERT, UPDATE, and DELETE.
Changing one table row also requires Oracle to change the relevant bitmap. A bitmap entry represents many row locations, so the index lock can cover more logical rows than the single table row being changed. Two sessions that update different table rows can therefore compete for the same bitmap index entry.
For example, imagine two sessions updating different orders from NEW to PAID:
-- Session 1
UPDATE orders
SET status = 'PAID'
WHERE order_id = 101;
-- Session 2, running at the same time
UPDATE orders
SET status = 'PAID'
WHERE order_id = 202;The table rows are different, but both updates modify the bitmap entries for NEW and PAID. Session 2 may wait for Session 1 to commit. A larger number of concurrent updates can create lock contention and reduce throughput.
The problem is especially important when:
- Many users update rows at the same time.
- Updates change the indexed column.
- Transactions remain open for a long time.
- The table supports order entry, payments, inventory, or another OLTP workload.
A B-tree index usually has better concurrency characteristics for these workloads because its entries identify individual rows more directly.
When a bitmap index fits
Bitmap indexes can suit a reporting table that is loaded in batches and queried repeatedly between loads. Columns such as status, region, gender, or yes/no flags often have low cardinality, meaning they contain relatively few distinct values.
In Oracle, bitmap indexes are an Enterprise Edition feature. A typical definition is:
CREATE BITMAP INDEX orders_status_bix
ON orders (status);The index can help a query that filters on status, especially when the query returns a moderate portion of a large table and combines several low-cardinality predicates.
Do not assume that every low-cardinality column needs a bitmap index. A B-tree index may still be preferable when the table receives frequent DML, when queries usually find only a few rows, or when the application requires high update concurrency.
Choosing between bitmap and B-tree
Use a bitmap index when the table is read-mostly, the indexed column has low cardinality, and analytical queries combine several filters. Use a B-tree index when the table changes frequently or queries commonly locate individual rows.
Index design must match the workload, not just the column definition. Measure query performance and transaction waiting under realistic concurrent activity.
Exercise
Two sessions update different rows, but both change status from NEW to PAID. Why can a bitmap index cause these updates to wait for each other?
Show answer
Both updates must modify the same bitmap entries for NEW and PAID. Bitmap index locking can cover shared index entries, so the sessions can contend even though their table rows are different.