Skip to content
Preptima
hardConceptDesignMidSeniorStaff

Three queries hit the same table with different filters. What composite indexes do you build, and how do you order the columns?

6 min readUpdated 2026-08-15

Answer

Short answer

Order by how the predicate uses the column, not by cardinality: equality columns first, then one range column, then columns needed only for sorting.A range predicate stops the index seeking on everything after it, and the leftmost prefix decides which other queries the index can serve.

Was this answer helpful?

Likely follow-ups

  • Which of your indexes is now redundant, and how would you prove it before dropping it?
  • When would you use a partial index instead of adding a column to an existing one?
  • How does an InnoDB secondary index differ from a PostgreSQL one in what it stores?
  • The query filters on a low-cardinality flag where 2% of rows are true. What is the best index?

Related questions

Further reading

Related Topics