Sub topic 7
Access and filter predicates
This chapter assumes you can run EXPLAIN PLAN and DBMS_XPLAN.DISPLAY and that you can read a plan as a tree. It also assumes you know what a B-tree index is and what its leading column means. If you can find the operation with the deepest indentation and say that it runs first, you have enough background.
Where predicates appear
Below the plan table, many plans contain a section titled Predicate Information (identified by operation id). Each line starts with an operation Id and says what Oracle does with a condition at that step. A star next to an Id in the plan table (* 3) means that operation has an entry in this section.
Two labels matter:
- access: the condition is used to locate rows. It limits which index entries, hash buckets or sorted rows Oracle visits.
- filter: the condition is checked on rows that have already been read. Rows that fail are thrown away.
The same condition can be an access predicate in one plan and a filter predicate in another. Plans that read many rows and keep few often show it as a filter. The label tells you where the condition does its work.
The section appears with the TYPICAL format (the default for DISPLAY) and with ALL. BASIC leaves it out. To keep the compact layout and add the section, use BASIC +PREDICATE.
EXPLAIN PLAN FOR
SELECT last_name
FROM hr.employees
WHERE department_id = 50
AND salary > 3000;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));Filter predicates on a full scan
A full table scan has no way to jump to matching rows. It reads every block and tests each row. Every condition on a full scan is therefore a filter.
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
|* 1 | TABLE ACCESS FULL| EMPLOYEES |
Predicate Information (identified by operation id):
1 - filter("DEPARTMENT_ID"=50 AND "SALARY">3000)The plan above is abbreviated and illustrative. EMPLOYEES has about 100 rows, so the optimizer often chooses a full scan even when an index exists. Your output may differ.
Access and filter predicates on an index
Indexes show the difference most clearly. An index range scan walks the B-tree to a start key, reads entries in order, and stops at a stop key. A condition that defines those keys is an access predicate.
The HR schema has EMP_NAME_IX on (LAST_NAME, FIRST_NAME). Consider this query:
EXPLAIN PLAN FOR
SELECT employee_id
FROM hr.employees
WHERE last_name LIKE 'K%'
AND first_name = 'Steven';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));If the optimizer uses the index, you will see something like this:
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES |
|* 2 | INDEX RANGE SCAN | EMP_NAME_IX |
Predicate Information (identified by operation id):
2 - access("LAST_NAME" LIKE 'K%')
filter("FIRST_NAME"='Steven' AND "LAST_NAME" LIKE 'K%')Operation 2 has both labels. The LIKE 'K%' condition on the leading column sets the scan range, so Oracle reads only index entries whose last name starts with K. Within that range, FIRST_NAME is not contiguous. Last names such as Kaufling and Khoo have different first names mixed in. Oracle cannot jump to Steven, so it reads each entry in the range and tests the first name as a filter.
This is a general rule for composite indexes. Conditions on the leading columns can become access predicates. Equality on the leading column followed by equality on the next column lets both be used as keys. A range condition on one column stops later columns from narrowing the scan. Those later conditions become filters on the index step.
Filters on the table access step
A filter on a TABLE ACCESS BY INDEX ROWID operation means the condition could not be tested from the index. Oracle fetched the row from the table before it could test the condition.
EXPLAIN PLAN FOR
SELECT last_name
FROM hr.employees
WHERE department_id = 50
AND salary > 10000;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));With the single-column index EMP_DEPARTMENT_IX in use, expect this shape:
|* 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES
|* 2 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX
1 - filter("SALARY">10000)
2 - access("DEPARTMENT_ID"=50)The index finds every row in department 50. Only then does Oracle read each row from the table and discard those with a salary of 10000 or less. If department 50 had a million rows and few high earners, the table-level filter would be costly. A composite index on (department_id, salary) would turn both conditions into access predicates.
Predicates on joins
Join conditions also appear in this section, and the label depends on the join method.
EXPLAIN PLAN FOR
SELECT e.last_name, d.department_name
FROM hr.employees e
JOIN hr.departments d ON d.department_id = e.department_id
WHERE e.salary > 10000;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +PREDICATE'));For a hash join, the join condition appears as an access predicate on the HASH JOIN operation. Oracle uses it to probe the hash table. A single-table condition appears as a filter on the scan of that table:
|* 1 | HASH JOIN
|* 2 | TABLE ACCESS FULL| EMPLOYEES
| 3 | TABLE ACCESS FULL| DEPARTMENTS
1 - access("D"."DEPARTMENT_ID"="E"."DEPARTMENT_ID")
2 - filter("E"."SALARY">10000)In a nested loops join, the join condition usually moves to the inner (second) child. It becomes an access predicate on an index scan, for example access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID"). That step runs once per row from the outer side. Check its Starts value in the runtime statistics to see how many times.
Deciding which label you get
The diagram shows the question Oracle answers for each condition at each operation.
What turns an access predicate into a filter
Several common patterns remove a condition from the access list:
- A function on the column.
WHERE UPPER(last_name) = 'KING'cannot useEMP_NAME_IX. The plan showsfilter(UPPER("LAST_NAME")='KING')on a full scan. A function-based index onUPPER(last_name)would restore an access predicate. - A skipped leading column. A condition only on
FIRST_NAMEcannot define a range inEMP_NAME_IX. Oracle may do a full index scan with a filter, or an index skip scan. - A range before an equality. After a range condition on one index column, later columns are tested as filters.
- Implicit data type conversion. If you compare a
VARCHAR2column to a number, Oracle converts the column. The predicate then showsTO_NUMBER("COL")orINTERNAL_FUNCTION("COL"), and the index on that column is not used as a key. Compare like with like. - A leading wildcard.
LIKE '%ing'has no fixed prefix, so it cannot set a start key.
Bind variables appear as :B1, :1 or :NAME in this section. When you read a plan from DISPLAY_CURSOR, the values peeked at parse time may be listed under Peeked Binds if you request the +PEEKED_BINDS modifier.
Using predicates with runtime statistics
The Predicate Information section tells you where a condition is applied. The runtime statistics tell you what that cost. Run the statement with row-source statistics, then show the actual plan:
SELECT /*+ GATHER_PLAN_STATISTICS */ last_name
FROM hr.employees
WHERE department_id = 50
AND salary > 10000;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(format => 'ALLSTATS LAST +PREDICATE'));Compare A-Rows on the index step with A-Rows on the table step. If the index step returns 45 rows and the table step with the filter returns 0, Oracle fetched 45 table rows to discard them all. The Buffers column shows the block reads behind that. A large gap between a child's A-Rows and its parent's A-Rows marks a filter that does a lot of work. It is a candidate for a better index.
Checklist for one operation
- Find operations with a
*next to the Id. - Read the lines for that Id in the Predicate Information section.
- Decide whether each condition is an access or a filter.
- For an index scan, check which index columns the access predicate uses. Check whether any condition on later columns fell back to a filter.
- Look for functions, conversions or wildcards that explain a filter.
- Confirm the cost with
A-RowsandBuffersfromALLSTATS LAST.
Exercise
An index exists on EMPLOYEES(DEPARTMENTID, SALARY). The query is SELECT lastname FROM hr.employees WHERE departmentid = 50 AND salary > 10000. Which predicate labels do you expect on the INDEX RANGE SCAN, and how does this differ from a plan that uses only an index on DEPARTMENTID?
Show answer
Both conditions are leading-column-compatible, so both become access predicates on the INDEX RANGE SCAN: access("DEPARTMENTID"=50 AND "SALARY">10000). Oracle reads only index entries for department 50 with salary above 10000, so the table access step has no salary filter and fetches only matching rows. With only a DEPARTMENTID index, salary is a filter on the TABLE ACCESS BY INDEX ROWID step, and Oracle fetches every department 50 row before discarding most of them.
Exercise
A plan shows a TABLE ACCESS FULL on EMPLOYEES with filter(UPPER("LASTNAME")='KING'), although EMPNAME_IX exists. Why is the index not used as an access path, and what is one fix?
Show answer
The function UPPER is applied to the indexed column, so the index entries (stored as plain last names) cannot be searched by that value. The condition can only be tested on each row as a filter. Fixes: create a function-based index on UPPER(lastname), or rewrite the query to compare lastname directly if the data's case allows it.