Reduce the number of generated SQL statements for Updates/Inserts
See original GitHub issueI’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:
const updatedPost = await prisma.post.update({
where: {
id: 1,
},
data: {
published: true,
},
});
Generates this SQL:
prisma:query BEGIN
prisma:query SELECT `dev`.`Post`.`id` FROM `dev`.`Post` WHERE `dev`.`Post`.`id` = ?
prisma:query UPDATE `dev`.`Post` SET `published` = ? WHERE `dev`.`Post`.`id` IN (?)
prisma:query SELECT `dev`.`Post`.`id`, `dev`.`Post`.`title`, `dev`.`Post`.`content`, `dev`.`Post`.`published`, `dev`.`Post`.`authorId` FROM `dev`.`Post` WHERE `dev`.`Post`.`id` = ? LIMIT ? OFFSET ?
prisma:query COMMIT
Solution
In PostgreSQL, I would normally write this as:
update `dev`.`Post` set published = ? where id = ? returning *
Additional context
You could use returning
on inserts too.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:50
- Comments:18 (7 by maintainers)
Top Results From Across the Web
Ryan Chenkie on Twitter: "Some will say "that's a lot of SQL for a ...
They are: error info query warn Let's look at how to use the `query` log level ... Reduce the number of generated SQL...
Read more >Db2 Log Analysis Tool - Generating SQL - IBM
Log Analysis Tool can generate SQL for REDO and UNDO purposes, though you should be ... A COMMIT statement will be added every...
Read more >Performance Tuning SQL Queries | Advanced SQL - Mode
Learn how to conduct SQL performance tuning by reducing table size, simplifying joins, & the EXPLAIN command in this advanced SQL tutorial.
Read more >SQL Server and Azure SQL index architecture and design guide
Use many indexes to improve query performance on tables with low update ... on primary replicas in Azure SQL Database automatically generate ......
Read more >3 Features of Oracle Transparent Gateways and Generic ...
In the case of SQL statements, if functionality is missing at the remote system, ... reducing the number of times a SQL statement...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No updates on this one yet? It’s now been more than 2 years… 😦
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 https://github.com/prisma/prisma/issues/2157.