Skip to content

Prisma not combining queries for GraphQL server, leading to N+1 issues #5274

Closed

Description

@dickfickling

Bug description

I've created a small server running Prisma, Apollo, and type-graphql with a few entities. A simple fetch that traverses multiple edges on the graph causes N+1 db queries when I'd have expected Prisma to combine them.

How to reproduce

Check out https://github.com/dickfickling/prisma-n1 for a repro. I'm not sure whether the culprit is within prisma, type-graphql, typegraphql-prisma, or somewhere in the way i'm structuring my code. Regardless if you install deps and run npm run test in this example, you'll see the test creates 10 users with 10 posts each, and then fetches the feed, which should return all 100 posts and their attached users. The test passes, but you see a massive number of queries logged.

There's a github action workflow that demonstrates this as well.

Expected behavior

I'd expect a constant number of db queries to run

Prisma information

prisma 2

Environment & setup

  • OS: debian/ubuntu
  • Database: mysql
  • Node.js version: reproduced on 14 and 15
  • Prisma version: 2.15.0

Activity

  1. sahildeliwala commented on Jan 30, 2021

    @sahildeliwala

    I'm facing the same issue with same environment setup. Prisma's findUnique just runs queries individually with SELECT .... WHERE user.id = 2 instead of batching them like SELECT... WHERE user.id in [1,2,3,4]

  2. self-assigned this
    on Feb 2, 2021
  3. timsuchanek commented on Feb 2, 2021

    @timsuchanek
    Contributor

    Thanks for reporting! We'll have a look.
    Related: prisma/prisma-examples#2464

  4. added this to the 2.17.0 milestone on Feb 3, 2021
  5. added
    bug/2-confirmed
    kind/bug
    on Feb 8, 2021
  6. janpio commented on Feb 8, 2021

    @janpio
    Contributor

    2 more cases were reported in #5485 - one was explicitly confirmed to be the same problem as described above.

  7. timsuchanek commented on Feb 10, 2021

    @timsuchanek
    Contributor

    Thanks everyone for reporting. We just implemented a fix for this. It's available in @prisma/[email protected].
    Please let us know if that fixes it for you!
    @dickfickling in your project I also needed to add the prisma for the typegraphql generator.

  8. dickfickling commented on Feb 10, 2021

    @dickfickling
    Author

    it works! thanks @timsuchanek

  9. timsuchanek commented on Feb 10, 2021

    @timsuchanek
    Contributor

    Awesome @dickfickling! Thanks again for the reproduction and also for getting back to me!
    That's not a given.

  10. janpio commented on Feb 12, 2021

    @janpio
    Contributor

    Can confirm that the reproduction from #5485 is also back to normal performance with 2.17.0-dev.67 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug/2-confirmedkind/bugtopic: performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions