Performance: How to make prisma perform a single SQL query with joins instead of multiple queries #12715
Replies: 3 comments 57 replies
-
All these queries are executed with one and the same connection. There is no way currently to influence the types of queries Prisma executes besides using raw queries and send plain SQL. |
Beta Was this translation helpful? Give feedback.
-
@janpio Are there plans to ever address that? drizzle managed to implement joins via just one sql query, maybe you could take a look at their approach. |
Beta Was this translation helpful? Give feedback.
-
While it's irritating that Prisma doesn't use joins, there are two distinct issues that should be addressed separately. (1) efficiencies relating to using joins or not, and (2) data correctness relating to using joins or not. (1) is a performance-only issue, important but doesn't affect data integrity directly -- while (2) is an ACID correctness / data integrity issue that's deadly if it's done wrong. If (2) is done wrong, we don't need to care about (1). Frankly if (2) is done wrong, we should all stop using Prisma immediately. The key question is, does Prisma wrap ALL operations in transactions? If the answer is yes, and we as developers configure our DBs with the right isolation level, then issue (2) is OK. As I read the current Prisma docs, they guarantee transactional behavior for only some operations. See https://www.prisma.io/docs/concepts/components/prisma-client/transactions. That lists: nested writes; bulk transactions; and when the
Bottom line: Prisma team, can you assure us that ALL operations, meaning all writes and all reads, are transactional? If it's true that all Prisma operations are wrapped in a transaction, there is no potential race condition between multiple INSERTs, UPDATEs and SELECTs generated by Prisma, and we don't have to worry about data integrity problems as a result of issue (2). Then we can get back to griping about the performance aspects in issue (1) :). (And you should update the docs to say ALL is transactional). However if there are any Prisma operations that aren't transactional (not just writes -- even reads, if Prisma chooses to break the read operation into multiple SELECTs) -- then there is a Severity 1+++++++ data integrity issue. Which seems unlikely but I'd really like to hear the Prisma team say all is transactional, not just the special cases listed on the doco page I referenced above. |
Beta Was this translation helpful? Give feedback.
-
Hi there,
When I make a query with the prisma client which includes other tables data, prisma seems to be chaining several SQL queries instead of making a single query to the database using joins.
It is making my database timeout, as it performs many select queries for very simple operations.
Prisma Version:
3.8.1
Here is my query:
My model is defined like this (I deleted many of the fields to keep it simpler):
This is how I instantiate the
prismaClient
to be able to debug the queries:Here is the list of SQL queries performed by the client:
What I would expect is some sort of intelligent joining like:
Does someone know if there is a way that prisma makes joins like instead of calling multiple queries sequentially?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions