Database-side relation loading
Use join when the database should assemble nested relation data and return it through one query.
Prisma · Pipe
relationLoadStrategy selects how Prisma loads related records for a relation query. Use join for a database-level single-query strategy or query for multiple queries merged in application code.
Add the relationJoins preview feature, regenerate Prisma Client, then set relationLoadStrategy at the top level of a query that uses include or relation select. Choose join for database-side aggregation in one query, or query for multiple table queries merged by the application.
relationLoadStrategy: "join" | "query"| Name | Type | Description |
|---|---|---|
relationLoadStrategy | "join" | "query" | Controls relation loading; accepted values are join or query. |
The selected relation query result; the loading strategy changes execution rather than the returned data shape.
generator client {
provider = "prisma-client-js"
previewFeatures = ["relationJoins"]
}
const users = await prisma.user.findMany({
relationLoadStrategy: "join",
include: { posts: true },
});Loads users and their posts with one database query using the join strategy.
Use join when the database should assemble nested relation data and return it through one query.
Use query when database-server resource usage should be reduced or profiling favors application-level merging.
Choose the strategy separately for each root query that loads relations.
relationLoadStrategy controls how Prisma loads related records. join is the default and uses a database-level join or correlated subqueries to return the complete result in one database query; query sends separate table queries and merges the results in application code.
Follow-up: When might query be preferable? Use it when reducing database-server resource usage matters or profiling shows that application-level merging performs better.
The option belongs at the top level of a query that supports include or relation selection. A top-level setting governs all nested subqueries, so nested relations cannot independently choose another strategy.
Follow-up: How would you configure the feature? Add relationJoins to the generator's previewFeatures and regenerate Prisma Client.
The documented relationJoins feature is available for PostgreSQL, CockroachDB, and MySQL. With join, PostgreSQL uses a LATERAL JOIN, MySQL uses correlated subqueries, and the database performs JSON aggregation.
Follow-up: What is the operational distinction of query? It performs multiple queries, one per table, then joins the records in the application layer.
includeselectrelationJoinsprisma generateRelation queries