-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
findUnique()
batching should deduplicate equality checks when where
clause parameters are the same
#22902
Comments
This sounds like a nice improvement, thank you for the suggestion. Have you observed any real performance issues because of duplication (e.g. increased latency, optimiser not being able to collapse them on the database side, etc) or is it more of a thing that just came up when you saw the query? |
findUnique
batching should deduplicate equality checks when where clause parameters are the same
I don’t have exact metrics at the moment (but can pull them if it helps). I have found some instances where the optimizer does not correctly collapse the clauses, but that is a rare occurrence. In our use case we can see this number of clauses in the |
findUnique
batching should deduplicate equality checks when where clause parameters are the samefindUnique()
batching should deduplicate equality checks when where clause parameters are the same
@aqrln I have run into performance issues with this due to our extension that adds soft delete support. Here's what our extension looks like:
Concurrent queries that look like this are handled two different ways depending on whether the extension is enabled:
Without the extension, it becomes:
With the extension, it becomes:
Ideally, it would be batched as:
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. |
Hey piggybacking on this. Running into the same issue that ends up throwing an error because we're exceeding the number of bound parameters allowed by our driver. We would not be running into this issue just yet if Prisma was a little more efficient in the batch query generated. Also using this thread to suggest limiting the batch size to prevent parameter binding limits being hit. |
@chris-basis Please make sure to also open a bug report issue if you are running into a problem/crash because of this. We need that information to fully understand the situation. Thanks. |
#23957 👍 |
findUnique()
batching should deduplicate equality checks when where clause parameters are the samefindUnique()
batching should deduplicate equality checks when where
clause parameters are the same
@dickfickling 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 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. |
@janpio 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: With this specific setup the error ( |
@dickfickling Hm, that works fine for me:
Should this fail every time, or just sometimes? |
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 |
I split out the discussion with @dickfickling about the database hanging into its own issue: #24419 (I will hide the comments above some time in the future so clean up this issue thread.) |
Problem
When
findUnique
queries are issued with the samewhere
andinclude
parameters within the same tick, they are batched into a singlefindUnique
with each of the original parametersOR
'd together. Resulting in an output from the prisma query log like this:However, in cases that the
where
clause values are the same across queries, this could be flattened to reduce the number of equality checks like so:where $1 === $3 and $2 === $4
would become
Suggested solution
Deduplicate these queries to reduce the size of the where clause generated
Alternatives
None
Additional context
This is especially useful when using graphQL and trying to reduce the overhead associated with the N+1 problem.
The text was updated successfully, but these errors were encountered: