Repository navigation
Description
Extracted issue from #22902:
I have run into performance issues with this due to our extension that adds soft delete support. Here's what our extension looks like:
const prisma = basePrisma.$extends({ name: "Soft Delete", query: { $allModels: { findUnique: ({ args, query, model }) => { return query({ ...args, where: { deletedAt: null, ...args.where } }); }, findUniqueOrThrow: ({ args, query, model }) => { return query({ ...args, where: { deletedAt: null, ...args.where } }); }, }, }, });Concurrent queries that look like this are handled two different ways depending on whether the extension is enabled:
prisma.myTable.findUniqueOrThrow({ where: { id: "a" } }); prisma.myTable.findUniqueOrThrow({ where: { id: "b" } }); prisma.myTable.findUniqueOrThrow({ where: { id: "c" } }); prisma.myTable.findUniqueOrThrow({ where: { id: "d" } });Without the extension, it becomes:
select * from "myTable" where id in ("a", "b", "c", "d");With the extension, it becomes:
select * from "myTable" where (id = "a" and "isDeleted" is null) or (id = "b" and "isDeleted" is null) or (id = "c" and "isDeleted" is null) or (id = "d" and "isDeleted" is null)Ideally, it would be batched as:
select * from "myTable" where id in ("a", "b", "c", "d") and isDeleted is null;Sometimes upwards of 2-3k of these queries are batched at once, and where the original or ideal query work just fine, the query where every id/isDeleted combination is specified explicitly causes our database to hang.
Originally posted by @dickfickling in #22902 (comment)
Do you know why this is causing a performance problem? Generally the database should be smart enough to use the same indexes in a query where
"isDeleted" is nullis repeated multiple times. And just the additional characters in the query string also should not have any significant impact.If you have an explicit reproduction, that would be amazing. Thanks!
Aside from that, better and more readable queries are of course desirable, which this improvement would lead to.
Originally posted by @janpio in #22902 (comment)
I haven't dug into the issue much, but here's a repo that displays the error on my machine: https://github.com/dickfickling/prisma_perf. To reproduce:
yarn infra,yarn push, andyarn goWith this specific setup the error (
yarn gojust hangs forever) seems to crop up with queries for 5000-7000 records. We've reproduced it in our prod env with as few as 2000 records.
Originally posted by @dickfickling in #22902 (comment)
Hm, that works fine for me:
yarn run v1.22.22 $ dotenv -e .env.dev -- node --loader ts-node/esm src/index.ts prisma:query SELECT "public"."User"."id", "public"."User"."firstName", "public"."User"."lastName", "public"."User"."email", "public"."User"."createdAt", "public"."User"."updatedAt", "public"."User"."deletedAt" FROM "public"."User" WHERE (("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $1) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $2) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $3) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $4) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $5) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $6) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $7) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $8) OR ("public"."User"."deletedAt" IS NUL [...] $9940) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9941) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9942) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9943) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9944) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9945) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9946) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9947) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9948) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9949) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9950) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9951) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9952) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9953) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9954) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9955) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9956) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9957) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9958) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9959) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9960) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9961) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9962) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9963) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9964) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9965) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9966) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9967) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9968) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9969) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9970) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9971) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9972) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9973) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9974) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9975) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9976) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9977) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9978) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9979) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9980) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9981) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9982) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9983) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9984) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9985) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9986) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9987) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9988) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9989) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9990) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9991) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9992) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9993) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9994) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9995) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9996) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9997) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9998) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $9999) OR ("public"."User"."deletedAt" IS NULL AND "public"."User"."id" = $10000)) OFFSET $10001 [ null, { id: 1, firstName: '0', lastName: '0', email: '[email protected]', createdAt: 2024-06-03T14:57:47.460Z, updatedAt: 2024-06-03T14:57:47.460Z, deletedAt: null }, { id: 2, firstName: '1', lastName: '1', email: '[email protected]', createdAt: 2024-06-03T14:57:47.460Z, updatedAt: 2024-06-03T14:57:47.460Z, deletedAt: null }, [...] { id: 98, firstName: '97', lastName: '97', email: '[email protected]', createdAt: 2024-06-03T14:57:47.460Z, updatedAt: 2024-06-03T14:57:47.460Z, deletedAt: null }, { id: 99, firstName: '98', lastName: '98', email: '[email protected]', createdAt: 2024-06-03T14:57:47.460Z, updatedAt: 2024-06-03T14:57:47.460Z, deletedAt: null }, ... 9900 more items ] Done in 12.50s.Should this fail every time, or just sometimes?
Originally posted by @janpio in #22902 (comment)
if you increase from 10k is there a number where the extended prisma with middleware stops working but the base prisma still works?
i think it's something to do with memory or CPU limits in parsing the query. for context i'm running postgres with 2gb of memory and 4vcpus
edit: at 10k it fails every time for me
Originally posted by @dickfickling in #22902 (comment)
Activity
janpio commentedon Jun 4, 2024
I interestingly run into an unrelated bug, for example with
40000:That could be another case of #23957
wellington3110 commentedon Jun 5, 2024
I'm having the same issue
janpio commentedon Jun 5, 2024
What issue is that exactly @wellington3110?
Do you have hanging database queries when doing many
findUnique()in the same tick? If so, can you provide a reproduction?wellington3110 commentedon Jun 7, 2024
@janpio i'm experiencing the same issue that Dick when running the repro in his repo, the "doesn't work" line hangs forever
janpio commentedon Jun 7, 2024
Interesting. So you just follow his reproduction instructions, and then it hangs? Can you maybe share a screenshot? What OS, Node version etc are you using?
dickfickling commentedon Jun 7, 2024
Postgres prisma-test container gets pegged to 100%

I'm on a Mac M1 Max. Node 21.7.1. Postgres 14.6
isaacpz commentedon Jun 17, 2024
We're seeing the same problem on our Prisma project. When a query uses client extensions, it appears that no query optimizations work and Prisma reverts to the N+1 behavior it claims to avoid. This is a pretty major performance issue with client extensions and causes very noticeable performance impact.
If helpful, it seems like this issue is not a new regression. We've tested on both 5.15 and 4.14 and are seeing the same performance issue on both versions.