Sub topic 8
Where bitmap indexes work well
This chapter assumes you know the basic structure of a bitmap index (one bitmap per distinct key value, one bit per row), how to read an EXPLAIN PLAN output, and how B-tree indexes behave. The goal here is to decide when a bitmap index is a good fit and to recognize the cases where it is not.
The two conditions: low cardinality and read-mostly data
Two properties of a workload decide whether a bitmap index suits it.
- Low cardinality relative to the row count. Cardinality is the number of distinct values in a column. A bitmap index keeps one bitmap per distinct value, so few distinct values means few bitmaps, and each bitmap is compressed well.
- Read-mostly data. Bitmap indexes are cheap to read and combine, but expensive to modify under concurrent DML. Tables that are loaded in batches and then queried suit them well.
Bitmap indexes are an Enterprise Edition feature. If your database runs another edition, CREATE BITMAP INDEX fails.
CREATE BITMAP INDEX orders_status_bix ON orders (status);The status column in the orders table is a typical candidate. It might hold values such as NEW, PAID, SHIPPED, and CANCELLED. A table with millions of orders still has only four bitmaps in this index.
Low cardinality is relative
There is no fixed threshold such as "fewer than 100 distinct values". What matters is the ratio of distinct values to rows. A column with 10,000 distinct values on a table of 500 million rows has an average of 50,000 rows per value, which compresses well. The same 10,000 values on a table of 20,000 rows give about two rows per value, and a B-tree index is the better choice.
A rough guide:
| Distinct values compared to rows | Typical choice |
|---|---|
| Very few (for example, 5 values in 10 million rows) | Bitmap, if the table is read-mostly |
| Moderate (for example, 10,000 values in 500 million rows) | Bitmap is possible; test it against a B-tree index |
| Nearly unique (for example, `order_id`, `email`) | B-tree |
Treat this table as a starting point. Measure with your own data.
Why bitmaps are efficient for low-cardinality columns
A B-tree index entry stores the key value plus a ROWID for every row. For a column with four values on a 10-million-row table, that means 10 million entries, each repeating the key. A bitmap index stores one compressed bitmap per value. Runs of zeros and ones compress to very small sizes, so the index is often a small fraction of the size of the equivalent B-tree index.
You can check the actual sizes in the data dictionary after creating both kinds of index on a test copy of the table:
SELECT segment_name, ROUND(bytes / 1024 / 1024, 1) AS size_mb
FROM user_segments
WHERE segment_name IN ('ORDERS_STATUS_BIX');Create a B-tree index on the same column in a test schema and run the same query to compare. The ratio depends on the data distribution and on how the rows are ordered in the table, so do not assume a fixed saving.
Combining several bitmap indexes
The strongest use case for bitmap indexes is a query that filters on several low-cardinality columns together. Oracle can fetch the bitmap for each predicate and combine them with fast bitwise operations (AND, OR, NOT) before it touches the table.
Suppose a reporting table has separate bitmap indexes on status, region, and channel:
SELECT COUNT(*)
FROM orders
WHERE status = 'PAID'
AND region = 'EMEA'
AND channel = 'WEB';Each predicate alone may match a large share of the table, so no single B-tree index on one column would be selective enough. Combined, the three conditions may match a small share. With bitmap indexes, the optimizer can:
- Read the bitmap for
status = 'PAID'. - Read the bitmap for
region = 'EMEA'. - Read the bitmap for
channel = 'WEB'. - AND the three bitmaps to get one bitmap of matching rows.
- Convert the result to ROWIDs, or, for
COUNT(*), count the set bits directly.
For a COUNT(*) query like this one, Oracle may never need to read the table at all. This is the reason bitmap indexes are common in data warehouses and reporting systems.
Checking the plan
Use EXPLAIN PLAN to confirm that the optimizer uses the bitmaps:
EXPLAIN PLAN FOR
SELECT COUNT(*)
FROM orders
WHERE status = 'PAID'
AND region = 'EMEA'
AND channel = 'WEB';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);This query needs bitmap indexes on region and channel in addition to orders_status_bix, and the orders table shown earlier has no region or channel column. Add them to a test table first. In the plan, look for operations such as BITMAP AND, BITMAP INDEX SINGLE VALUE, and BITMAP CONVERSION COUNT or BITMAP CONVERSION TO ROWIDS. If you see TABLE ACCESS FULL instead, the optimizer judged that a scan is cheaper. That judgment depends on statistics and on how much of the table the predicates match.
Predicates that bitmap indexes handle well
Bitmap indexes support more predicate types than many people expect.
- Equality, such as
status = 'PAID', uses one bitmap. INlists andORconditions, such asstatus IN ('NEW', 'PAID'), combine bitmaps with a bitwise OR.NOTand inequality, such asstatus <> 'CANCELLED', can be answered with bitmap operations.IS NULL, which a B-tree index on a single column cannot answer, because B-tree indexes do not store entries where all indexed columns are null. Bitmap indexes do store a bitmap for null keys.COUNT(*)over a combination of predicates, as shown above.
The IS NULL behavior is a practical advantage. If a query asks for rows where shipped_date IS NULL, a bitmap index on that column can serve it, while a single-column B-tree index cannot.
Where bitmap indexes work poorly
Frequent concurrent DML
A bitmap covers a range of rows. When a session inserts, updates, or deletes a row, Oracle must change the bitmap for the key value, and it locks the bitmap entry, which covers many rows, not only the one you changed. Other sessions that change any row covered by the same bitmap entry have to wait. The next chapter covers this in detail. For now, the rule is simple: an OLTP table with many sessions inserting and updating rows at once is a poor home for bitmap indexes.
High-cardinality columns
With nearly unique values, each bitmap has one or very few set bits. The index loses its compression advantage and can become larger than a B-tree index on the same column. It also gives you nothing a B-tree index does not already provide.
Range scans on ordered data
A B-tree index stores keys in sorted order, so range predicates such as order_date BETWEEN ... can read adjacent leaf entries. Bitmap indexes can handle some range predicates, but a B-tree index is the usual choice for ranges and for queries that return rows in key order without a sort.
Unique constraints
A bitmap index cannot enforce uniqueness. Use a B-tree index (created by a PRIMARY KEY or UNIQUE constraint) for that.
A typical bulk-load pattern
In a warehouse, the common pattern is to load data in a batch window and then query it. Bitmap indexes are expensive to maintain row by row during a large load, so many teams mark them unusable, load, then rebuild.
ALTER INDEX orders_status_bix UNUSABLE;
-- bulk load here, for example with INSERT /*+ APPEND */ ... SELECT ...
ALTER INDEX orders_status_bix REBUILD;While an index is unusable, the optimizer ignores it and DML on the table skips its maintenance. Always rebuild before the reporting workload starts, or queries lose the index. Test this pattern on your own load size, since a rebuild reads the whole table.
A decision checklist
Ask these questions about a candidate column.
- Is the number of distinct values small compared to the number of rows?
- Is the table mostly read, with changes arriving in controlled batches?
- Do queries combine this column with other low-cardinality predicates, or count rows?
- Is Enterprise Edition available?
If all four answers are yes, a bitmap index is a strong candidate. If the table takes concurrent single-row DML all day, prefer a B-tree index even on a low-cardinality column, and accept the larger size.
Summary
Bitmap indexes suit columns with few distinct values on tables that are mostly read. They are small, they combine through bitwise operations, they answer IS NULL and COUNT(*) queries, and they work best when several low-cardinality predicates are used together. They suit data warehouses and loaded-then-queried reporting tables. They suit OLTP tables poorly because of locking on bitmap entries, and they cannot enforce uniqueness.
Exercise
A table shipments has 50 million rows and is loaded once each night. Analysts run queries such as "count shipments where carrier = 'X' and priority = 'HIGH' and delivereddate IS NULL". The columns carrier (12 values) and priority (3 values) have few distinct values, and `delivereddate` is null for about 2% of rows. Which index types would you consider, and why? Name one change to the workload that would make you reconsider.
Show answer
Bitmap indexes on carrier, priority, and delivered_date are a good fit. The table is read-mostly with a nightly batch load, the columns are low cardinality relative to 50 million rows, and the query combines several predicates and counts rows, so Oracle can AND the bitmaps and count set bits without reading the table. A bitmap index also stores null keys, so it can answer delivered_date IS NULL, which a single-column B-tree index cannot. This assumes Enterprise Edition. If the nightly load became many concurrent sessions updating individual shipments during the day, for example marking each one delivered as it happens, locking on bitmap entries would cause waits, and I would reconsider B-tree indexes.
Exercise
A colleague proposes a bitmap index on orders.customer_id, which has 2 million distinct values across 4 million rows, because "bitmap indexes are faster." Give two reasons to decline.
Show answer
First, the column is nearly unique relative to the row count, so each bitmap has only about two set bits, the compression advantage disappears, and the index can be larger than a B-tree index. Second, a lookup by customer_id is highly selective, which a B-tree index already serves well, and the orders table is likely subject to concurrent DML, where bitmap entry locking would cause contention. A B-tree index (or a composite index such as (customer_id, order_date)) is the better choice.