当前位置:网站首页>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.038s
Method 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.017s
Method 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.022s
Method 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
边栏推荐
- LeetCode刷题(7)
- The best interests of buying and selling stocks with handling fees [What is missing in the definition of DP status?]
- Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)
- MySQL报错1055解决办法:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains
- 2022.07.31(LC_6132_使数组中所有元素都等于零)
- OC-Category
- 【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】
- 2020美亚团队赛复盘
- Modify apt-get source to domestic mirror source
- Xilinx约束学习笔记—— 时序约束
猜你喜欢
随机推荐
MySQL-多版本并发控制
LeetCode 2360. The longest cycle in a graph
spark读取文件夹数据
Go implements distributed locks
Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)
php删除一维数组中一个值
正则表达式
Azure Synapse Analytics上创建用户并赋予权限
MySQL-索引优化和查询优化
新产品立大功 伟世通第二季度营收双增
飞桨paddle技术点整理
【机器学习】课程设计布置:某闯关类手游用户流失预测
自然语言处理 文本预处理(上)(分词、词性标注、命名实体识别等)
LeetCode 283. 移动零(简单、数组)
CSRF-跨站请求伪造-相关知识
【机器学习】实验5布置:AAAI会议论文聚类分析
【ROS基础】rosbag 的使用方法
sql 远程访问链接服务器
JS初识高阶函数和函数柯里化
2022年数据泄露平均成本高达435万美元,创历史新高!