Sub topic 5
Recognizing Access Paths
Assume you know how to write SELECT statements, joins, and WHERE clauses. You should also know that indexes store ordered references to table rows. This chapter focuses on how Oracle reaches rows, which is called the access path.
An access path is the method Oracle chooses to find rows in a table. The optimizer chooses it from available indexes, table statistics, predicates, and estimated costs. An access path is only one part of a complete execution plan. In a plan, look for operations such as TABLE ACCESS FULL, INDEX UNIQUE SCAN, and INDEX RANGE SCAN.
Full table scans
A full table scan reads the table's blocks and checks rows against the query predicates. It does not mean that every row must match the predicate.
EXPLAIN PLAN FOR
SELECT last_name, salary
FROM hr.employees
WHERE salary > 10000;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC'));A full scan can be the best choice when:
- The table is small.
- The query returns a large percentage of the table.
- No useful index exists.
- Reading the table sequentially costs less than visiting many table blocks through an index.
Do not label every full scan as a problem. An index is useful only when it reduces the work needed to find the requested rows.
Index access paths
An index access path first searches an index. Oracle may then visit the table rows identified by the index.
| Plan operation | Typical meaning |
|---|---|
| `INDEX UNIQUE SCAN` | Finds at most one index entry from a unique equality condition |
| `INDEX RANGE SCAN` | Finds one or more entries in an ordered index range |
| `INDEX FULL SCAN` | Reads the whole index in index order |
| `INDEX FAST FULL SCAN` | Reads all index blocks without preserving index order |
For example, a primary-key lookup commonly produces an INDEX UNIQUE SCAN:
EXPLAIN PLAN FOR
SELECT last_name
FROM hr.employees
WHERE employee_id = 100;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC'));An equality condition on a nonunique indexed column, or a range condition such as salary > 10000, commonly produces an INDEX RANGE SCAN. The word range includes a range containing one value. It does not mean that the query must use > or <.
After finding index entries, Oracle often performs TABLE ACCESS BY INDEX ROWID. A rowid identifies the physical location of a table row. This two-step path is efficient when the index identifies relatively few rows. If it identifies many scattered rows, repeated table access can cost more than a full scan.
Choosing between table and index access
The optimizer estimates how many rows each operation will return. It may choose a full scan even when an index exists because the query returns many rows or because the indexed value is not selective. Selectivity describes how much a predicate narrows the result. A predicate that returns 1 percent of a table is more selective than one that returns 70 percent.
Composite indexes and leading columns
For an index on (department_id, salary), a predicate on department_id can use the index's leading column. A predicate only on salary may not use that index efficiently. Oracle can sometimes use an index skip scan, but that is a special case and is not equivalent to having an index beginning with salary.
A practical reading method
When you see an access path:
- Identify the table or index named by the operation.
- Check the estimated rows and cost.
- Look for
TABLE ACCESS BY INDEX ROWIDbeneath or beside the index operation. - Read the Predicate Information section to see which condition drove the index lookup.
- Later, compare estimated rows with actual rows using
DBMS_XPLAN.DISPLAY_CURSOR.
Exercise
A query returns 90 percent of a large table, but an index exists on its filtered column. Why might Oracle choose TABLE ACCESS FULL?
Show answer
Reading the table sequentially may require less work than finding many index entries and visiting most table blocks through rowids.