Duplicate joins & offsets in count expression
Hi! I'm doing some complex count operations and the query builder is making some decisions that I do not understand.
Essentially, I'm doing this query:
Very complex query, so I'd expect the count op to be complex, but I feel its a little un-optimized.
This generates the following SQL (flag1 = false, so it's filtering based on
foreignTable1
):5 Replies
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into
#ask-ai
for a quick spin!
Do I have to manually optimize these queries? Or am i doing something wrong in my prisma calls?
I'm sorry I have obfuscated this. Private code base and all, but I hope it can be followed!
I am using relationship joins. Does this impact how count queries are performed?
It seems that some of your questions are related to discussion in this GitHub Issue:
https://github.com/prisma/prisma/issues/23891
Can you take a look?
GitHub
nested count, aggregate _count query inefficiency · Issue #23891 ...
Bug description When prisma builds count queries or aggregates for "_count" values why does it use IN to get values after joining when counting directly from the joined table would be muc...
You can also use TypedSQL to create highly optimised queries:
https://www.prisma.io/docs/orm/prisma-client/using-raw-sql/typedsql
Writing Type-safe SQL with TypedSQL and Prisma Client | Prisma Docu...
Learn how to use TypedSQL to write fully type-safe SQL queries that are compatible with any SQL console and Prisma Client.
Thank you! I'm going to look into seeing if not using relation joins fixes the perf issues im seeing thank you!