SQL · Queries that survive scale
What is index selectivity and why does it matter?
Short answer
Selectivity measures how many distinct values a column has relative to row count. Highly selective columns are better index candidates because a lookup returns a small fraction of the table.
An index is most useful when it eliminates most rows. If a predicate matches 40 percent of a table, reading the index plus many table pages can cost more than a sequential scan. The planner uses statistics to estimate this before choosing a plan.
SELECT
COUNT(*) AS total_rows,
COUNT(DISTINCT status) AS distinct_status,
COUNT(DISTINCT user_id) AS distinct_users
FROM orders;How to use the idea
- High selectivity: email, uuid, user_id, or a narrow date range
- Low selectivity: boolean flags, status with few values, or similar enums
- A low-selectivity column can work in a partial index that targets a rare value
- Composite indexes can combine a low-selectivity equality column with a selective one
- Stale statistics produce bad estimates, so run ANALYZE after large data changes
- Histograms and extended statistics help when columns are correlated
CREATE INDEX idx_orders_pending
ON orders (created_at)
WHERE status = 'pending';Selectivity is not only about distinct values. The distribution matters: a column can have many distinct values but still be dominated by one value. The planner's histogram captures that skew, which is why fresh statistics are important.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How do database statistics influence index choice?
- 2When is a partial index better than a full index?
- 3Why can a low-selectivity column still be useful in a composite index?
- 4What makes a predicate non-sargable?
Say it in the interview
Selectivity is the fraction of rows a predicate returns, or how many distinct values a column has relative to the row count. Highly selective predicates benefit most from an index because they eliminate most of the table. Low-selectivity columns often cause the planner to choose a sequential scan. I use partial indexes for rare values, keep statistics fresh, and verify with real row counts rather than guessing.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live SQL mock interview and get scored feedback on depth, clarity and confidence.