Sub topic 12
Index Skip Scan and When It Helps
Assume you know basic SELECT and WHERE syntax, composite indexes, and how to read an execution plan with DBMS_XPLAN. This chapter focuses on Oracle B-tree indexes with more than one column.
The leading-column rule
A composite index stores entries in column order. Consider this index:
CREATE INDEX orders_status_customer_ix
ON orders (status, customer_id);The entries are ordered first by status, then by customer_id within each status. A query that supplies both columns can use the index efficiently:
SELECT order_id, order_date
FROM orders
WHERE status = 'OPEN'
AND customer_id = 42;A query that supplies only customer_id does not normally have a regular range scan available. It does not identify one continuous section of the index because matching customer IDs are spread across all status values.
Oracle can sometimes solve this with an index skip scan.
How a skip scan works
A skip scan treats the composite index as several smaller logical indexes, one for each distinct value of the leading column. For the example, Oracle can search as if it had separate indexes for:
status = 'OPEN'status = 'CLOSED'status = 'CANCELLED'
It probes each logical section for customer_id = 42, then combines the results.
The leading column is not ignored. Oracle still searches each distinct leading-column value. “Skip” means that the query does not specify the leading column, so Oracle skips between its distinct values.
When it helps
A skip scan is most useful when the leading column has few distinct values and the later column is selective. For example, status may have only four values, while customer_id identifies a small number of rows.
Oracle may prefer a skip scan when probing those four sections costs less than scanning the table. Current optimizer statistics are important because Oracle needs estimates for:
- The number of distinct
statusvalues. - The number of rows matching
customer_id = 42. - The clustering of table rows.
- The cost of accessing the table after finding index entries.
You can inspect the chosen plan like this:
EXPLAIN PLAN FOR
SELECT order_id
FROM orders
WHERE customer_id = 42;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);Look for INDEX SKIP SCAN in the operation column. The plan is evidence of what Oracle chose for that statement and set of statistics. It is not a permanent guarantee for every value or data distribution.
Common errors and trade-offs
Do not assume that a composite index can efficiently answer every predicate on any indexed column. A skip scan may be expensive when the leading column has many distinct values. Oracle then performs many logical probes, and a full table scan may be cheaper.
Also avoid forcing INDEX_SS hints before checking the plan and measuring the query. Data volume, statistics, selectivity, and the number of matching rows can change the best choice. If the query must return many rows, the table visits after the index scan may dominate the cost.
Exercise
An index exists on (region, accountid), and region has 3 distinct values. A query filters only accountid. What access path might Oracle choose, and when might it reject that choice?
Show answer
Oracle might use an index skip scan, probing the accountid value within each region section. It may reject the skip scan if region has many distinct values, accountid is not selective, statistics are stale, or a full table scan costs less.