INDEX_FFS and covering: not reading the table

If every needed column is in the index, the table need not be read. Look at INDEX_FFS, which reads the whole index instead of the table, and at building a covering query by reducing SELECT * to the needed columns.

SQL

Situation

An operations dashboard shows order counts by status and the time of the last order. There is also a feature that downloads a list of "paid orders since June", and this list actually uses only the order ID and order time. Both queries read the whole table or many blocks.

Tables

TableColumnTypeDescription
ORDERSORD_IDNUMBEROrder ID (PK)
ORDERSSTATUSVARCHAR23 order statuses
ORDERSORD_DTDATEOrder date and time
ORDERSOthersCUST_ID, ORD_NO, AMT
IndexColumns
ORDERS_X2STATUS, ORD_DT, ORD_ID

The ORDERS table is about 14,000 blocks, and ORDERS_X2 is about 8,300 blocks.

① Count by status and last order time

BEFORE · Table FULL SCAN

SELECT /*+ FULL(o) */ status, COUNT(*) AS cnt, MAX(ord_dt) AS last_dt
FROM orders o
GROUP BY status

② List of paid orders since June

**BEFORE · SELECT ***

SELECT o.*
FROM orders o
WHERE o.status = 'PAID'
  AND o.ord_dt >= DATE '2025-06-01'

For both queries, check whether the execution plan has TABLE ACCESS and which columns the query uses.

Approach

1. List every column the query uses

List every column in the SELECT, WHERE, GROUP BY, and ORDER BY clauses, and check whether an index contains all of them. If one does, there is no reason to read the table.

2. Check whether the index can serve as a "small table"

① has no condition, so it must read everything anyway. But if the needed columns (STATUS, ORD_DT) are all in the index, it can read the whole smaller index instead of the large table. No order is needed here, so index blocks can be read several at a time in storage order.

3. Ask whether every column is really needed

Because of SELECT * in ②, it visits the table for each row to fetch columns not in the index (AMT, CUST_ID, etc.). Keeping only the columns the screen or file actually uses may let the index alone finish the job.

Explanation

① FULL → INDEX_FFS

AFTER · INDEX_FFS

SELECT /*+ INDEX_FFS(o orders_x2) */ status, COUNT(*) AS cnt, MAX(ord_dt) AS last_dt
FROM orders o
GROUP BY status

Why it got faster

  • Before #1 reads the whole table (about 14,000 Buffers).
  • After #1 reads the whole index (about 8,300 Buffers) with INDEX FAST FULL SCAN. Both needed columns are in the index, and the index is smaller than the table.
  • INDEX FAST FULL SCAN reads the index in storage order, not key order, several blocks at a time like a FULL SCAN. So the result is not sorted, but it is fast.
  • With the hint removed, the optimizer chooses this plan (the FULL hint was blocking it).

② SELECT * → only the needed columns

AFTER · Covering (needed columns only)

SELECT o.ord_id, o.ord_dt
FROM orders o
WHERE o.status = 'PAID'
  AND o.ord_dt >= DATE '2025-06-01'

Why it got faster

  • Before #2 uses about 1,770 Buffers. It found about 16,600 rows through the index and then read a table block for each row with TABLE ACCESS BY INDEX ROWID.
  • After #2 uses about 90 Buffers. ORD_ID, ORD_DT, and STATUS are all in ORDERS_X2, so INDEX RANGE SCAN alone finishes the job. TABLE ACCESS disappears from the execution plan.

Why this is optimal

  • ① aggregates everything without a condition, so any structure must be read in full once. It picked the smallest structure (the index).
  • ② reads only the index blocks of the needed range. It must read as many rows as are in the range, so it cannot go lower.

Key points

  • Covering index: if every column the query uses is in the index, the table is not read. SELECT * is the most common cause of breaking coverage.
  • INDEX FAST FULL SCAN: when everything must be read, it reads an index smaller than the table instead. Sort order is not guaranteed.
  • INDEX FULL SCAN (key order, one block at a time) and INDEX FAST FULL SCAN (storage order, several blocks at a time) differ. When no sort is needed, FFS is faster.
  • Differences by DB: In PostgreSQL, Index Only Scan plays the same role; in SQL Server, a covering nonclustered index (INCLUDE columns) does. PostgreSQL may still check the table (heap) depending on the visibility map state (Heap Fetches).