Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: N + 1 Query issues #1

Closed
MikeAliG opened this issue Apr 30, 2020 · 8 comments
Closed

Question: N + 1 Query issues #1

MikeAliG opened this issue Apr 30, 2020 · 8 comments
Labels
question Further information is requested

Comments

@MikeAliG
Copy link

Hello,

This is a great package and thanks for creating this awesome package, now my question is that, does this package handle N + 1 Query issues?

Thanks a lot in advance.
Ali

@AhmedElywa
Copy link
Collaborator

AhmedElywa commented Apr 30, 2020

The main thing we create this package is facing N + 1 query issue in my private project
Idea here is convert info: GraphQLResolveInfo that has query fields come from frontend to be available prisma select object

Example query

query {
  findOneUser(where: { id: 1 }) {
    id
    email
    name
    posts(
      where: { title: { contains: "a" } }
      orderBy: { createdAt: asc }
      first: 10
      skip: 5
    ) {
      id
      title
      comments(where: { contain: { contains: "a" } }) {
        id
        contain
      }
    }
  }
}

convert to

{
  select: {
    id: true,
    email: true,
    name: true,
    posts: {
      select: {
        id: true,
        title: true,
        comments: {
          select: { id: true, contain: true },
          where: { contain: { contains: 'a' } }
        }
      },
      where: { title: { contains: 'a' } },
      orderBy: { createdAt: 'asc' },
      first: 10,
      skip: 5
    }
  }

with this way you just query what the frontend ask and save your performance
and don't do a lot of findOne for every relation result

old way

In this query example if we have result with 10 users so i was doing 10 queries prisma.post.findMany and for every post result i will do prisma.comment.findMany so this arrive with me in some cases to 850 query in one request take 40 second

my way

convert info to be available prisma select object you will do one query to get all what you need the query was take 40 second before now take less than 100ms

I hope i made it clear

@MikeAliG
Copy link
Author

Hello,

Very interesting! Thanks a lot for clearing that up. Is it possible to give an example for converting info to be available prisma select object (your suggested way)?

Thanks a lot in advance!

@AhmedElywa
Copy link
Collaborator

AhmedElywa commented Apr 30, 2020

It's already in my tools https://github.com/AhmedElywa/prisma-tools/tree/master/packages/select
you can download my examples and try it

@AhmedElywa
Copy link
Collaborator

@AliTheAli you can use this one https://github.com/AhmedElywa/prisma-tools/tree/master/examples/apollo-nexus-schema

@MikeAliG
Copy link
Author

MikeAliG commented May 1, 2020

This is great! Thanks a lot for all your amazing work!

@MikeAliG MikeAliG closed this as completed May 1, 2020
@AhmedElywa AhmedElywa pinned this issue May 1, 2020
@AhmedElywa AhmedElywa added the question Further information is requested label Sep 30, 2020
@nghiepdev
Copy link

I just wanted to say thank you for creating the Prisma Select!

@AhmedElywa
Copy link
Collaborator

@nghiepit Thank you for your positive feedback

@ielireb
Copy link

ielireb commented Jan 17, 2022

I have trouble with @@id. its giving me error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants