当前位置:网站首页>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 .
边栏推荐
- Software engineering UML drawing
- Lumiprobe生物素亚磷酰胺(羟脯氨酸)说明书
- ssh-server配置文件参数PermitRootLogin介绍
- C file pointer
- Lumiprobe dye hydrazide - BDP FL hydrazide solution
- 多表操作-外键约束
- Qiao NPMS: search for NPM packages
- Amazon restricts LGBTQ related search and product sales in the United Arab Emirates
- [原创]用代码dialog 高度 宽度无法屏幕屏幕问题
- Adobe Photoshop (PS) - script development - remove file bloated script
猜你喜欢

关于,奇安信检测代码漏洞,XSS系列解决

stacking集成模型预测回归问题

centos——开启/关闭oracle

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could

Markdown笔记简明教程

银行集体下架的智能投顾产品,为何成了“鸡肋”?

ArcGIS构建发布简单路网Network数据服务及Rest调用测试

Lumiprobe biotin phosphimide (hydroxyproline) instructions

Introduction of 3D Max fine model obj model into ArcGIS pro (II) key points supplement

Double solid histogram / double y-axis
随机推荐
在线教育项目用户登录和注册
Web APIs 综合案例-Tab栏切换 丨黑马程序员
SQL必需掌握的100个重要知识点:创建和操纵表
凤凰架构——架构师的视角
报错FileSystemException: /datas/nodes/0/indices/gtTXk-hnTgKhAcm-8n60Jw/1/index/.es_temp_file:结构需要清理
电子方案开发——智能跳绳方案
B_QuRT_User_Guide(34)
What about degradation of text generation model? Simctg tells you the answer
What are database OLAP and OLTP? Same and different? Applicable scenarios
Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse
DM8:生成DM AWR报告
oprator-1初识oprator
Basic concepts of tree
对多态的理解
双立体柱状图/双y轴
Use the log server to output the topn of various Apache logs
片荒吗?不用下载直接在线免费看的资源来了!2022年收藏夹必须有它!
【等级测评师】等级测评师怎么报名?多少分及格?
MySQL introduction, detailed installation steps and usage | dark horse programmer
防范未授权访问攻击的十项安全措施