当前位置:网站首页>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 .
边栏推荐
- How do I get the largest K massive data
- 在线教育项目用户登录和注册
- 将博客搬至CSDN
- Lumiprobe biotin phosphimide (hydroxyproline) instructions
- Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse
- 修改已经上线的小程序名称
- Personal developed penetration testing tool Satania
- 开发技术-获取10分钟前的时间
- What are database OLAP and OLTP? Same and different? Applicable scenarios
- Peking University ACM problems 1001:exposition
猜你喜欢
随机推荐
对多态的理解
Peking University ACM problems 1006:biorhythms
Dynamic style binding --style and class
Understanding polymorphism
qiao-npms:搜索npm包
Personal developed penetration testing tool Satania
报错FileSystemException: /datas/nodes/0/indices/gtTXk-hnTgKhAcm-8n60Jw/1/index/.es_temp_file:结构需要清理
Flutter 嵌套地狱?不存在的,ConstraintLayout 来解救!
Web APIs 综合案例-Tab栏切换 丨黑马程序员
注册设备监理师难考吗,和监理工程师有什么关系?
ICML2022 | 序列决策的效用理论
数字货币:影响深远的创新
判断js对象是否为空的方式
ArcGIS construction and release of simple road network data service and rest call test
我想知道股票开户要认识谁?另外,手机开户安全么?
Game 81 biweekly
On inline function
加密与解密以及OpenSSL的应用
MySQL advanced 3
Qiao NPMS: search for NPM packages









