当前位置:网站首页>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
边栏推荐
- 请教一下,Flink SQL ,JDBC sink 入 mysql 库,想要搞一个自增主键,要怎么写
- Splunk Field Caculated 计算字段
- spark读取文件夹数据
- 2022年数据泄露平均成本高达435万美元,创历史新高!
- gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
- 【机器学习】实验4布置:AAAI会议论文聚类分析
- apt & apt-get命令
- Xilinx约束学习笔记—— 时序约束
- Neural network
- spark 读取本地文件
猜你喜欢

LeetCode 2360. 图中的最长环

Facebook社媒营销的5大技巧,迅速提高独立站转化率!

A Preliminary Study on the Basic Principles of Formal Methods

聊天机器人如何提升独立站的营销水平?

Enterprise training and reproduction guidebook - training and reasoning of the OpenPose model based on Huawei ModelArts platform, realizing the recognition of two behaviors of climbing and climbing ov

Splunk Field Caculated 计算字段

“蔚来杯“2022牛客暑期多校训练营5,签到题KBGHFCD

初探形式化方法基本原理
![带手续费买卖股票的最大利益[找DP的状态定义到底缺什么?]](/img/14/cd6ed7452230571db2e027f61dbdba.png)
带手续费买卖股票的最大利益[找DP的状态定义到底缺什么?]

海缆探测仪TSS350(二)
随机推荐
结构体大小计算--结构体内存对齐
apt & apt-get commands
主流定时任务解决方案全横评
LeetCode 2360. The longest cycle in a graph
MySQL批量更新
spark读取文件夹数据
MySQL-数据库事务详解
队列题目:无法吃午餐的学生数量
反射课后习题及做题记录
Azure Synapse Analytics上创建用户并赋予权限
About the SQL concat () function problem, how to splice
吃透Chisel语言.31.Chisel进阶之通信状态机(三)——Ready-Valid接口:定义、时序和Chisel中的实现
OC-NSNumber and NSValue are generally used for boxing and unboxing
hdu1752 copy
2022年数据泄露平均成本高达435万美元,创历史新高!
请教一下,Flink SQL ,JDBC sink 入 mysql 库,想要搞一个自增主键,要怎么写
有趣的网站
Link with Game Glitch
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
OC - NSSet (set)