Fix a slow endpoint (N+1 queries)
Medium · Coding (typed)
GET /orders returns the latest 50 orders with each customer's name. It takes 2 seconds. The code runs SELECT * FROM orders ORDER BY created_at DESC LIMIT 50, then, for each order, SELECT name FROM customers WHERE id = ?. Fix it and say which index you would add.
Create a free account to answer it and get scored on what a strong answer covers.