Sub topic 16
Indexing join columns
This chapter assumes you can read a basic execution plan with DBMS_XPLAN and know how a B-tree index is searched, including the leading-column rule for composite indexes. It covers which join columns need an index, which side of the join needs it, and how to check that the optimizer uses it.
Which side of the join needs the index
A join does not use an index in one fixed way. The access path depends on the join method the optimizer picks.
- Nested loops read rows from an outer (driving) row source. For each outer row, they look up matching rows in the inner table. The inner table needs an index on its join column, or each lookup becomes a full scan.
- Hash join reads both inputs once, builds an in-memory hash table from the smaller one, and probes it with the other. An index on the join column does not help the join itself.
- Sort-merge join sorts both inputs on the join key. An index can only supply pre-sorted input. This is uncommon.
An index on a join column therefore pays off mostly when the optimizer expects few outer rows, so it can choose nested loops and probe the inner table many times cheaply.
A worked example
Consider a query that finds one customer's recent orders.
SELECT c.last_name, o.order_id, o.order_date
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
WHERE c.customer_id = 42
AND o.order_date >= DATE '2026-01-01';customers.customer_id is the primary key, so Oracle has already created a unique index on it. Oracle does not create an index on the foreign key column orders.customer_id. If you create no index there, the optimizer has to scan all of orders.
CREATE INDEX orders_customer_ix ON orders (customer_id);After gathering statistics, check the plan.
EXPLAIN PLAN FOR
SELECT c.last_name, o.order_id, o.order_date
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
WHERE c.customer_id = 42
AND o.order_date >= DATE '2026-01-01';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));A typical plan has this shape. Exact operations and names vary by version and statistics.
NESTED LOOPS
TABLE ACCESS BY INDEX ROWID CUSTOMERS
INDEX UNIQUE SCAN CUSTOMERS_PK
TABLE ACCESS BY INDEX ROWID ORDERS
INDEX RANGE SCAN ORDERS_CUSTOMER_IX
Predicate Information:
INDEX UNIQUE SCAN: access("C"."CUSTOMER_ID"=42)
INDEX RANGE SCAN: access("O"."CUSTOMER_ID"=42)
TABLE ACCESS: filter("O"."ORDER_DATE">=DATE' 2026-01-01')The join condition, or the constant the optimizer derived from it, shows up as an access predicate on the inner index. Here the optimizer transitively inferred o.customer_id = 42. If the join column appears only under filter on a table access, or the inner side shows TABLE ACCESS FULL, the join is not using an index on that column.
Put the filter column in the same index
In the example, order_date is applied as a filter after the table access. Oracle must fetch each of the customer's orders, then discard the ones outside the date range. A composite index lets the date range become an access predicate too.
CREATE INDEX orders_cust_date_ix
ON orders (customer_id, order_date);The join column goes first, as an equality condition. The range column goes second. This follows the same ordering rule as any composite index, and it lets the same index serve both the join and the range filter. Once you create it, orders_customer_ix is redundant for this query. Any query that needs only customer_id can use the leading column of the composite index. Drop the single-column index unless you have a reason to keep it.
Keep join columns free of functions and mismatched types
The sargability rules from earlier chapters apply to join conditions. A function on either side of the join blocks a normal index on that column.
-- Blocks an index on orders.customer_id: the function is applied to the column
SELECT o.order_id
FROM customers c
JOIN orders o ON TO_CHAR(o.customer_id) = c.customer_ref;
-- Better: join columns of the same type, with no function
SELECT o.order_id
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id;Data type mismatches are a common source of this problem. If one side stores a number in a VARCHAR2 column and the other side is a NUMBER, Oracle converts the character column with TO_NUMBER. An index on the character column is then unusable for the join, and a failing conversion on a non-numeric value raises an error.
The fix is to store the same data type on both sides of every foreign key. If you cannot change the schema, a function-based index on the exact expression used in the join condition can help. Treat that as a workaround, not a design.
Foreign keys and locking
Index foreign key columns even when no query joins on them. Without an index on the child column, Oracle takes a stronger table-level lock on the child table when you delete a parent row or update its key. Concurrent DML on the child table can then block. Oracle also has to scan the child table to check for dependent rows.
You can find foreign key columns that have no matching leading index column with a data dictionary query.
SELECT cc.table_name, cc.constraint_name, cc.column_name
FROM user_constraints c
JOIN user_cons_columns cc
ON cc.constraint_name = c.constraint_name
WHERE c.constraint_type = 'R'
AND cc.position = 1
AND NOT EXISTS (
SELECT 1
FROM user_ind_columns ic
WHERE ic.table_name = cc.table_name
AND ic.column_name = cc.column_name
AND ic.column_position = 1
);This checks only the first column of each foreign key. It can flag a multi-column key that has an index on a different column order, so review each result.
When a missing index is fine
A hash join with full scans is often the best plan, and the optimizer is right to pick it. This happens when:
- the query joins a large share of both tables, such as a report over a full year
- the join returns millions of rows, so a million index probes would cost more than one scan
- the tables are small enough that a full scan costs about as much as one index probe
Adding an index for such a query only adds maintenance cost to every insert, update and delete. Look at the plan and the row counts before adding one. If the optimizer has the right cardinality estimates and still chooses a hash join, accept its choice. If its estimates are wrong, fix the statistics first. See the selectivity and statistics chapter for details.
Choosing the driving table
You rarely need to choose the join order yourself, but you should understand why the optimizer picks it. It usually starts from the table with the most selective filter, so that few outer rows drive the loop. For WHERE c.last_name = 'Smith' on a large customers table, an index on customers.last_name produces few outer rows. An index on orders.customer_id then handles the inner lookup. Each index serves one step in the join, so a fast join often needs an index on both the filter column of the driving table and the join column of the inner table.
Summary of rules
| Situation | Index to consider |
|---|---|
| Foreign key in a child table | Index on the child's foreign key column |
| Join plus a range filter on the inner table | Composite index: join column first, range column second |
| Selective filter on the driving table | Index on that filter column |
| Large join returning much of both tables | Often none; a hash join is fine |
| Join columns of different data types | Fix the data types, then index |
Exercise
A query joins order_items i to orders o on i.order_id = o.order_id and filters o.customer_id = 42. The plan shows a hash join with a full scan of order_items, and order_items has 50 million rows. Only about 20 order rows match. Which index would you add, and what would you expect the plan to change to?
Show answer
Add an index on order_items (order_id). With only about 20 outer rows from orders, the optimizer can use nested loops and probe order_items through the index once per order, replacing the full scan with an INDEX RANGE SCAN. Check the plan to confirm that i.order_id = o.order_id appears as an access predicate on the new index.