multi-row multi-value update in a MySQL transaction
I am using innodb tables in MySQL. I want to update several rows in a table, with each row getting a different value, e.g.:
UPDATE tbl_1 SET
col1=3 WHERE id=25,
col1=5 WHERE id=26
In Postgres I believe this is possible:
UPDATE tbl_1 SET col1 = t.col1 FROM (VALUES
(25, 3)
(26, 5)
) AS t(id, col1)
WHERE tbl_1.id = t.id;
How do you do this efficiently and effectively in a transaction?
Issues I hit so far:
- using an intermediate temporary MEMORY table turns out to not be transaction safe
- using a TEMPORARY table - persumably MEMORY type again - is virtually undocumented and I can find no re开发者_高级运维al explanation of how it works and how well it works in my case, for example any discussion on whether the table is truncated after each transaction on the session
- using an InnoDB table as a temporary table and filling, joining to update and then truncating it in the transaction seems a very expensive thing to do; I've been fighting MySQL's poor throughput enough as it is
Do you update with a case and set value for col1
depending on id
UPDATE tbl_1 SET col1=CASE id WHEN 25 THEN 3 WHEN 26 THEN 5 END WHERE id IN (25,26)
精彩评论