Home  >  Article  >  Database  >  MySQL Advanced (14) Batch update and batch update of multiple records with different values ​​​​implementation methods

MySQL Advanced (14) Batch update and batch update of multiple records with different values ​​​​implementation methods

黄舟
黄舟Original
2017-02-10 11:03:05858browse

mysql Batch update and batch update of multiple records with different value implementation methods

In mysql we may use # for batch update ##update,replace into to operate, the following details mysqlBatch update and performance.

Batch update

mysql

The update statement is very simple. It updates a certain field of a piece of data. It is usually written like this:

UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_value';


If you update the same field to the same value,

mysql is also very simple, just modify itwhereThat's it:

UPDATE mytable SET myfield = 'value' WHERE other_field in ('other_values');


Note here that '

other_values' is separated by a comma (,) string, such as: 1,2,3

Then if multiple pieces of data are updated to different values, many people may write like this:

foreach ($display_order as $id => $ordinal) { 
    $sql = "UPDATE categories SET display_order = $ordinal WHERE id = $id"; 
    mysql_query($sql); 
}


is to loop through the update records one by one. One record

update once, which has poor performance and can easily cause blocking.

So can batch updates be implemented with one

sql statement? mysql does not provide a direct method to implement batch updates, but it can be achieved with some tricks.

UPDATE mytable 
    SET myfield = CASE id 
        WHEN 1 THEN 'value'
        WHEN 2 THEN 'value'
        WHEN 3 THEN 'value'
    END
WHERE id IN (1,2,3)

The little trick

case when is used here to achieve batch updates.

For example:

UPDATE categories 
    SET display_order = CASE id 
        WHEN 1 THEN 3 
        WHEN 2 THEN 4 
        WHEN 3 THEN 5 
    END
WHERE id IN (1,2,3)


This sentence

sql means , update the display_order field. If id=1 , then the value of display_order is 3, if id=2 then the value of display_order is 4, if id=3 then the value of display_order is 5.

is to write the conditional statements together.

The

where part here does not affect the execution of the code, but will improve the efficiency of sql execution. Make sure that the sql statement only executes the number of rows that need to be modified. Here, only 3 pieces of data are updated, while The where clause ensures that only 3 rows of data are executed.

If you update multiple values, you only need to modify it slightly:

UPDATE categories 
    SET display_order = CASE id 
        WHEN 1 THEN 3 
        WHEN 2 THEN 4 
        WHEN 3 THEN 5 
    END, 
    title = CASE id 
        WHEN 1 THEN 'New Title 1'
        WHEN 2 THEN 'New Title 2'
        WHEN 3 THEN 'New Title 3'
    END
WHERE id IN (1,2,3)


At this point, one item has been completed

mysql The statement updated multiple records.

But to use it in business, it needs to be combined with server-side language. Here, take

php as an example to construct this mysql Statement:

$display_order = array( 
    1 => 4, 
    2 => 1, 
    3 => 2, 
    4 => 3, 
    5 => 9, 
    6 => 5, 
    7 => 8, 
    8 => 9 
); 
$ids = implode(',', array_keys($display_order)); 
$sql = "UPDATE categories SET display_order = CASE id "; 
foreach ($display_order as $id => $ordinal) { 
    $sql .= sprintf("WHEN %d THEN %d ", $id, $ordinal); 
} 
$sql .= "END WHERE id IN ($ids)"; 
echo $sql;


这个例子,有8条记录进行更新。代码也很容易理解,你学会了吗

性能分析

当我使用上万条记录利用mysql批量更新,发现使用最原始的批量update发现性能很差,将网上看到的总结一下一共有以下三种办法:

1.批量update,一条记录update一次,性能很差

update test_tbl set dr='2' where id=1;


2.replace into 或者insert into ...on duplicate key update

replace into test_tbl (id,dr) values (1,'2'),(2,'3'),...(x,'y');


或者使用

insert into test_tbl (id,dr) values  (1,'2'),(2,'3'),...(x,'y') on duplicate key update dr=values(dr);


3.创建临时表,先更新临时表,然后从临时表中update

create temporary table tmp(id int(4) primary key,dr varchar(50));
insert into tmp values  (0,'gone'), (1,'xx'),...(m,'yy');
update test_tbl, tmp set test_tbl.dr=tmp.dr where test_tbl.id=tmp.id;


注意:这种方法需要用户有temporary 表的create 权限。

下面是上述方法update 100000条数据的性能测试结果:

逐条update

real    0m15.557s

user    0m1.684s

sys    0m1.372s

replace into

real    0m1.394s

user    0m0.060s

sys    0m0.012s

insert into on duplicate key update

real    0m1.474s

user    0m0.052s

sys    0m0.008s

create temporary table and update:

real    0m0.643s

user    0m0.064s

sys    0m0.004s

就测试结果来看,测试当时使用replace into性能较好。

replace into  insert into on duplicate key update的不同在于:

replace into 操作本质是对重复的记录先delete insert,如果更新的字段不全会将缺失的字段置为缺省值

insert into 则是只update重复记录,不会改变其它字段。

QUESTION:

Error Code: 1175. You are using safe update mode and you tried to update a table without a 
WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.

SOLVE:

原因是在safe mode,要强制安全点,update只能跟where要取消这个限制,可以:

SET SQL_SAFE_UPDATES=0;

以上就是mysql进阶(十四) 批量更新与批量更新多条记录的不同值实现方法 的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn