Sub topic 3
Selectivity and statistics: why the optimizer may skip an index
This chapter assumes you know basic SELECT, WHERE, and ORDER BY syntax, Oracle data types, and the purpose of a database index.
What selectivity means
Selectivity is the fraction of table rows that satisfy a predicate. A predicate is a condition in a WHERE clause. A condition that matches few rows has high selectivity. A condition that matches most rows has low selectivity.
For example, customer_id = 42 may match one row in a million-row table. status = 'ACTIVE' may match 900,000 rows. The first condition is selective. The second is not.
Oracle estimates how many rows each condition will return. This estimate is called cardinality. The optimizer compares possible access paths, such as an index scan followed by table lookups or a full table scan.
| Predicate | Estimated matching rows | Likely access path |
|---|---|---|
| `customer_id = 42` | 1 | Index range or unique scan |
| `status = 'ACTIVE'` | 900,000 | Full table scan |
| `order_date >= DATE '2026-01-01'` | 10,000 | Depends on table size and clustering |
An index is not automatically faster. An index lookup finds row addresses, but Oracle may then perform a table access for every matching row. If many rows match, reading the table blocks through the index can cost more than scanning the table sequentially.
How statistics guide the decision
Optimizer statistics describe tables, columns, indexes, and data distributions. Oracle uses them to estimate cardinality and cost. Important statistics include:
- Number of rows in the table.
- Number of distinct values in a column.
- Minimum and maximum column values.
- Number of nulls.
- Index leaf blocks and clustering factor.
- Histograms for columns whose values are unevenly distributed.
The clustering factor estimates how well index order matches table block order. A low clustering factor means rows found through the index are grouped in relatively few table blocks. A high clustering factor means an index scan may require many scattered table reads.
Statistics can become stale after large inserts, updates, or deletes. You can refresh them with DBMS_STATS:
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => USER,
tabname => 'ORDERS',
method_opt => 'FOR ALL COLUMNS SIZE AUTO',
cascade => TRUE
);
END;
/SIZE AUTO lets Oracle create histograms when they may improve estimates. cascade => TRUE also gathers related index statistics.
Checking an unexpected full table scan
Use an execution plan to compare Oracle's estimate with the actual result. The E-Rows value is the estimated row count. A large difference between E-Rows and A-Rows can indicate stale statistics, a missing histogram, or a predicate whose selectivity is difficult to estimate.
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE status = 'CANCELLED';
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);A full table scan can be the correct plan. Do not add an index solely because one query does not show an index operation. First check the number of matching rows, table size, statistics freshness, and the query's response-time requirement.
Exercise
Exercise
A table has one million rows, and 950,000 rows have status = 'ACTIVE'. Why might Oracle choose a full table scan even when an index exists on status?
Show answer
The predicate has low selectivity. Using the index would require finding many entries and visiting many table blocks, so a full table scan may have the lower estimated cost.