Sub topic 15
Performance and Indexing Considerations
Assume you know basic SELECT, WHERE, and ORDER BY, primary and foreign keys, self-joins, and the Oracle DUAL table. Hierarchical queries can become expensive because Oracle may visit many rows at every level. Performance depends mainly on the starting rows, the direction of traversal, branch filters, and indexes.
Understand the access pattern
In a top-down query, Oracle starts with root rows and repeatedly finds children:
SELECT employee_id, last_name, LEVEL
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;For each employee, Oracle searches for rows whose manager_id equals that employee's employee_id. An index on employees(manager_id) supports this repeated child lookup.
In a bottom-up query, Oracle starts with one employee and repeatedly finds the manager:
SELECT employee_id, last_name, LEVEL
FROM employees
START WITH employee_id = 107
CONNECT BY employee_id = PRIOR manager_id;The primary-key index on employee_id supports both the starting lookup and the searches for each manager. The manager_id index is still useful when other queries traverse downward.
| Query shape | Useful index | Main reason |
|---|---|---|
| Find children from each manager | `employees(manager_id)` | Locates rows below the current parent |
| Find one starting employee | Primary key on `employees(employee_id)` | Locates the starting row quickly |
| Find managers above an employee | Primary key on `employees(employee_id)` | Locates each parent row |
| Find children with an extra selective condition | `(manager_id, salary)` or a suitable composite index | Filters child candidates during traversal |
A normal Oracle B-tree index does not store rows where every indexed column is null. Therefore, an index containing only manager_id may not help much with START WITH manager_id IS NULL. If root lookup is a bottleneck, inspect the plan before adding a composite index such as (manager_id, employee_id).
Inspect the execution plan
Use EXPLAIN PLAN to see Oracle's intended operations:
EXPLAIN PLAN FOR
SELECT employee_id, last_name, LEVEL
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);Look for the access method on employees. An index range scan can indicate that Oracle is using an index to find child or parent rows. A full table scan is not automatically wrong. It may be cheaper when the query returns a large part of the table.
To compare estimated work with actual row counts, add the gather_plan_statistics hint and inspect the cursor after running the query:
SELECT /*+ gather_plan_statistics */
employee_id, last_name, LEVEL
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL, NULL, 'ALLSTATS LAST'));Filters, sorting, and repeated work
A condition in WHERE is applied after hierarchical row selection. A condition in CONNECT BY can prune a branch:
SELECT employee_id, last_name, LEVEL
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
AND salary > 5000;Pruning can reduce work, but it also changes the result because descendants of a rejected employee are not visited. Use ORDER SIBLINGS BY when branch order matters. Oracle may sort each group of siblings, so this adds memory and CPU work. An index does not guarantee the requested sibling order.
Exercise
Which index most directly supports a top-down query that joins each employeeid to child managerid values, and how should you verify its benefit?
Show answer
Create or use an index on employees(managerid), then compare estimated and actual plans with DBMSXPLAN. Confirm that the index reduces work for the real data rather than assuming an index scan is always faster.