medium10 pts · first answer countsdatabasesperformance
The N+1 query problem
A page lists 50 posts with each author name. Logs show 51 database queries for one page load. What is the best fix?
const posts = await db.post.findMany({ take: 50 })
for (const p of posts) {
p.author = await db.user.findUnique({ where: { id: p.authorId } })
}