What is a composite index and how does the prefix rule apply?
A composite index (also called a multi-column index) is an index built on two or more columns in a defined order. The key principle governing its use is the leftmost prefix rule: the database can use the index only when the query's predicates include the leading columns of the index in order, without skipping.
For a composite index on `(last_name, first_name, birth_year)`, the following queries can use the index: filtering on `last_name` alone, `last_name + first_name`, or `last_name + first_name + birth_year`. Filtering on `first_name` alone or `birth_year` alone cannot use the index because the leftmost column (`last_name`) is absent from the predicate.
The prefix rule exists because composite indexes are sorted first by the leftmost column, then by the next column within each group of equal leftmost values, and so on. Scanning the index without the leftmost column would require a full index scan — at which point a full table scan may be comparable or cheaper.
Range predicates on an intermediate column stop the index from being used for filtering on subsequent columns. For `WHERE last_name = 'Smith' AND first_name LIKE 'J%' AND birth_year = 1985`, the index is used for `last_name` (equality) and `first_name` (range), but `birth_year` cannot be satisfied from the index — it requires a post-filter on qualifying rows.
Composite index column order should reflect your most selective and most commonly used filter first. Equality predicates should come before range predicates in the index definition, as range predicates terminate prefix utilization. Covering indexes extend this concept: by including all columns referenced in the query (SELECT + WHERE + ORDER BY), the database can satisfy the query entirely from the index without touching the heap table, eliminating a second I/O lookup.
Correctly states the leftmost prefix rule with an example, explains why skipping the first column prevents index use, and understands that the rule applies to WHERE clause filtering.
Explains why range predicates on an intermediate column block use of subsequent columns, discusses optimal column ordering (selectivity + equality before range), and explains covering indexes as an extension.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment