Skip to content
RED SAGSkills
medium10 pts · first answer countssqlperformanceorm

The N+1 query problem

This endpoint returns 100 posts with their authors. How many database queries does it run, and what is the usual fix?

const posts = await db.query("SELECT * FROM posts LIMIT 100")
for (const p of posts) {
  p.author = await db.query("SELECT * FROM users WHERE id = $1", [p.author_id])
}
Choose one answer