IK
Interview Knowledge Book
Topics
Practice
Examples
Playground
RU
browser
N+1 query batching
Compare the number of lookups made by per-row fetching and a batched lookup.
index.js
const posts = [{ authorId: 1 }, { authorId: 2 }, { authorId: 1 }]; const perRowQueries = 1 + posts.length; const uniqueAuthorIds = new Set(posts.map((post) => post.authorId)); const batchedQueries = 1 + 1; console.log('n+1 queries', perRowQueries); console.log('batched queries', batchedQueries); console.log('authors fetched', uniqueAuthorIds.size);
Run
Stop
Reset