DB2intermediate

Indexes and Clustering

Understand how DB2 indexes accelerate access, why clustering indexes matter for physical row order, and the tradeoffs of adding indexes.

Indexes are the primary lever DB2 developers pull to influence query performance, and understanding both how they accelerate lookups and what they cost in return is essential for any performance-oriented interview discussion. An index is a separate physical structure (a B-tree) mapping key column values to the physical location of matching rows, letting DB2 avoid scanning an entire table space to find matching rows.

A non-clustering index is the index at the back of a textbook pointing you to scattered page numbers for a topic; a clustering index is more like actually reorganizing the whole book so all the pages about that topic sit physically next to each other — faster to read through in bulk, but every time you insert a new page you have to carefully re-sort the whole section rather than just adding one line to the back index.

Key Concepts

1
A clustering index is special: DB2 attempts to physically store rows in the table space in the same order as that index's key, meaning a query that scans a range of clustered key values can read mostly sequential physical pages rather than jumping around — a substantial performance advantage for range queries and sequential batch processing. Only one index per table can meaningfully be the clustering index (declared via CLUSTER on CREATE INDEX or implicitly the first index created if none is explicit), so choosing the right clustering key is an important upfront design decision, particularly for tables frequently scanned by range or accessed in a particular processing order.
2
Every additional non-clustering index accelerates specific lookup patterns matching its key columns but adds overhead: each INSERT, UPDATE (of an indexed column), or DELETE must also maintain every index on that table, meaning heavily-indexed tables see slower write performance in exchange for faster targeted reads. This tradeoff — read acceleration versus write overhead and storage cost — is exactly what interviewers want you to weigh explicitly rather than treating 'just add an index' as a universally free performance fix.
3
A well-rounded interview answer also touches on composite (multi-column) indexes and the importance of column order within them: a composite index on (STATUS, OPEN_DATE) can satisfy queries filtering on STATUS alone or on both STATUS and OPEN_DATE, but generally cannot efficiently satisfy a query filtering on OPEN_DATE alone, because the leading column of a composite index must typically be part of the query's predicates for the index to be usable at all (reflected in EXPLAIN's MATCHCOLS value).