Sub topic 16
Indexing Join Columns
This chapter assumes you know basic SELECT, WHERE, and JOIN syntax, Oracle data types, and the difference between a primary key and a foreign key. You should also be able to read a basic execution plan.
An index on a join column can help Oracle find matching rows without scanning the entire table. The most common case is a parent table with a primary key and a child table with a foreign key:
SELECT o.order_id, c.customer_name
FROM orders o
JOIN customers c
ON c.customer_id = o.customer_id
WHERE c.customer_id = 42;customers.customer_id normally has an index because it is a primary key. The child column, orders.customer_id, often needs an index too. The useful index depends on the join method and the other predicates in the query.
How join indexes help
Oracle chooses a join method based on estimated cost. An index is especially useful for nested loops joins. Oracle reads a small set of rows from one table, then uses an index to find matching rows in the other table.
| Join method | How an index may help |
|---|---|
| Nested loops | Finds matching rows quickly for each row from the driving table |
| Hash join | Often scans both inputs, so a join-column index may not be used |
| Sort merge join | May benefit from ordered access, but often sorts or scans inputs |
For example, if a query selects one customer and that customer has a small number of orders, an index on the foreign key can support efficient lookups:
CREATE INDEX orders_customer_id_ix
ON orders (customer_id);
SELECT o.order_id, o.order_date
FROM orders o
JOIN customers c
ON c.customer_id = o.customer_id
WHERE c.customer_id = 42;The index does not force Oracle to use nested loops. If the query returns most customers or most orders, a hash join and full table scans may cost less. An index is an available access path, not a command.
Which side should be indexed?
Index the child-side foreign key when queries commonly start with parent rows and find their children. This also helps operations on the parent table. When Oracle deletes or updates a parent key, it must check the child table for referencing rows. Without a foreign-key index, Oracle may need to inspect many child rows and can hold stronger locks during the operation.
The parent primary key is already indexed in the usual design. If a join uses a non-key column, consider an index on both columns used to locate rows and filter them:
CREATE INDEX orders_status_customer_ix
ON orders (status, customer_id);
SELECT o.order_id
FROM orders o
JOIN customers c
ON c.customer_id = o.customer_id
WHERE o.status = 'OPEN'
AND c.customer_id = 42;Column order matters. Put columns that form the common access condition first, based on the query workload. Test the actual plan rather than choosing only by intuition.
Verify the chosen access path
Use DBMS_XPLAN after testing representative data:
EXPLAIN PLAN FOR
SELECT o.order_id
FROM orders o
JOIN customers c
ON c.customer_id = o.customer_id
WHERE c.customer_id = 42;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);Look for an index access operation on the child table, such as INDEX RANGE SCAN, and check whether the join condition appears as an access predicate. A full scan is not automatically an error. It can be the correct choice when the join returns many rows.
Exercise
Why can an index on a child table's foreign-key column help when deleting a parent row?
Show answer
Oracle can find referencing child rows through the index instead of scanning the entire child table, which reduces lookup work and can reduce locking impact.