Sub topic 5
Recognizing access paths
An access path is the method the optimizer chooses to read rows from one table. In a plan, access paths are the operations that touch segments directly: table scans, index scans, and rowid lookups. Everything above them (joins, sorts, aggregates) consumes the rows they produce.
This chapter assumes you can already generate a plan with EXPLAIN PLAN and DBMS_XPLAN.DISPLAY. It also assumes you know the plan is a tree, and that you know indexes and primary keys. The examples use the HR sample schema. Costs, row counts, and even the access path chosen depend on your statistics and version, so treat the plans below as illustrative.
The access paths you will meet most often
| Operation in the plan | What it does | Typical trigger |
|---|---|---|
| TABLE ACCESS FULL | Reads every block below the table's high-water mark | No usable index, or a large fraction of rows is needed |
| TABLE ACCESS BY INDEX ROWID (or BY INDEX ROWID BATCHED) | Fetches table rows using rowids from an index | Child of an index scan when columns beyond the index are needed |
| INDEX UNIQUE SCAN | Finds at most one entry | Equality on a unique or primary key |
| INDEX RANGE SCAN | Walks a range of entries in index order | Equality on a non-unique index, or a range condition |
| INDEX FULL SCAN | Reads all leaf entries in index order | Needs sorted output, or the index covers the query |
| INDEX FAST FULL SCAN | Reads all index blocks with multiblock I/O, unordered | Index alone can answer the query, such as `COUNT(*)` |
| INDEX SKIP SCAN | Probes a composite index without its leading column | Leading column has few distinct values |
The diagram omits cost. The optimizer picks the cheapest path by estimated cost, so a usable index is not always chosen.
Full table scan
EXPLAIN PLAN FOR
SELECT * FROM hr.employees WHERE salary > 10000;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE +ROWS'));---------------------------------------------------
| Id | Operation | Name | Rows |
---------------------------------------------------
| 0 | SELECT STATEMENT | | 15 |
|* 1 | TABLE ACCESS FULL| EMPLOYEES | 15 |
---------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("SALARY">10000)SALARY has no index in the HR schema, so a full scan is the only choice. The predicate is a filter: Oracle reads every row and then discards those that fail the test.
A full scan is not automatically bad. It is the right choice for small tables, and for queries that need a large share of the rows. It uses multiblock reads, which are far cheaper per block than the single-block reads of index lookups.
Unique scan plus rowid lookup
EXPLAIN PLAN FOR
SELECT * FROM hr.employees WHERE employee_id = 100;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));-----------------------------------------------------
| Id | Operation | Name |
-----------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES |
|* 2 | INDEX UNIQUE SCAN | EMP_EMP_ID_PK |
-----------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("EMPLOYEE_ID"=100)Read it as a tree. Operation 2 runs first and returns a rowid. Its parent, operation 1, uses that rowid to read the table block. The predicate is an access predicate on the index operation, which means it drives where the scan starts and stops.
Range scan
EXPLAIN PLAN FOR
SELECT last_name, salary FROM hr.employees WHERE department_id = 10;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));-------------------------------------------------------------
| Id | Operation | Name |
-------------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| EMPLOYEES |
|* 2 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX |
-------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("DEPARTMENT_ID"=10)DEPARTMENT_ID is not unique, so Oracle must scan every index entry with the value 10. BATCHED means Oracle collects several rowids before visiting the table, which can reduce repeated reads of the same block. It does not change which rows are returned.
Which path wins depends on how selective the value is. Department 10 matches one employee. Department 50 matches about 40% of the table, so the optimizer may prefer TABLE ACCESS FULL for that value. The same SQL text can therefore produce different plans for different values, especially when bind variables and histograms are involved.
Index-only access
If the index contains every column the query needs, Oracle skips the table.
EXPLAIN PLAN FOR
SELECT department_id FROM hr.employees WHERE department_id = 10;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));Here the plan has only an INDEX RANGE SCAN of EMP_DEPARTMENT_IX under the SELECT STATEMENT. There is no TABLE ACCESS BY INDEX ROWID line. Missing table access is the sign that the index covers the query.
Two related paths:
INDEX FULL SCANreads leaf blocks one at a time in key order. The optimizer may choose it to avoid a sort.INDEX FAST FULL SCANreads the whole index like a small table, with multiblock reads and no ordering. A query such asSELECT COUNT(*) FROM hr.employeescan use it on an index over aNOT NULLcolumn, because every row has an entry.
Why an index is not used
When you expect an index and see TABLE ACCESS FULL, check the predicate before blaming the optimizer. Common causes:
- A function on the column, such as
WHERE UPPER(last_name) = 'KING'. The index onLAST_NAMEstores unmodified values. Create a function-based index, or rewrite the condition. - An implicit conversion. Comparing a
VARCHAR2column to a number makes Oracle wrap the column inTO_NUMBER. The Predicate Information section shows the wrapped column, which gives it away. - A leading wildcard, as in
LIKE '%ing'. The index cannot be searched by prefix. - A composite index whose leading column is not constrained.
EMP_NAME_IXis on(LAST_NAME, FIRST_NAME). A query onFIRST_NAMEalone can use at most anINDEX SKIP SCAN, and that only pays off when the leading column has few distinct values. - A condition that matches too many rows, so the full scan is cheaper.
EXPLAIN PLAN FOR
SELECT employee_id FROM hr.employees WHERE UPPER(last_name) = 'KING';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));The predicate line reads filter(UPPER("LAST_NAME")='KING') on a TABLE ACCESS FULL. The function call in the predicate explains why the index on LAST_NAME was ignored.
Combining access and filter predicates
An index operation can show both kinds of predicate.
EXPLAIN PLAN FOR
SELECT * FROM hr.employees
WHERE last_name = 'King' AND first_name LIKE '%a%';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));LAST_NAME = 'King' appears as an access predicate on EMP_NAME_IX. The LIKE '%a%' test cannot narrow the index range, so it appears as a filter predicate, applied to each entry or row that the access predicate returned. When you tune, aim for the selective condition to be an access predicate.
Access paths at runtime
With ALLSTATS LAST (covered in a later chapter), compare E-Rows and A-Rows on the access path. Pay attention to Starts. An index scan under a nested loops join runs once for each outer row, so Starts can be large and the Buffers figure shows the total cost. Also, a full scan that returns few rows but reads many buffers means most of the work was discarded by the filter.
Exercise
A plan shows INDEX RANGE SCAN on EMPNAMEIX with "access("LASTNAME"='King')" and "filter("FIRSTNAME" LIKE '%a%')" on the parent TABLE ACCESS BY INDEX ROWID. Which part of the WHERE clause limited the index range, and which part was applied afterwards?
Show answer
The equality on LASTNAME is the access predicate and limited the index range. The LIKE condition on FIRSTNAME is a filter predicate, applied to the rows after the range was read. A leading wildcard cannot be used to position within the index.
Exercise
Why does SELECT departmentid FROM hr.employees WHERE departmentid = 10 show no TABLE ACCESS BY INDEX ROWID line?
Show answer
The index EMPDEPARTMENTIX contains DEPARTMENT_ID, the only column the query needs, so Oracle answers it from the index alone and never visits the table.
Exercise
You run WHERE UPPER(lastname) = 'KING' and get TABLE ACCESS FULL although an index exists on LASTNAME. Name two ways to let Oracle use an index for this query.
Show answer
Create a function-based index on UPPER(lastname), or rewrite the condition to compare the column directly (for example lastname = 'King') if the data's case allows it.