当前位置:网站首页>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 .
边栏推荐
- Lumiprobe 聚乙二醇化和 PEG 接头丨碘-PEG3-酸研究
- Peking University ACM problems 1002:487-3279
- uniapp-富文本编辑器
- Flinksql两个kafka 流可以进行join么?
- On inline function
- Binary search tree (1) - concept and C language implementation
- Lumiprobe biotin phosphimide (hydroxyproline) instructions
- Qiao NPMS: search for NPM packages
- Two skylines
- Electronic scheme development - Intelligent rope skipping scheme
猜你喜欢

Iclr'22 spotlight | how to measure the amount of information in neural network weights?

多态在代码中的体现

stacking集成模型预测回归问题

Lumiprobe protein quantitation - qudye Protein Quantitation Kit

Binary search tree (1) - concept and C language implementation

Qiao NPMS: search for NPM packages

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

uniapp-生命周期/路由跳转

Study on lumiprobe modified triphosphate biotin-11-utp

Lvalue reference and lvalue reference
随机推荐
Lumiprobe copper free click chemical solution
电子方案开发——智能跳绳方案
uniapp-第三方网络请求
多表操作-外键约束
Deflection lock / light lock / heavy lock lock is healthier. How to complete locking and unlocking
判断js对象是否为空的方式
ncat详细介绍(转载)
Go learning notes
docker安装mysql
Personal developed penetration testing tool Satania
Adobe-Photoshop(PS)-脚本开发-去除文件臃肿脚本
A small step in code change and a big leap in thinking
What about degradation of text generation model? Simctg tells you the answer
在手机炒股开户安全吗?
Binary search tree (1) - concept and C language implementation
软工UML画图
Go build server Foundation
MySQL高级篇3
Two skylines
SQL Server 提取字符串中的纯数字