Sub topic 2
Choosing Column Order in a Composite Index
This chapter assumes you know basic SQL (SELECT, WHERE, ORDER BY), can create tables in Oracle, and understand that the optimizer chooses an execution plan.
Choosing column order in a composite index
A composite index is an index on two or more columns. Oracle sorts the index entries by the first column, then by the second. The leading column therefore matters most. A query can search the index efficiently when its WHERE clause filters on the leading column.
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 INDEX orders_cust_date_ix ON orders (customer_id, order_date);This index helps queries that filter on customerid alone, or on customerid and orderdate together. It helps little with a query that filters only on orderdate, because the matching entries are spread across the whole index.
Checking index use with EXPLAIN PLAN
EXPLAIN PLAN asks the optimizer for its plan without running the query. Run it, then display the plan:
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);In the Operation column, INDEX RANGE SCAN on orderscustdate_ix means the query uses the index. TABLE ACCESS FULL means Oracle reads every row of the table.
How a bitmap index stores data
A bitmap index stores one bitmap for each distinct value in a column. Each bit stands for one row. A bit set to 1 means that row holds the value. Bitmaps suit columns with few distinct values, such as status.
Where bitmap indexes work well
Bitmap indexes fit read-mostly tables, such as reporting data. They work best on low-cardinality columns, meaning columns with few distinct values. Oracle can combine several bitmaps with AND or OR quickly. Bitmap indexes require the Enterprise Edition.
CREATE BITMAP INDEX orders_status_bix ON orders (status);
SELECT COUNT(*)
FROM orders
WHERE status = 'PENDING';Bitmap indexes and concurrent DML
One bitmap entry often covers many rows. Two sessions that change different rows can therefore need the same bitmap entry. Oracle locks that entry, and the second session waits. Avoid heavy concurrent INSERT, UPDATE, or DELETE activity on tables with bitmap indexes.
Exercise
Exercise
Using the index on (customerid, orderdate), which query searches the index more efficiently: WHERE orderdate >= DATE '2026-01-01' alone, or WHERE customerid = 42 AND order_date >= DATE '2026-01-01'? Why?
Show answer
The second query. Its filter starts with the leading column, customer_id, so Oracle can go directly to the matching entries. The first query filters only on the trailing column, so the matching entries are spread across the index.