-
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
Reduce the number of generated SQL statements for Updates/Inserts #5043
Comments
This is a recurring demand from the community, but as the devs said many times the first goal of the beta is to get things working correctly then optimize. There are many open issues for optimization. This one seems a duplicate of #2157. |
Actually we discussed this a bit and calls are different in the issues so we will keep this open |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Actually, PostgreSQL and SQL Server support Also, this would help reducing errors coming from an overly saturated pool in #4280 |
Lets note that the returning doesn't do anything if the update doesn't update a row. So it's debatable is the first select should be left in place or not. It also won't work if other tables are requested in the select/include. I am not even sure it will actually improve things significantly considering the data is likely still in memory when it was just updated. Sometimes the query optimizer is intelligent enough to catch the select and rewrite that as a returning. |
maybe have it as an option to where you can set returning to true |
If we'd be able to cut a simple insert into one query utilizing returning or output, we could then skip the transaction from that query. This would mean that instead of four queries:
we would just do |
any update? I think performance is important for database middlewares like Prisma. |
Any updates on this issue? This looks like a major problem that should be fixed asap. |
I would like to see this fixed as well. I am fairly certain that this is adding a huge amount of latency to my queries. I was hoping to provide some more detailed numbers from the Observability epic #9601, but that development seems to have stalled. From a high level, I've tried a number of setups to get a sense of the latency:
When I say "slow", above, these queries took multiple seconds to run when they worked almost instantly when running entirely on my mac. My understanding of the problem is that joins will often waterfall, so the deeper the joins, the more round trips the data has to take to complete all the queries. This is basically broken from what I can tell, unless the latency is extremely low between app and database servers. Even having the app and database server in the same region was not enough. I do not exactly understand the networking aspect, but my rough understanding/assumption is that if the servers are not configured to do otherwise (e.g. by using the same VPC), they will round trip to the internet for each trip. This will very quickly get too slow to be usable. |
This sounds like a concrete problem we would love to have an issue about with a reproduction project we can try out ourselves. As you are on PlanetScale, there could be some cross effect of the |
Done! See: #12582 |
No updates on this one yet? It's now been more than 2 years... :( |
Would love to have this. Whats the best way to do an update without selects right now? |
@rohanrajpal If I am not mistaken |
Yeah right, didn't know |
Please be aware that this does not work in MS SQL Server if your table has triggers. See https://stackoverflow.com/questions/13198476/cannot-use-update-with-output-clause-when-a-trigger-is-on-the-table for details. So if this is added, there must also be an option to turn it off via some configuration setting. I ran into this problem on a previous project using Knex/Bookshelf.js as the ORM and this option was not configurable, so we had to fork the whole project and make the change ourselves. |
I found that upsert uses only one query instead of three when making an update, and also it uses |
Hey folks, this should be now available for Postgres & CockroachDB on the lastest Prisma 5.1. Have a look at the release notes for more information! Cheers |
I'm not sure if there is a good reason for how it currently works, but it looks like you could reduce the number of SQL statements generated for
update
.Problem
The following code:
Generates this SQL:
Solution
In PostgreSQL, I would normally write this as:
Additional context
You could use
returning
on inserts too.The text was updated successfully, but these errors were encountered: