Suppose you are designing a task-management application where users create tasks, update their status, and retrieve their recent tasks.
The database contains millions of tasks, and you have added several indexes to speed up filtering and sorting. Reads have improved, but creating and updating tasks now takes longer.
The challenge is to identify which indexes provide meaningful benefits, determine which can be removed or redesigned, and restore write performance without making frequently used queries unacceptably slow.
Identify the queries that need optimization
Choose an index for the complete query pattern, including its filtering, ordering and result size.
Start by examining the queries the application executes rather than adding indexes based on individual columns.
Suppose users frequently retrieve their most recent tasks:
As the table grows, the database may need to examine many records belonging to the user and sort them by creation time. An index on user_id can help locate matching tasks, but it may not eliminate the sorting work.
A composite index on (user_id, created_at) may support both filtering and ordering, allowing the database to retrieve the most recent tasks more efficiently.
With this ordered composite index, entries for the same user are grouped together, and timestamps are ordered within that user's group.