当前位置:网站首页>mysql-批量更新
mysql-批量更新
2022-06-30 20:55:00 【zjun1001】
方法一:用update结合case、then实现
原始SQL语句
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写法
<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>
方法二:用foreach循环实现
mybatis写法
<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>
用这种方法时,yml文件中配置的数据库URL,需要加上&allowMultiQueries=true
性能比较
mysql表数据量:2000w;一次更新数据量:1000
| 更新方法 | 更新总耗时 |
|---|---|
| 单条循环更新(for) | 1-3s |
| 方法1 | 110ms-180ms |
| 方法2 | 110ms-180ms |
总体比较,单条循环更新最慢的,想想也知道。而方法1和方法2的耗时相差不大。更高数量的批量更新,可能会导致结果不同,这里暂时没有测试。
边栏推荐
- 请问海量数据如何去取最大的K个
- Game 81 biweekly
- Evolution of screen display technology
- Huffman Tree (1) Basic Concept and C - language Implementation
- Go build server Foundation
- 数据库 OLAP、OLTP是什么?相同和不同?适用场景
- Lumiprobe nucleic acid quantitative qudye dsDNA br detection kit
- 微信小程序怎么实现圆心进度条
- 基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
- Peking University ACM problems 1002:487-3279
猜你喜欢
随机推荐
Study on lumiprobe modified triphosphate biotin-11-utp
加密与解密以及OpenSSL的应用
微信小程序怎么实现圆心进度条
stacking集成模型预测回归问题
MySQL高级篇3
减少嵌入式软件调试时间的三个技巧
Deflection lock / light lock / heavy lock lock is healthier. How to complete locking and unlocking
翻转链表II[翻转链表3种方式+dummyHead/头插法/尾插法]
Scene 299
uniapp-富文本编辑器
开发技术-使用easyexcel导入文件(简单示例)
哈夫曼樹(一)基本概念與C語言實現
Lumiprobe细胞生物学丨DiA,亲脂性示踪剂说明书
判断js对象是否为空的方式
断点续传和下载原理分析
利用日志服务器输出各种apache的日志的TOPN
Dynamic style binding --style and class
Wechat applet development practice cloud music
19.04 distributor
在线教育项目用户登录和注册









