question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`findUnique` batching performance

See original GitHub issue

Bug description

I want to retrieve 10,000 accounts (by IDs) from the database.

Due to the nature of how GraphQL resolvers work, I can’t simply use findMany something like

prisma.account.findMany({
  where: { id: { in: [1, 2, 3 /* ... */ } }
})

Instead, I use the findUnique inside of the resolver which gets called 10,000 times:

// inside a GraphQL resolver
prisma.account.findUnique({
  where: { id: account.id }
})

As per https://www.prisma.io/docs/guides/prisma-guides/query-optimization-performance#solving-n1-in-graphql-with-findunique-and-prismas-dataloader, it shouldn’t really introduce a performance bottleneck as the findUnique calls which happen in the same tick should be batched.

From my understanding, it should be almost equivalent to calling the findMany method directly.

Interestingly, if I use the dataloader library in order to create my own batch function (in which I call the findMany method), the performance is almost the same as calling the findMany method directly:

// create a data loader
const accountByIdLoader = new DataLoader<number, Account>((ids) =>
  prisma.account.findMany({
    where: {
      id: { in: ids as number[] },
    },
  })
);

// inside a resolver
accountByIdLoader.load(account.id)

How to reproduce

Full reproduction available on https://github.com/tomdohnal/prisma-dataloader-perf

Running on my machine gives this performance metrics: Screenshot 2021-04-14 at 17 07 54

Expected behavior

I’d expect the performance of using dataloader library and calling the findUnique method to be similar

Prisma information

Available on https://github.com/tomdohnal/prisma-dataloader-perf

Environment & setup

  • OS: Mac OS
  • Database: sqlite, postgres
  • Node.js version: 12.4
  • Prisma version: 2.21.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
sjerratschcommented, Oct 5, 2021

Any updates on this? Have there been any releases addressing this issue?

0reactions
carloslibardocommented, Dec 2, 2022

I’m having the same issue, any updates?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query optimization - Prisma
This guide describes ways to optimize query performance, ... Automatic batching of findUnique is particularly useful in a GraphQL context.
Read more >
DataLoaders not combining findUnique queries that can be ...
I think the behaviour is expected. Dataloader is batching the first two queries and using IN and perform two queries for the third...
Read more >
GraphQL N+1 issues are a thing of the past. Huh, care to ...
This isn't true. Prisma currently only supports batching of findUnique queries: https://www.prisma.io/docs/guides/performance-and-optimizati..
Read more >
Handling relational databases with NestJS, Prisma and ...
Just like performance and reliability, security is a requirement to ... findUnique({ where: { id } }); // This code is related to...
Read more >
how do i speed up mysql updates other than looping
(batching the inserts into however many statements you can fit in however ... Alternatively, find unique values and update them together:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found