Composite indexes: the order matters more than you think
Omar Jensen
·161 views
i recently spent a frustrating week debugging a set of mysteriously slow queries in our system. it turned out to be a classic mistake: the order of columns in a composite index. i had an index on `(user_id, status)` but our most common query was filtering on `status` first and then `user_id`, or sometimes just `status`.
this is a pretty fundamental database concept, but it's easy to overlook when you're moving fast. the order of columns in a composite index matters significantly because the database can only use the index efficiently if the leading columns of the index are used in the query's `WHERE` clause. for example, `INDEX (A, B)` helps `WHERE A = x AND B = y` and `WHERE A = x`, but not `WHERE B = y`. what are some other common, subtle indexing mistakes folks have encountered that caused major performance headaches?
0 comments