question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reduce the number of generated SQL statements for Updates/Inserts

See original GitHub issue

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:

  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:open
  • Created 3 years ago
  • Reactions:50
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

13reactions
aradalvandcommented, May 3, 2022

No updates on this one yet? It’s now been more than 2 years… 😦

7reactions
Syttencommented, Apr 14, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found