Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
To stop an AI agent from generating Prisma N+1 queries, give it a project rule that requires it to check where database calls occur, batch relation reads when possible, and verify the resulting query behavior. A rule can guide Cursor or Claude Code, but it cannot guarantee correct or faster code. The core fix is usually to replace a per-item query loop with a nested read, a grouped lookup, or a supported relation-loading strategy.
Why is Prisma running one query per item?
An N+1 pattern starts with one query that fetches a collection, then performs another query for each returned item. Prisma’s v7 query optimization guide defines it as “looping through query results and performing one additional query per result.” Prisma’s query optimization guide shows how this can happen in ordinary application code as well as GraphQL resolvers.
For example, this loads users and then fetches each user’s posts separately:
const users = await prisma.user.findMany();
for (const user of users) {
const posts = await prisma.post.findMany({
where: { authorId: user.id },
});
}
The first call fetches the users; the loop adds one posts query per user. As the result set grows, so does the number of database calls.
#1 Best Overall
How do I fix N+1 queries in Prisma?
Choose the fix that matches the result your application needs. A nested result, a flat collection of related records, and relation data fetched for separate GraphQL resolvers are not the same query shape.
Use a nested read when the response needs parents and their related records
Prisma’s documented nested-read pattern uses include to fetch users and their posts together at the Prisma API level:
const users = await prisma.user.findMany({
include: { posts: true },
});
In Prisma’s documented example, this results in two SQL queries rather than one query for every user. Select only the fields the response needs instead of retrieving every available field.
Use a grouped in query when you can associate results in code
If you need the related records as a separate collection, fetch the parent IDs and use one grouped lookup rather than calling findMany() inside the loop:
Rank #3
const users = await prisma.user.findMany({
select: { id: true },
});
const posts = await prisma.post.findMany({
where: { authorId: { in: users.map((user) => user.id) } },
});
You can then associate the posts with their users in application code. This changes the result-handling work, so confirm it still produces the shape and behavior your caller expects.
Consider a relation join when your version and provider support it
Prisma documents relationLoadStrategy: "join" as a database-side relation-loading strategy. Its "query" strategy issues separate queries and merges the results in the application. Availability, provider support, and preview configuration depend on the installed Prisma version. Check the relation query documentation and Prisma Client reference before adding this option; do not assume code using it works across every project.
Use automatic batching only for qualifying findUnique() calls
Prisma documents automatic batching for qualifying findUnique() calls made in the same tick, subject to conditions on the filters. This is useful in some resolver patterns, but it is not a general promise that arbitrary Prisma queries—or findMany() calls in a loop—will be batched. Check the conditions in the query optimization guide.
What should a Cursor rule or Claude Code skill tell the agent?
Give the agent an explicit review standard for collection reads, rather than telling it only to “avoid N+1.” A practical instruction should ask it to:
- Inspect whether a database call sits inside a loop, resolver, or callback that runs once per returned record.
- When related data is needed for a collection, consider a nested read, a grouped
inlookup, or a suitable relation-loading strategy. - Preserve the requested result shape and fetch only the fields the application needs.
- Check the project’s Prisma version and database provider before using version-sensitive relation options.
- Validate the generated query behavior; do not claim a performance improvement without evidence from the relevant workload.
Prisma publishes Cursor guidance that includes a project-rule example. It also documents a CLI for synchronizing skills shipped in Prisma packages with agent harnesses, including Claude Code and Cursor: Prisma skills documentation. These sources establish that project rules and package-shipped skills are legitimate ways to guide agents. They do not establish the exact current Claude Code path or installation steps for the particular skill named in this article, so check the skill’s own current instructions rather than assuming a path or command.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.How can I confirm the agent stopped the N+1 pattern?
Review query count and query shape for the operation that loads the collection. A high number of queries for one request can indicate N+1, but query count alone does not establish which alternative is fastest: data volume, database workload, and application-side processing matter too.
Prisma’s query optimization guide describes client-level query events for inspecting generated queries and execution times. Its Query Insights guidance describes investigating high query counts and annotating Prisma operations so SQL can be traced to the originating call. Use those diagnostics to check whether calls still scale with the number of parent records and to compare the actual behavior of a change.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

