


MySQL Advanced (14) Batch update and batch update of multiple records with different values implementation methods
mysql Batch update and batch update of multiple records with different value implementation methods
Batch updatemysqlIn mysql we may use # for batch update ##update,replace into to operate, the following details mysqlBatch update and performance.
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';
mysql is also very simple, just modify itwhereThat's it:
UPDATE mytable SET myfield = 'value' WHERE other_field in ('other_values');
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); }
update once, which has poor performance and can easily cause blocking.
So can batch updates be implemented with onesql 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)
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)
Copy after login
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)
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. Thewhere 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)
mysql The statement updated multiple records.
But to use it in business, it needs to be combined with server-side language. Here, takephp 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中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

Application cases of Oracle stored procedure batch update in data processing In actual data processing, we often need to update a large amount of data in the database. Oracle database provides the function of stored procedures, which can effectively handle these large batch data update operations and improve data processing efficiency and performance. In this article, we will introduce the application case of batch update of Oracle stored procedures and provide specific code examples to help readers better understand and use this function. Case Background Suppose we have a

The tacit cooperation between Vue and Excel: How to realize batch update and import of data Introduction: With the rapid development of information technology, Excel tables, as a popular data management tool, are widely used in various industries and fields. At the same time, Vue is also widely popular as a flexible and efficient front-end development framework. This article will introduce how to achieve batch update and import of data through the tacit cooperation of Vue and Excel. To help readers understand better, we will give code examples. Implement data batch update: in V

Efficient combination of Vue and Excel: How to achieve batch update and import of data. With the continuous development of web applications and the increasing amount of data, we often encounter situations where we need to update and import data in batches. As a widely used spreadsheet tool, Excel has powerful data processing and import and export functions, and has become one of our first choice tools for processing large amounts of data. This article will introduce how to use Vue and Excel to implement batch update and import of data to improve the efficiency of data processing. First, we need

In this article, we will see how to perform bulk insert/update in Hibernate. Whenever we execute a sql statement, we do it by making a network call to the database. Now, if we have to insert 10 entries into the database table, then we have to make 10 network calls. Instead, we can optimize network calls by using batch processing. Batch processing allows us to execute a set of SQL statements in a single network call. To understand and implement this, let us define our entity − @EntitypublicclassParent{@Id@GeneratedValue(strategy=GenerationType.AUTO)

How to solve database update performance issues in Java development Summary: With the increase in data volume and business changes, database update performance issues have become a major challenge in Java development. This article will introduce some common methods and techniques to solve database update performance problems. Keywords: Java development, database, update performance issues, solutions Introduction: In most Java applications, the database plays an important role. The performance of the database directly affects the response speed and stability of the application. In actual development, the number

MySQL is a widely used relational database management system that provides many effective data manipulation methods. When a large amount of data needs to be updated, the batch update method can be used to improve efficiency. This article will introduce the batch update method in MySQL. 1. What is batch update? Batch update refers to updating multiple data rows through one SQL statement. Compared with the method of updating one row at a time, batch update can effectively reduce the load of the database and network transmission time, and improve the efficiency and speed of data operations. 2. Implementation method of batch update

Title: Oracle stored procedure batch update implementation method In Oracle database, using stored procedures to batch update data is a common operation. Batch updates can improve the efficiency of data processing, reduce frequent access to the database, and also reduce the complexity of the code. This article will introduce how to use stored procedures to update data in batches in Oracle database, and give specific code examples. First, we need to create a stored procedure that will implement the function of updating data in batches. under
