`findUnique` batching performance
See original GitHub issueBug 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:
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:
- Created 2 years ago
- Reactions:3
- Comments:6 (2 by maintainers)
Top GitHub Comments
Any updates on this? Have there been any releases addressing this issue?
I’m having the same issue, any updates?