Sub topic 3
Ordering columns in a composite index
This chapter assumes you can write SELECT with WHERE, JOIN and ORDER BY, you know Oracle types such as NUMBER, VARCHAR2 and DATE, and you can read an execution plan with DBMSXPLAN. The examples use an orders table with `customerid, orderdate and orderid` columns.
Ordering columns in a composite index
A composite index is a B-tree index on more than one column. Oracle sorts its entries by the first (leading) column, then by the second within each value of the first.
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);
-- Uses the leading column (equality) and the second column (range)
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42
AND order_date >= DATE '2026-01-01'
AND order_date < DATE '2026-02-01';Put equality columns first, then the range column. A query that filters only on order_date cannot start at the top of this index, so it usually needs a skip scan or a different index.
Index skip scan and when it helps
An index skip scan lets Oracle search a composite index without a constraint on its leading column. It works best when the leading column has few distinct values. Check the plan to confirm that Oracle uses it.
Covering indexes and avoiding table access
A covering index holds every column the query reads, so Oracle answers the query from the index alone and skips the table.
CREATE INDEX orders_cust_cover_ix
ON orders (customer_id, order_date, order_id);
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42;Using index order for ORDER BY and top-N queries
An index already stores rows in sorted order. For a top-N query, Oracle can read the first entries and stop early.
CREATE INDEX orders_date_ix ON orders (order_date);
SELECT order_id, order_date
FROM orders
ORDER BY order_date DESC
FETCH FIRST 10 ROWS ONLY;Writing OR and IN conditions for index use
An IN list on an indexed column runs as several equality lookups. An OR across one indexed column is often handled the same way. An OR that includes a column with no index usually forces a full table scan, so rewrite it as UNION ALL when performance matters.
Indexing join columns
Index the foreign key column on the child table, such as order_items.order_id. Without that index, Oracle must scan the child table for each parent row in a nested loop join.
The cost of indexes on DML and when not to add one
Each index must update on every INSERT, and on UPDATE or DELETE of an indexed column. Extra indexes slow writes and use storage. Skip an index on a column that no query filters or sorts on.
flowchart TD
A[Query filters on leading column?] -->|Yes| B[Index range scan]
A -->|No| C{Leading column has few distinct values?}
C -->|Yes| D[Index skip scan possible]
C -->|No| E[Full table scan or new index]callout note Always confirm the access path with EXPLAIN PLAN and DBMS_XPLAN.DISPLAY. Do not assume the index is used.
exercise Q: Which index best serves WHERE customer_id = 42 AND order_date >= DATE '2026-01-01'? A: CREATE INDEX orders_cust_date_ix ON orders (customer_id, order_date);. The equality column leads, and the range column follows.
estimate minutes: 20