What is a composite (multi-column) index, and can you explain the leftmost prefix rule and why column order matters?
Last updated
A composite index covers multiple columns sorted hierarchically: an index on
(a, b, c)sorts bya, thenbwithina, thenc, like a phone book.Leftmost prefix rule: it supports seeks on
a,(a, b), or(a, b, c), but not onborcalone, and skipping a column breaks use of later ones.Order matters: lead with the most frequently filtered, selective column. A range predicate on an early column stops later columns from being used for seeking.
- https://dev.mysql.com/doc/refman/8.2/en/multiple-column-indexes.html
- https://dev.mysql.com/doc/refman/9.7/en/mysql-indexes.html
- https://www.postgresql.org/docs/current/indexes-multicolumn.html
- https://planetscale.com/blog/why-isnt-mysql-using-my-index
- https://medium.com/@harsh.vaghela.work/why-mysql-sometimes-hates-your-perfect-index-and-how-to-make-it-love-you-back-75e52509ddd0
- https://www.skillsdirectory.com/skills/intense-visions-db-composite-index
- https://ask.use-the-index-luke.com/questions/161/when-index-skip-scan-is-used