Sub topic 1
How a B-tree index is searched
Prerequisites
This chapter assumes you know basic SELECT, WHERE, JOIN and ORDER BY syntax. You should understand Oracle types such as NUMBER, VARCHAR2 and DATE, be able to run statements in SQL*Plus or SQL Developer, and know what a database index is.
How a B-tree index is searched
A B-tree index is a sorted tree of blocks. The top block is the root, the middle blocks are branches, and the bottom blocks are leaves. Each leaf entry holds an indexed value and a ROWID, which is the physical address of the matching row. To find a value, Oracle walks from the root to one leaf, then follows the ROWID to the table block.
Checking index use in an execution plan
Use EXPLAIN PLAN and DBMS_XPLAN to see the access path Oracle chose:
EXPLAIN PLAN FOR
SELECT order_id, order_date
FROM orders
WHERE customer_id = 42;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Look for INDEX RANGE SCAN or TABLE ACCESS FULL in the Operation column. The Predicate Information section lists access predicates, which limit the index search, and filter predicates, which are checked after rows are read.
Selectivity and statistics: why the optimizer may skip an index
Selectivity is the fraction of rows a condition matches. A condition that matches 1 row in a million is highly selective. A condition that matches half the table is not. When a query matches many rows, reading the table in order is cheaper than jumping between index and table blocks, so Oracle often chooses a full table scan.
The optimizer estimates selectivity from statistics such as row counts and distinct values. Stale statistics give poor estimates, so refresh them after large loads:
EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'ORDERS');Keeping indexed columns free of functions and expressions
A function applied to an indexed column hides the stored values from the index:
-- Cannot use a range scan on an index on order_date
SELECT order_id FROM orders
WHERE TRUNC(order_date) = DATE '2026-01-15';
-- Can use an index on order_date
SELECT order_id FROM orders
WHERE order_date >= DATE '2026-01-15'
AND order_date < DATE '2026-01-16';Rewrite the condition so the bare column is compared with constants.
Function-based indexes for expressions you cannot remove
Some expressions are required, such as a case-insensitive search. Create an index on the same expression:
CREATE INDEX customers_upper_last_name_ix
ON customers (UPPER(last_name));
SELECT customer_id, last_name
FROM customers
WHERE UPPER(last_name) = 'SMITH';The predicate must use the same expression as the index definition.
Exercise
Why does WHERE TRUNC(orderdate) = DATE '2026-01-15' prevent a range scan on an index on orderdate?
Show answer
TRUNC is applied to the indexed column, so the index holds the untruncated dates and cannot be searched by the truncated value. Rewrite the condition as a half-open range on the bare column.