Implementation of different values for mysql batch update and batch update of multiple records _ MySQL

Source: Internet
Author: User
Tags mysql update
Implementation of different values for mysql batch update and batch update of multiple records bitsCN.com

Batch update

The mysql update statement is simple. to update a field of a piece of data, write as follows:


UPDATE mytable SET myfield = 'value' WHERE other_field = 'other _ value ';

If the same field is updated to the same value, mysql is also very simple. modify where:


UPDATE mytable SET myfield = 'value' WHERE other_field in ('other _ values ');

Note that 'other _ values' is a comma (,)-separated string, for example, 1, 2, 3.

If multiple data entries are updated with different values, many may write as follows:


Foreach ($ display_order as $ id => $ ordinal ){
$ SQL = "UPDATE categories SET display_order = $ ordinal WHERE id = $ id ";
Mysql_query ($ SQL );
}

It is an update record that repeats one by one. Update a record once, which has poor performance and can easily cause blocking.

Can I update an SQL statement in batches? Mysql does not provide a direct method for batch update, but it can be implemented with tips.


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)

Here we use the case when tip to implement batch update.
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 SQL statement indicates that the display_order field is updated. if id = 1, the value of display_order is 3. if id = 2, the value of display_order is 4, if id = 3, the value of display_order is 5.
The condition statements are written together.
The where part does not affect code execution, but it increases the SQL execution efficiency. Make sure that the SQL statement only executes the number of rows to be modified. here, only three rows of data are updated, and the where clause ensures that only three rows of data are executed.

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


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, a mysql statement has been completed to update multiple records.
But to use it in the business, you need to combine the server language. Here we 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;

In this example, eight records are updated. The code is easy to understand. have you learned it?

Performance analysis

When I use tens of thousands of records to use mysql for batch update, I find that the performance is poor when using the most primitive batch update. I can summarize the following three methods on the Internet:

1. batch update: one record is updated once, with poor performance


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

2. replace into or insert into... on duplicate key update


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

Or use


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

3. create a temporary table, update the temporary table first, and then update from the temporary table

The code is as follows:
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;

Note: This method requires you to have the create permission for the temporary table.

The following is the performance test result of the above method to update 100000 pieces of data:

Update one by one

Real 0m15. 557 s
User 0m1. 684 s
Sys 0m1. 372 s

Replace
Real 0m1. 394 s
User 0m0. 060 s
Sys 0m0. 012 s

Insert into on duplicate key update
Real 0m1. 474 s
User 0m0. 052 s
Sys 0m0. 008 s

Create temporary table and update:
Real 0m0. 643 s
User 0m0. 064 s
Sys 0m0. 004 s

The test results show that the performance of replace into is better.

The difference between replace into and insert into on duplicate key update is:
The replace into operation is essentially to delete duplicate records and then insert them. if the updated field is not plenary, set the missing field to the default value.
Insert into indicates that only the record is updated and other fields are not changed.

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.