Choose a Composite Index for the Your Orders Lookup Query
Last updated: November 29, 2026
Quick Overview
A query that loads a customer's recent shipped orders, newest first, takes milliseconds and scans the whole table. Design the right composite index, explain the column order, prove it with the query plan, and show what a wrong column order does.
Amazon
November 29, 20260
0
0 solved
A query that loads a customer's recent shipped orders, newest first, takes milliseconds and scans the whole table. Design the right composite index, explain the column order, prove it with the query plan, and show what a wrong column order does.
Order history and seller reporting queries run against large relational tables, and index design is a staple of Amazon database fundamentals interviews. The interviewer checks the equality, then range or sort column rule, covering indexes, and reading a query plan instead of guessing.
What the Interviewer Expects
- Puts equality predicates first, then the range and ORDER BY column
- Explains how the index also removes the sort step
- Reads EXPLAIN output to confirm a seek instead of a scan
- Explains covering indexes and when they are worth it
- Shows why a range column first defeats the index
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- What is the write cost of each additional index on a hot orders table?
- Would you index status if only 1 percent of orders are not shipped?
- How would this design change in DynamoDB with a partition and sort key?
- How do you find unused indexes in production?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Query filters
customer_id = ?,status = 'SHIPPED',placed_at >= ?, orders byplaced_at DESC, limit 20. - 200,000 rows in the demo, far more in production.
- Same result rows before and after...
High-Level Design
A B-tree index sorts by its columns left to right. With (customer_id, status, placed_at) the database seeks directly to the block of rows for one customer and status, which is already sorted by time...