Sub topic 12
Index skip scan and when it helps
This chapter assumes you can read a basic DBMS_XPLAN.DISPLAY plan. It also assumes you know that a composite index is searched from its leading column. If you have not seen that rule yet, review composite indexes and column order first.
The problem skip scan addresses
A composite index is sorted by its first column, then by its second column within each first-column value. A query that constrains only the second column cannot use a normal range scan on that index, because matching entries are scattered through the whole index.
Oracle has a fallback for this case, the index skip scan. The optimizer treats the index as a set of smaller sub-indexes, one for each distinct value of the leading column. It probes each sub-index for the value you searched for and skips the rest of that sub-index.
How a skip scan works
Suppose an index on (gender, customer_id) where gender has two values, F and M. The query filters only on customer_id.
Oracle does not run two separate queries. It walks the index and, at each leading-column value, jumps down the branch blocks to find where customer_id = 4711 would sit. If it finds a match, it returns it. Then it moves to the next leading value.
The cost depends on the number of distinct values of the leading column, because that sets how many probes Oracle makes. With 2 values it makes 2 probes. With 2 million values it makes 2 million, which is usually worse than a full scan of the index or table.
A worked example
This script creates a table of 200,000 rows and an index whose leading column has only two values.
CREATE TABLE customer_demo AS
SELECT level AS customer_id,
CASE MOD(level, 2) WHEN 0 THEN 'F' ELSE 'M' END AS gender,
'Name' || TO_CHAR(level) AS last_name
FROM dual
CONNECT BY level <= 200000;
CREATE INDEX customer_demo_gender_id_ix
ON customer_demo (gender, customer_id);
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(USER, 'CUSTOMER_DEMO');
END;
/Now search by the second column only.
EXPLAIN PLAN FOR
SELECT /*+ INDEX_SS(c customer_demo_gender_id_ix) */
c.customer_id, c.last_name
FROM customer_demo c
WHERE c.customer_id = 4711;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);The INDEX_SS hint asks for a skip scan so you can see the plan shape. Without the hint, Oracle may choose a skip scan on its own when the statistics make it look cheapest. The relevant part of the plan looks like this. Exact costs will differ on your system.
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| CUSTOMER_DEMO |
|* 2 | INDEX SKIP SCAN | CUSTOMER_DEMO_GENDER_ID_IX |
Predicate Information (identified by operation id):
2 - access("C"."CUSTOMER_ID"=4711)
filter("C"."CUSTOMER_ID"=4711)The same predicate appears under both access and filter for the skip scan step. The access entry means Oracle uses the predicate to navigate the sub-indexes. The filter entry means it also rechecks entries it reads. Seeing both on an INDEX SKIP SCAN line is normal.
When a skip scan helps
A skip scan is a good outcome when all of these hold:
- The leading column has few distinct values, typically a handful such as a status, a flag, a gender code, or a region.
- The column you search on is selective, so each probe returns few rows.
- The index already exists for other reasons, and adding another index would cost more than it saves.
- Statistics are current, so the optimizer knows how many distinct leading values exist.
A skip scan is a poor outcome when the leading column has many distinct values, such as customer_id or a timestamp. Oracle must then make one probe per value.
Comparing the alternatives
For a query that filters only on customer_id against (gender, customer_id), you have several options.
| Option | How it works | Typical fit |
|---|---|---|
| Index skip scan | One probe per distinct `gender` value | Leading column has very few values |
| Index fast full scan | Reads every index block in multiblock I/O | Query is covered by the index and returns many rows |
| Full table scan | Reads the whole table | Query returns a large share of the table |
| New index on `(customer_id)` | Direct range scan with no probes | The query is frequent and must be fast |
If the query is frequent or latency-sensitive, a dedicated index on the searched column is almost always the better answer. A skip scan is a way to get acceptable performance from an index you already have, not a design goal.
Controlling skip scans with hints
You can steer the optimizer in both directions.
-- Ask for a skip scan on a named index
SELECT /*+ INDEX_SS(c customer_demo_gender_id_ix) */ c.customer_id
FROM customer_demo c
WHERE c.customer_id = 4711;
-- Forbid skip scans on that index
SELECT /*+ NO_INDEX_SS(c customer_demo_gender_id_ix) */ c.customer_id
FROM customer_demo c
WHERE c.customer_id = 4711;Use NO_INDEX_SS when a skip scan was chosen from stale or skewed statistics and runs slower than a full scan. Treat hints as a diagnostic tool. If you need one in production code, fix the statistics or the index design.
Common gotchas
- Stale statistics. If the number of distinct leading values has grown since the last gather, the optimizer underestimates the number of probes and picks a skip scan that runs slowly.
- Skewed leading values. The probe count depends on distinct values, but the sub-index sizes can differ greatly. A search that finds nothing is cheap in a small sub-index and expensive in a huge one.
- Range predicates. A skip scan is most efficient with an equality condition on the searched column. Wide ranges force Oracle to read more entries in every sub-index.
- Silent plan changes. A new value in the leading column adds one more probe. Plans that used a skip scan can later switch to a full scan without any code change.
- Reading the plan. An
INDEX SKIP SCANline does not prove the query is fast. Check the number of rows read (useGATHER_PLAN_STATISTICSandDBMS_XPLAN.DISPLAY_CURSORfor actual row counts) and compare it with the rows returned.
Decision checklist
- Is the leading column absent from the
WHEREclause? - How many distinct values does it have? Check
NUM_DISTINCTinUSER_TAB_COL_STATISTICS. - Is the searched column selective?
- Is the query frequent enough to justify its own index?
- Are statistics current?
If the leading column has few values and the query is occasional, accept the skip scan. If the query is frequent, add or reorder an index.
Exercise
A table has an index on (orderstatus, orderid). orderstatus has 4 distinct values and the table has 50 million rows. A report runs once a day with WHERE orderid = :id. A second query runs 500 times per second with the same predicate. For which query is a skip scan acceptable, and what would you do for the other?
Show answer
A skip scan is acceptable for the daily report. Oracle makes about 4 probes, which is cheap, and the index already exists. For the query that runs 500 times per second, create an index on (orderid). That gives a direct range scan with no extra probes and avoids dependence on the distribution of orderstatus. Also check that statistics are current so the optimizer sees the real number of distinct statuses.
Exercise
In a plan, an INDEX SKIP SCAN step lists the same predicate under both access and filter. Is that a problem?
Show answer
No. For a skip scan the predicate on the non-leading column is used to navigate each sub-index (access) and is also rechecked on the entries read (filter). This is expected. Judge the step by how many index entries it reads compared with the rows it returns, not by the duplicated predicate.