当前位置:网站首页>MySQL batch update
MySQL batch update
2022-08-02 07:52:00 【web18224617243】
Article Directory
Disadvantages of direct update
I recently made a request to update 3w pieces of data, one by one, and it took 80 minutes to update, which is very poor in performance and easy to block, so I found some ways to update MySQL in batches and record it here
Method one; replace into
This update will update other fields to default values, because it deletes duplicate records before updating, use with caution
replace into `user` (id,age) values (1,'2'),(2,'3'),(3,'4'),(4,'98');-- > Time: 0.038sMethod 2: insert into [table] values… on duplicate key update
This method should also delete the record and then update it, but save the original data of other fields, so other fields will not change
insert into `user`(id,age) values (1,'5'),(2,'7'),(3,'2'),(4,'198') on duplicatekey update age=values(age)-- > > Time: 0.017sMethod 3: Create a temporary table
Create a temporary table, insert the updated data into the temporary table, and then perform the update, you need to have the permission to create a table
DROP TABLE if EXISTS tmp;-- > Time: 0.016screate temporary table tmp(id int(4) primary key,age varchar(50));-- > Time: 0.01sinsert into tmp values (1,'13'), (2,'16'),(3,'18'),(4,'18');-- > Time: 0.009supdate `user`, tmp set `user`.age=tmp.age where `user`.id=tmp.id;-- > Time: 0.022sMethod 4: Use MySQL's own batch update statement
update `user`set age = CASE idWHEN 1 THEN '22'WHEN 2 THEN '22'WHEN 3 THEN '22'WHEN 4 THEN '22'END WHERE id IN(1,2,3,4);-- > Time: 0.015supdate (tablename)set (updated field) = case (updated field)when (updated field value) then (updated field value)...end where (updated field) in((updated field value)...)Example: set age = CASE id WHEN 1 THEN '22'
In simple terms, id is used as the query condition, and age=22 is updated when id=1. The where statement improves the execution of sqlEfficiency, first filter the records that need to be modified and then update.
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- A full review of mainstream timed task solutions
- 暑假第五周总结
- Splunk Filed extraction 字段截取
- Go 实现分布式锁
- 企业实训复现指导手册——基于华为ModelArts平台的OpenPose模型的训练和推理、基于关键点数据实现对攀爬和翻越护栏两种行为的识别、并完成在图片中只标注发生行为的人
- 有关 sql中的 concat()函数问题,如何拼接
- 查找最大的n个文件
- MySQL-Multiversion Concurrency Control
- The best interests of buying and selling stocks with handling fees [What is missing in the definition of DP status?]
- 【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】
猜你喜欢

【云原生】如何快速部署Kubernetes
![MySQL error 1055 solution: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains](/img/aa/ab58ec47bb96df803dbc6a8ff6dde3.png)
MySQL error 1055 solution: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains

2022夏暑假每日一题(六)

2022.07.31(LC_6132_使数组中所有元素都等于零)

FormData upload binary file, object, object array

MySQL - slow query log

自然语言处理 文本预处理(下)(张量表示、文本数据分析、文本特征处理等)

OC-error prompt

LeetCode 2312. 卖木头块

MySQL-数据库事务详解
随机推荐
Splunk Filed Alias 字段改名
神经元网络
牛客编程题中——需要处理输入较大数的题目
OC-NSString
WebGPU 导入[2] - 核心概念与重要机制解读
MySQL-Multiversion Concurrency Control
OC-范畴
【红队】ATT&CK - 创建或修改系统进程实现持久化(更新ing)
FormData上传二进制文件、对象、对象数组
逆变器锁相原理及DSP实现
mysql操作入门(四)-----数据排序(升序、降序、多字段排序)
获取间隔的日期列表工具类
在VMware上安装Metasploitable2
【机器学习】实验6布置:基于集成学习的Amazon用户评论质量预测
自然语言处理 文本预处理(下)(张量表示、文本数据分析、文本特征处理等)
About the SQL concat () function problem, how to splice
【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】
MySQL-执行流程+缓存+存储引擎
LeetCode刷题(7)
LeetCode 283. Shifting Zeros (Simple, Array)