C
C
crayes232016-09-30 14:02:37
MySQL
crayes23, 2016-09-30 14:02:37

Update case or two requests?

Which is more productive:
two regular UPDATEs with a WHERE condition on the primary key or
UPDATE test SET text =
CASE
WHEN id = 1 THEN 'ok'
WHEN id = 2 THEN 'no'
ELSE text <--additional question-- it's not necessary, why then do that?
END
, id - primary key
in InnoDB table with 100,000+ rows

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Marat, 2016-09-30
@crayes23

Simple queries are easier to maintain (than bunches of WHENs).
In terms of speed, it depends on the presence of simultaneous requests to the same table, in terms of locks - their number will be the same.
www.mysql.ru/docs/man/InnoDB_Locks_set.html etc.
PS Sub-question:
This is the case in many SQL servers (for example, in Oracle) - the analyzer does not know the values ​​of the fields involved in the When conditions, therefore the ELSE branch is not required, but a RUNTIME error may occur when optimizing the query for execution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question