What is a composite index, what is the main principle behind how it works, and how do you choose which fields to include?
Asked at
AI answer
A composite index is an index on multiple columns, for example, INDEX (status, created_at). The order of the columns matters: a DBMS can usually use this index efficiently for queries on its leftmost prefix (status or status, created_at), but not for queries on created_at alone. Therefore, choose columns based on actual WHERE conditions, joins, and sorts; columns checked for equality are often placed before a range column. Consider selectivity, query frequency, and the cost of the index: it takes up space and slows down inserts and updates. Verify the decision using the query execution plan.
Also asked at SkyCoach
Similar questions
- What do you know about indexes? How can you determine which indexes are used in a query, and how can you tell which indexes are unnecessary or missing?
- What types of indexes in MySQL have you worked with?
- What is a B-tree?
- What are the main principles of creating and using composite indexes for query optimization?
- What is a transaction, and what are transactions used for?