Combining two UPDATE commands - performance?

If I want to update two rows in a MySQL table using the following command:

UPDATE table SET Col = Value1 WHERE ID = ID1

UPDATE table SET Col = Value2 WHERE ID = ID2`

I usually combine them into one command, so I don’t need to access the MySQL server twice from my C client:

UPDATE table SET Col = IF( ID = ID1 , Value1 , Value2) WHERE ID=ID1 OR ID=ID2

Is this really an increase in productivity?

Reference Information. I am using a specially crafted, fully-written, high-performance, highly-loaded web server.

+3
source share
4 answers

A single server request is usually a good idea, since round trips between the client and server are often the most expensive part of many requests (as Will Hartung has already pointed out).

. id = id1 or id = id2 , (. OR Relations). , ID1 ID2, , "" . IN: where ID in (ID1, ID2). , , .

+1

. , ( - ) .

0

, . , , .

0
source

Yes, it will be faster because you are doing one trip to the database instead of two. The overhead of the network call will probably dominate this type request (ban on making real bad optimizer decisions on parts of MySQL).

However, even if you have two separate UPDATE statements, you can send them one request to the background, and you will also be left with standard SQL.

0
source

Source: https://habr.com/ru/post/1746082/


All Articles