当前位置:网站首页>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
边栏推荐
- 查看僵尸进程
- CollectionUtil:一个函数式风格的集合工具
- 图腾柱和推挽电路介绍
- 【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】
- ADS通信--倍福PLC和C#TextBox控件实现数据绑定的方法
- hdu1752 copy
- 论文阅读 (64):Weakly-supervised Video Anomaly Detection with Robust Temporal Feature Magnitude Learning
- 2022年数据泄露平均成本高达435万美元,创历史新高!
- Facebook社媒营销的5大技巧,迅速提高独立站转化率!
- LeetCode 283. 移动零(简单、数组)
猜你喜欢
论文阅读 (64):Weakly-supervised Video Anomaly Detection with Robust Temporal Feature Magnitude Learning
MySQL-FlinkCDC-Hudi enters the lake in real time
FormData上传二进制文件、对象、对象数组
21 days learning challenge 】 【 sequential search
图腾柱和推挽电路介绍
【请教】SQL语句按列1去重来计算列2之和
神经元网络
spark架构
jvm 二之 栈帧内部结构
【ROS基础】map、odom、base_link、laser 的理解 及其 tf 树的理解
随机推荐
MySQL-索引详解
有趣的网站
【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】
结构体大小计算--结构体内存对齐
【CNN回归预测】基于matlab卷积神经网络CNN数据回归预测【含Matlab源码 2003期】
Ask a question, my Flinkcdc has run through, I can monitor the binlog of msql, and I can also send kafk
OC-NSDictionary
MySQL-数据库设计规范
spark读取文件夹数据
LeetCode 283. Shifting Zeros (Simple, Array)
apt & apt-get命令
SimpleChannelInboundHandler使用总结
MySQL-数据库事务详解
【网络】IP、子网掩码
反射课后习题及做题记录
mysql操作入门(四)-----数据排序(升序、降序、多字段排序)
2022年数据泄露平均成本高达435万美元,创历史新高!
【心电信号】基于matlab心率检测【含Matlab源码 1993期】
hdu1752 copy
主流定时任务解决方案全横评