Multiple UPDATE in single transaction vs one UPDATE with big WHERE clause

Peter

what is more efficient:

START TRANSACTION
UPDATE mytable SET foo = 'bar' WHERE (col1 = 813242) AND (col2 = 25343);
UPDATE mytable SET foo = 'bar' WHERE (col1 = 312643) AND (col2 = 8353);
UPDATE mytable SET foo = 'bar' WHERE (col1 = 843564) AND (col2 = 41233);
UPDATE mytable SET foo = 'bar' WHERE (col1 = 321312) AND (col2 = 5325);
UPDATE mytable SET foo = 'bar' WHERE (col1 = 554235) AND (col2 = 6321);
... x 10,000 times or more
COMMIT;

or

UPDATE mytable SET foo = 'bar' WHERE
((col1 = 16344) AND (col2 = 5456)) OR
((col1 = 42134) AND (col2 = 5436)) OR
((col1 = 84563) AND (col2 = 2321)) OR
((col1 = 43216) AND (col2 = 4267)) OR
((col1 = 53248) AND (col2 = 6234)) OR
... x 10,000 times or more

Assuming I have UNIQUE index on (col1,col2)

So my guess is 1st option is nice because of index but it's split into multiple queries, 2nd option is nice because it's only one query but on another hand it does full table scan

this is EXPLAIN when not using OR:

type: ref, possible_keys: myindex_UNIQUE, key: myindex_UNIQUE, ref: const

this is EXPLAIN when using OR:

type: ALL, possible_keys: myindex_UNIQUE, key: null, ref: null

and is there any limit for query WHERE clause?

I aim for max speed

Pred

Based on the conversation in the comments, a possible solution:

If you have a large table and a list of values to identify the rows to update, you can create a helper table for the list of values.

Based on the example in the question, the table could be something like this:

CREATE TABLE mytable_operation (
    col1 INT
  , col2 INT
) ENGINE = MEMORY;

Please note, that the create statement contains the ENGINE = MEMORY hint, so the table will be stored in the memory instead of the disk.

Load the values into this table prior the update.

After all values are loaded into the helper table, you can use the following query to update the values in the production table.

UPDATE
  mytable
SET
  foo = 'bar'
WHERE
  EXISTS (SELECT 1 FROM mytable_operation MO WHERE mytable.col1 = MO.col1 AND mytable.col2 = MO.col2)

Of course, you can use any DML statements to manipulate the production data. (UPDATE with joins, DELETE, INSERT..ON DUPLICATE KEY, etc)

When you finished the data manipulation, you can truncate or drop the helper table.

If the amount of rows are slightly larger in the production table, this solution could be faster than the solutions in the question.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

UPDATE与UPDATE WHERE

来自分类Dev

通过猫鼬使用.where()与.update()查询?

来自分类Dev

UPDATE vs COUNT vs SELECT性能

来自分类Dev

Using date from one table in a where clause with a column in a different table

来自分类Dev

CURSOR vs. UPDATE

来自分类Dev

How do I get my command line utility to update one line instead of printing multiple lines?

来自分类Dev

Update old record instead of creating a new one?

来自分类Dev

How to update multiple rows using single where clause

来自分类Dev

Build string for Linq WHERE clause depending on multiple checkbox choices

来自分类Dev

Insert into a specific row multiple values with Where clause?

来自分类Dev

在UPDATE语句中使用WHERE子句

来自分类Dev

移动物理物体-didSimulatePhysics vs Update

来自分类Dev

使用IN SELECT的Oracle vs MS SQL UPDATE

来自分类Dev

可以在WHERE中执行UPDATE子句吗?

来自分类Dev

Pandas SQL equivalent of update where group by

来自分类Dev

Unknown Column in Where Clause : Mysql Update Query

来自分类Dev

可重复读取隔离级别SELECT vs UPDATE ... WHERE

来自分类Dev

.update()是否也像.set()一样取消.transaction()?

来自分类Dev

使用WHERE子句的UPDATE STATEMENT

来自分类Dev

PHP MYSQL UPDATE WHERE多个条件

来自分类Dev

SQL Update查询和Where子句

来自分类Dev

SQL:不带WHERE子句的UPDATE

来自分类Dev

使用IN SELECT的Oracle vs MS SQL UPDATE

来自分类Dev

Mysqli UPDATE SET WHERE语法错误

来自分类Dev

Lua mySQL update statements and where clauses

来自分类Dev

C#OleDb sql“ UPDATE,WHERE”异常

来自分类Dev

猫鼬,update()vs save()

来自分类Dev

MySQL:选择DATE vs UPDATE日期

来自分类Dev

Firestore 云功能。交易 vs get() => update()

Related 相关文章

热门标签

归档