The N+1 query that only appeared in production
Fast on my machine, 4 seconds for users. The fix took one line; finding it took most of a day.
The endpoint returned in 80ms locally and somewhere north of four seconds in production. Same code, same query plan, wildly different experience — which is usually a sign that the difference is data, not logic.
Seed data lies to you
My local database had twelve orders. Production had a customer with nine hundred. The list endpoint loaded orders, then loaded each order's items one query at a time. Twelve extra queries is invisible. Nine hundred is a support ticket.
ts
// before: one query per order
const orders = await db.orders.findMany({ where: { customerId } });
for (const o of orders) {
o.items = await db.items.findMany({ where: { orderId: o.id } });
}
// after: two queries, total
const orders = await db.orders.findMany({
where: { customerId },
include: { items: true },
});The lasting fix wasn't the include. It was logging the query count per request in development and failing loudly above a threshold, so the next one shows up before a user finds it.