SQL & Relational Databases (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What is a Composite Index and the Leftmost Prefix Rule?
senior LevelMost AskedSystem Design
In Simple Words:
An index defined on multiple columns (e.g. INDEX (country, city, postal_code)):
- The data is sorted by
countryfirst, then within each country bycity, then bypostal_code. - Leftmost Prefix Rule: The query optimizer can only use the index if the query filters on the leftmost columns in order:
WHERE country = 'US'-> Index used!WHERE country = 'US' AND city = 'Chicago'-> Full index used!WHERE city = 'Chicago'-> Index CANNOT be used! (violates leftmost prefix).