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
Unexpected query leading to querying full table when using batched findUnique()
#23343
Comments
findUnique()
in combination with extending the prisma client
Do I understand correctly that running a single |
Correct, it becomes too broad. The SQL query will return all posts of that specific tenant. The result that prisma returns is still correct, but my guess is that's because you filter under the hood. |
Could you maybe try to run the similar complete queries manually in parallel, instead of modifying them via client extensions? That would let us isolate if this is a Client extension problem, or a general batching problem for the type of query you are constructing via the extension. |
No problem @janpio , running the code like this (assuming this is what you meant): const queryPost = (tenantId: string, userId: number) =>
prisma.post.findUnique({
where: {
tenantId_userId: {
tenantId,
userId,
},
tenantId,
},
});
const concurrentPosts = await Promise.all([queryPost("tenant1", 1), queryPost("tenant2", 3)]); Results in the SQL query: SELECT
"public"."Post"."tenantId",
"public"."Post"."userId",
"public"."Post"."id",
"public"."Post"."text"
FROM "public"."Post"
WHERE (
("public"."Post"."tenantId" = $1 AND "public"."Post"."userId" = $2)
OR "public"."Post"."tenantId" = $3
OR ("public"."Post"."tenantId" = $4 AND "public"."Post"."userId" = $5)
OR "public"."Post"."tenantId" = $6
) OFFSET $7 So it looks like it does not have anything to do with extending the model, but with findUnique batching. |
That was exactly what I was hoping for, so this is isolated to "just" batching and we can figure out why this is happening. Thanks! |
findUnique()
in combination with extending the prisma clientfindUnique()
@janpio Just curious if there was any updates on this? |
No, this is now waiting to be picked up by someone from the engineering team for a full reproduction attempt. |
Hey @rgmvisser, Thanks for highlighting this issue! WHERE (
("public"."Post"."tenantId" = $1 AND "public"."Post"."userId" = $2)
OR "public"."Post"."tenantId" = $3
OR ("public"."Post"."tenantId" = $4 AND "public"."Post"."userId" = $5)
OR "public"."Post"."tenantId" = $6
) This was reproducible even without the second query as it was caused by the appearance of a field twice: once by itself, and once through the compound key. Our logic was set-up where if we encounter a key twice, we treated that as a new query, hence the many WHERE (
("public"."Post"."tenantId" = $1 AND "public"."Post"."userId" = $2)
OR "public"."Post"."tenantId" = $3
) Where really that Thanks again! |
Thanks for the update! :) Is there a timeline for when this will be resolved? |
You can track progress in the linked PR :) |
Bug description
This bug occurs when running multiple findUnique queries when extending the prisma client.
Resulting query:
Because we are dynamically adding the
tenantId
to every query, this results in a query where all posts of the tenant are being queried (seeOR "public"."Post"."tenantId" = $3 OR
). It does return the correct results, but it causes a lot of load as the database will return all Posts for a tenant.This was super hard to debug, but luckily found this article eventually that lead me in this direction: https://www.prisma.io/docs/orm/prisma-client/queries/query-optimization-performance
At the moment, I have changed all queries to "findFirst", but this is a bit of a footgun as it results in very unexpected behavior.
How to reproduce
I have made a repo with a reproduction here: https://github.com/rgmvisser/prisma-bug-reproduction
Expected behavior
That findUnique also takes into account the added "where" clause of the tenant inside the OR instead of outside. So instead of:
A query like:
Prisma information
Schema
Code
Environment & setup
Prisma Version
The text was updated successfully, but these errors were encountered: