Exercise 3: Measuring the N+1 Problem — Possible Solution =========================================================== Turn on logging in lib/prisma.ts: export const prisma = new PrismaClient({ adapter, log: ["query"] }); Create posts (for example with createMany or the Chapter 8 nested writes), then run each version. Slow loop (findMany, then findUnique for each post, awaited one by one), with 10 posts: 1 query to list the posts + 10 queries, one for each post's author = 11 queries Fast version (include: { author: ... } in the findMany): about 2 queries: one for the posts and one for the authors of all of them (or a single joined query if the relationJoins preview feature is enabled on a supported database). With 1,000 posts: Slow loop: 1 + 1,000 = 1,001 queries Fast version: still about 2 The slow version grows with every row; the fast one stays constant. If each query costs a few milliseconds of network time, that's the difference between a few milliseconds and several seconds. A detail: Prisma can automatically combine several findUnique calls made at the same moment (for example inside Promise.all), but calls awaited one after another in a loop, as here, are each sent separately — so the loop really does make 1,001 round trips. (The exact number of queries the fast version logs can vary with Prisma version and database.) WHY THIS WORKS AS AN ANSWER ------------------------------ It measures rather than assumes, and shows the key point: N+1 grows linearly with the data, while loading the relation with the list keeps the number of queries fixed.