As asked
You have a Next.js page that lists 50 blog posts and each post shows its author name. You are using Prisma and your server logs show 51 queries on every page load. Walk me through exactly what is happening and how you fix it without switching ORMs.
Sample answer outline
The N+1 happens because the code fetches posts in one query and then, inside a loop, fires a separate findUnique for each post's author. The fix is to add an include or select on the initial findMany so Prisma does a JOIN or a batched second query and assembles the result in memory. A strong answer also mentions Prisma's fluent relation queries, the difference between include and select for shaping the payload, and enabling Prisma query logging or using prisma.$on to surface slow queries in development.
Expect these follow-ups
- How would you verify the fix actually reduced query count in production without adding a full APM tool?