Automatically Solving the N+1 Problem with FindUnique
#904
-
Firstly, I'd like to express my appreciation for all the work you've put into this library 🎉 The Prisma TypeScript client automatically addresses the N+1 problem when we use the Does the prisma-client-go offer a similar solution to the N+1 problem like the TypeScript client does? I attempted to implement the following code, but it resulted in an N+1 problem: func (r *userResolver) Posts(ctx context.Context, obj *model.User) ([]*model.Post, error) {
u, err := r.db.User.FindUnique(db.User.ID.Equals(obj.ID)).With(db.User.Posts.Fetch()).Exec(ctx) (Repro is here https://github.com/KoichiKiyokawa/prisma-client-go-nplusone-repro) Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Thanks! The Go client currently does not do any batching at this time. I will have to think about this. I'm wondering if this is actually a good solution as it doesn't really solve the n+1 problem universally, it just solves it for one use case (when you do multiple findUnique) – please correct me if that's wrong though. For now, I would recommend doing For example, if you are able to fetch For example: func (r *Viewer) User(ctx context.Context, obj *models.Viewer, id string) (*db.UserModel, error) {
return r.db.User.FindFirst(
db.User.ID.Equals(id),
).With(
db.User.Posts.Fetch().With(
db.Post.Author.Fetch(),
),
).Exec(ctx)
} Of course this would only solve it for that query and not universally, but I'm not sure what the best solution is here |
Beta Was this translation helpful? Give feedback.
Thanks!
The Go client currently does not do any batching at this time. I will have to think about this. I'm wondering if this is actually a good solution as it doesn't really solve the n+1 problem universally, it just solves it for one use case (when you do multiple findUnique) – please correct me if that's wrong though.
For now, I would recommend doing
.Fetch()
for whatever data you most like need in the parent resolver.For example, if you are able to fetch
user -> user's posts -> post's comments
, you would run into the n+1 problem.For example: