当前位置:网站首页>MySQL batch update
MySQL batch update
2022-06-30 21:09:00 【zjun1001】
Method 1 : use update combination case、then Realization
original SQL sentence
UPDATE baginfo_2021_09
SET channel_id = CASE id
WHEN 1 THEN 3
WHEN 2 THEN 4
WHEN 3 THEN 5
END,
stationId = CASE id
WHEN 1 THEN 6
WHEN 2 THEN 7
WHEN 3 THEN 8
END
WHERE id IN (1,2,3)
mybatis How to write it
<update id="updateBatchCaseThen" parameterType="java.util.List">
update baginfo_2022_03
<trim prefix="set" suffixOverrides=",">
<trim prefix="clientId =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.clientId!=null">
when id=#{i.id} then #{i.clientId}
</if>
</foreach>
</trim>
<trim prefix="model_version =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.model_version!=null">
when id=#{i.id} then #{i.model_version}
</if>
</foreach>
</trim>
<trim prefix="create_time =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.create_time!=null">
when id=#{i.id} then #{i.create_time}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index">
id=#{i.id}
</foreach>
</update>
Method 2 : use foreach Cycle to achieve
mybatis How to write it
<update id="batchUpdateBagInfo" parameterType="com.example.demo.entity.BagInfo">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update baginfo_2022_03
<set>
<if test="item.clientId != null">
clientId = #{item.clientId},
</if>
<if test="item.model_version != null">
model_version = #{item.model_version},
</if>
<if test="item.create_time != null">
create_time = #{item.create_time},
</if>
</set>
where id = #{item.id}
</foreach>
</update>
In this way ,yml The database configured in the file URL, Need to add &allowMultiQueries=true
Performance comparison
mysql Table data quantity :2000w; Amount of data updated at one time :1000
| Update method | Total update time |
|---|---|
| Single loop update (for) | 1-3s |
| Method 1 | 110ms-180ms |
| Method 2 | 110ms-180ms |
Overall comparison , The slowest single loop update , Think about it . The method 1 And methods 2 The time taken is not much different . A higher number of batch updates , May lead to different results , There is no test here .
边栏推荐
猜你喜欢

Basic components of STL

Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)

CentOS - enable / disable Oracle

大学生研究生毕业找工作,该选择哪个方向?

uniapp-富文本编辑器

Icml2022 | utility theory of sequential decision making

申请Vector 总线协议彩图壁纸挂画,非常棒哦!

Lumiprobe cell biology - dia, instructions for lipophilic tracer

Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse

《大厂面试》之JVM篇21问与答
随机推荐
Dynamic style binding --style and class
多态在代码中的体现
学习总结
B_QuRT_User_Guide(35)
Peking University ACM problems 1006:biorhythms
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
SQL Server 提取字符串中的纯数字
防范未授权访问攻击的十项安全措施
RP原型资源分享-购物类App
Software engineering UML drawing
Gartner聚焦中国低代码发展 UniPro如何践行“差异化”
代码改变一小步,思维跨越一大步
MySQL introduction, detailed installation steps and usage | dark horse programmer
19.04 分配器
两个skyline
Personal developed penetration testing tool Satania
Analysis of breakpoint continuation and download principle
Testing principle and precautions of biovendor rage ELISA Kit
Study on lumiprobe modified triphosphate biotin-11-utp
uniapp-富文本编辑器