MySQL batch update and batch update multiple records of different values implementation method

Source: Internet
Author: User
Tags mysql update

Batch Update

MySQL UPDATE statement is simple, update a field of the data, generally write:

The code is as follows:


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

If you update the same field with the same value, MySQL is also simple, modify where you can:

The code is as follows:


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

Note here that ' other_values ' is a comma (,) delimited string, such as:

If you update more than one data for a different value, many people might write this:

The code is as follows:
foreach ($display _order as $id = + $ordinal) {
$sql = "UPDATE categories SET Display_order = $ordinal WHERE id = $id";
mysql_query ($sql);
}

That is, an update record that loops through one article. One record update once, so performance is poor, it is also easy to cause blocking.

So can a SQL statement implement batch update? MySQL does not provide a straightforward way to implement batch updates, but it can be accomplished with a little bit of skill.

The code is as follows:
UPDATE mytable
SET myfield = case ID
When 1 Then ' value '
When 2 Then ' value '
When 3 Then ' value '
END
WHERE ID in (All-in-a-

This is a small trick to implement a batch update.
As an example:

The code is as follows:
UPDATE Categories
SET Display_order = case ID
When 1 then 3
When 2 then 4
When 3 then 5
END
WHERE ID in (All-in-a-

This SQL means, update the Display_order field, if id=1 the value of Display_order is 3, if id=2 Display_order value is 4, if id=3 then Display_order value is 5.
That is, the conditional statements are written together.
The where section here does not affect the execution of the code, but it improves the efficiency of SQL execution. Make sure that the SQL statement executes only the number of rows that need to be modified, where only 3 of the data is updated, while the WHERE clause ensures that only 3 rows of data are executed.

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

The code is as follows:
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 (All-in-a-

Here, a MySQL statement has been completed to update multiple records.
But to use in the business, you need to combine the service-side language, here in PHP, for example, constructs this MySQL statement:

The code is as follows:
$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, there are 8 records to update. The code is easy to understand, have you learned it?

Performance analysis

When I use tens of thousands of records to use the MySQL batch update, found that using the most original batch update discovery performance is poor, will see the online summary of the following three ways:

1. batch update, one record update once, poor performance

The code is as follows:
Update test_tbl set dr= ' 2 ' where id=1;

2.replace into or INSERT INTO ... on duplicate key update

Copy CodeThe code is as follows:
Replace into TEST_TBL (ID,DR) VALUES (1, ' 2 '), (2, ' 3 '),... (x, ' Y ');

or use

The code is as follows:
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 staging table first, and then the update from the temp table

Code to copy code 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 the user to have the Create permission for the temporary table.

The following is a performance test result of the above method update 100,000 data:

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

As far as the test results are, the test uses replace into to perform better.

The difference between replace into and insert to on duplicate key update is:
The replace into operation Essence is to delete the duplicate record first after the insert, if the updated field does not set the missing field to the default value
Insert into is only an update duplicate record and does not change other fields.

MySQL batch update and batch update multiple records of different values implementation method

Related Article

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.