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 } })
}Discussion 0
Join free to post in the discussion.
Related questions
- Measuring speed in dev modeFull Stack Developer
- Offset vs cursor paginationFull Stack Developer
- Renaming a column safelyFull Stack Developer
- Caching hashed static filesFull Stack Developer
- Three awaits, three times slowerFull Stack Developer
- ETag and If-None-MatchFull Stack Developer
- One report slows every requestFull Stack Developer
- Fewer manual memosFull Stack Developer