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])
}Discussion 0
Join free to post in the discussion.
Related questions
- Stopping SQL injectionBackend Developer
- Purpose of a database indexBackend Developer
- Rows returned by a LEFT JOINBackend Developer
- What a foreign key enforcesBackend Developer
- Atomicity in a money transferBackend Developer
- COUNT and AVG with NULLsBackend Developer
- Filtering grouped resultsBackend Developer
- Slow email during signupBackend Developer