When to Add a Database Index (and Its Cost)
When to Add a Database Index (and Its Cost)
MediumBackend0% accepted
A query filtering by user_id and ordering by created_at does a full table scan. Explain when to add an index, what a composite index on (user_id, created_at) buys you, and the cost of indexes. Cover: how a B-tree index speeds lookups, column order in composite indexes, why too many indexes hurt writes, and covering indexes.
Approach
Index columns used in WHERE/JOIN/ORDER BY. A composite (user_id, created_at) serves filter+sort in one. Indexes speed reads but slow writes and use space — don't index everything.
More Backend problems
Browse all coding problems →Scenarios & Expected Answers
No example scenarios for this problem.
Sign in to continue
Create a free account to view answers and mark progress.
- Free forever — no credit card
- Theory Q&A with detailed answers
- Points, streaks & leaderboards