Sub topic 9
Bitmap indexes and concurrent DML
This chapter explains why a bitmap index that speeds up reporting queries can slow down a busy transactional table. It assumes you have read the earlier chapters on bitmap structure and use cases. You should also be comfortable with transactions, COMMIT, and the idea that a session holds a lock on a row until it commits or rolls back.
Recap: what a bitmap entry covers
A bitmap index stores, for each distinct key value, a bitmap with one bit per row in a range of rowids. A set bit means that row holds the key value. Oracle compresses these bitmaps and splits them into bitmap segments, each covering a range of rowids.
A B-tree index entry points at one row. A bitmap entry describes many rows, and that difference causes the locking behavior below.
Why DML on bitmap-indexed tables causes contention
In a B-tree index, each entry belongs to one row. When a session changes a row, only that row is locked. Another session can change the neighboring row at the same time.
A bitmap index entry holds the bits for many rows, often hundreds or thousands, depending on how the data is stored and compressed. When a session inserts, deletes, or updates an indexed column, Oracle must change a bit in the bitmap segment that covers that row. The session locks the whole segment until it commits or rolls back.
Another session that needs to change a bit in the same segment must wait, even though it is working on a different row.
The rules for which segments get locked are:
- An
INSERTlocks the segment for the new row's key value. - A
DELETElocks the segment for the deleted row's key value. - An
UPDATEof the indexed column locks two segments, the one for the old value and the one for the new value. - An
UPDATEof a column that is not bitmap-indexed does not touch the bitmap index and causes no bitmap contention. - A
SELECTis never blocked. Readers use read consistency and do not wait for these locks.
The waiting session shows a row lock wait (enq: TX - row lock contention), the same event you see for ordinary row conflicts.
Reproducing the contention
Use the orders table from this tutorial with a bitmap index on status.
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER NOT NULL,
status VARCHAR2(10) NOT NULL,
order_date DATE NOT NULL,
email VARCHAR2(100)
);
CREATE BITMAP INDEX orders_status_bix ON orders (status);
INSERT INTO orders (order_id, customer_id, status, order_date)
SELECT LEVEL, MOD(LEVEL, 50) + 1, 'OPEN', DATE '2026-01-01'
FROM dual
CONNECT BY LEVEL <= 1000;
COMMIT;The 1,000 rows were inserted together, so their rowids are close and they share bitmap segments. Now run two sessions.
-- Session A
UPDATE orders SET status = 'SHIPPED' WHERE order_id = 1;
-- no COMMIT yet-- Session B
UPDATE orders SET status = 'SHIPPED' WHERE order_id = 2;
-- this statement hangsRow 1 and row 2 are different rows, and a table with only B-tree indexes would let both updates run. Here Session B hangs because it needs the same OPEN and SHIPPED segments that Session A holds.
From a third session with access to the dynamic performance views, you can see who is blocked:
SELECT sid, blocking_session, event
FROM v$session
WHERE blocking_session IS NOT NULL;When Session A runs COMMIT, Session B's update completes.
Even an update that does not use the same new value can collide. If Session B had changed row 2 from OPEN to CANCELLED, it would still need the OPEN segment for removing the bit. The old value matters as much as the new one.
Deadlocks
Because an update locks two segments, two sessions can lock them in opposite order. Session A holds OPEN and wants SHIPPED, and Session B holds SHIPPED and wants OPEN. Oracle detects this and raises ORA-00060 in one session. This can happen between sessions that touch entirely different rows, which makes it hard to diagnose from application logic alone.
Other costs of frequent DML
Locking is the main problem, but not the only one.
- Index growth. Many small changes can fragment bitmap segments and increase the index size. A rebuild may be needed to compact it.
- Slower DML. Each change to an indexed column updates a compressed bitmap, which costs more than a single B-tree leaf entry.
- No uniqueness. A bitmap index cannot enforce a unique or primary key constraint. Keys such as
order_idstay on B-tree indexes.
Which workloads are safe
The effect is a matter of how DML arrives, not just how much.
| Workload | Bitmap index suitability |
|---|---|
| Data warehouse loaded once per night, queried all day | Good |
| Reporting table that is only appended to in batches by one process | Good |
| OLTP table with many sessions inserting or updating rows at once | Poor |
| Table where the indexed column is updated often (such as a status field) | Poor |
| Table where only non-indexed columns are updated | Acceptable, since the bitmap is not touched |
The status column in orders is a fair example of the trap. It has low cardinality, which suits a bitmap index for reads, but it is also a column that an order-processing system updates constantly as orders move from OPEN to SHIPPED. If orders is a live transactional table, a B-tree index on status, or no index at all, may be the better choice. If the data is a nightly copy used only for reports, the bitmap index fits.
Loading data into bitmap-indexed tables
A single process doing bulk loads avoids the concurrency problem, but row-by-row maintenance of bitmap indexes is still slow. A common pattern is to make the index unusable, load the data, and then rebuild the index.
ALTER INDEX orders_status_bix UNUSABLE;
INSERT /*+ APPEND */ INTO orders (order_id, customer_id, status, order_date, email)
SELECT order_id, customer_id, status, order_date, email
FROM orders_stage;
COMMIT;
ALTER INDEX orders_status_bix REBUILD;Here orders_stage is a staging table with the same columns as orders. DML is allowed against an unusable index when the parameter SKIP_UNUSABLE_INDEXES is TRUE, which is the default. Queries cannot use the index until it is rebuilt, so run this pattern in a load window. Rebuilding scans the table once and produces a fresh, compact bitmap index.
Diagnosing and deciding
Follow these steps when you suspect bitmap contention.
- Find sessions that are blocked and identify the blocker with
V$SESSION.BLOCKING_SESSION. - Look at the statements both sessions run. Check whether they change a bitmap-indexed column, or insert or delete rows in the table.
- Check the indexes on the table, for example with
USER_INDEXESwhereINDEX_TYPEisBITMAP. - Test dropping the index, or make it invisible as covered in the maintenance chapter, and compare both lock waits and the reporting queries that relied on it.
- If reports need the index and transactions cannot tolerate it, move the reporting workload to a separate copy of the data, or use a B-tree index on a column that the transactions update rarely.
A bitmap index remains a good tool. The point is to keep it on data where one process changes many rows in a batch, and away from data where many sessions change a few rows each.
Exercise
A table tickets has a bitmap index on state (values NEW, ASSIGNED, CLOSED). Session A runs UPDATE tickets SET state = 'CLOSED' WHERE ticket_id = 10 and has not committed. Session B runs UPDATE tickets SET assignee = 'KIM' WHERE ticket_id = 11, where assignee has no bitmap index. Session C runs UPDATE tickets SET state = 'ASSIGNED' WHERE ticket_id = 12, and ticket 12 is currently NEW. Rows 10, 11, and 12 are stored next to each other and share bitmap segments. Which of B and C wait for A, and why?
Show answer
Session B does not wait. It changes only assignee, which is not bitmap-indexed, so it touches no bitmap segment and only locks row 11. Session C may wait, because it changes the indexed column state and needs the segment for NEW (to clear the bit) and the segment for ASSIGNED (to set it). Session A holds the segment for the old value of ticket 10 and the CLOSED segment. C waits if ticket 10's old state was NEW or ASSIGNED, because they would need the same segment. If ticket 10 was previously CLOSED, A's old and new segments are both CLOSED, and C would not collide with A.
Exercise
Why can two sessions that update different rows deadlock when the table has a bitmap index on the updated column, and what does Oracle do about it?
Show answer
An update of a bitmap-indexed column locks two bitmap segments, one for the old value and one for the new value. Session A can hold the segment for value X and wait for value Y, while Session B holds Y and waits for X. Neither can proceed, so Oracle detects the cycle and raises ORA-00060 in one of the sessions, which rolls back its statement. The rows themselves do not conflict. The deadlock comes from the shared bitmap segments.