Sub topic 13
Choosing among B-tree, bitmap, and function-based indexes
Assume you know basic SELECT, WHERE, JOIN, and ORDER BY, and that the Oracle optimizer chooses an execution plan for each statement. This chapter uses three index choices:
- A B-tree index stores sorted key values and points to table rows.
- A bitmap index stores bit patterns that represent which rows contain each value.
- A function-based index stores the result of an expression such as
UPPER(email). In Oracle, it is usually a B-tree index unless you explicitly create another type.
Start with the workload
Choose an index from the workload, not from the column definition alone. Ask these questions:
- Which predicates occur often?
- How many different values does the column have?
- Is the table read mostly, or does it receive frequent inserts, updates, and deletes?
- Must the query apply a function to a column?
- Do several sessions modify the table at the same time?
A B-tree is the normal starting point. It works well for highly selective equality searches, ranges, joins, and ordered access. A query is selective when it returns a small fraction of the table.
| Workload characteristic | Suitable choice | Main reason |
|---|---|---|
| Frequent point or range lookups on a busy table | B-tree | Good access speed with normal row-level DML behavior |
| Low-cardinality filters on a read-mostly table | Bitmap | Efficient combination of repeated values |
| Predicate applies an expression to a column | Function-based | Stores the expression result for lookup |
Use a B-tree for general-purpose access
B-tree indexes suit columns such as customer IDs, order dates, email addresses, and primary keys. A composite B-tree can support multiple predicates, but column order matters. An index on (customer_id, order_date) is useful when a query supplies customer_id, then filters or ranges on order_date. It is usually less useful for a query that filters only on order_date.
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Look for INDEX RANGE SCAN on ORDERS_CUST_DATE_IX. A TABLE ACCESS FULL operation means Oracle chose to read the table directly. That choice can be correct when the query returns many rows, the table is small, or statistics are stale.
Use a bitmap for read-mostly, low-cardinality data
A bitmap index represents each distinct value with bits for matching rows. Columns such as status, region, or a yes/no flag may have low cardinality, meaning they contain relatively few distinct values. Oracle can combine bitmap conditions efficiently with bitwise operations.
Bitmap indexes are a poor fit for tables with heavy concurrent DML. One row change can affect bitmap structures covering multiple rows, so sessions can contend for index locks. Use them mainly in reporting or warehouse tables that receive batch loads rather than constant transactional updates.
Use a function-based index when the predicate transforms data
A normal index on email does not generally help a predicate on UPPER(email). Create a function-based index whose expression matches the query:
CREATE INDEX orders_email_upper_ix
ON orders (UPPER(email));
SELECT order_id
FROM orders
WHERE UPPER(email) = '[email protected]';The expression must match closely enough for the optimizer to recognize it. Functions should be deterministic, meaning the same input produces the same result. Gather current optimizer statistics after creating the index.
A virtual column is an alternative. It names the expression in the table definition and lets you index that column. This can make the expression easier to inspect and reuse, while a function-based index keeps the expression only in the index definition.
Test before committing
An index speeds reads but adds storage and work to inserts, updates, and deletes. Test representative queries and DML. You can make an index invisible to the optimizer without dropping it:
ALTER INDEX orders_cust_date_ix INVISIBLE;
-- Run the workload and compare execution plans.
ALTER INDEX orders_cust_date_ix VISIBLE;Rebuild an index only when monitoring or an administration task shows a reason. Do not rebuild indexes merely as routine maintenance.
Exercise
A transactional orders table receives many updates, and queries search by customer_id and an order-date range. Which index type is the safest starting point, and why?
Show answer
Use a composite B-tree index on (customer_id, order_date). It supports the equality and range predicates and handles frequent DML better than a bitmap index.