MySql批量更新的一种方式

使用union all + inner join 更新 速度很快。

SQL语法:

update table_test tt
inner join
(
select -1 as id, 'test' as name, 1 as age ---创建列头
union all select 1, '小红', 18
union all select 2, '小明', 19
...
) a on a.id = tt.id and a.id != -1
set
tt.name = a.name,
tt.age = a.age
where
xxx

Mybatis语法:

update table_test tt
inner join
(
select -1 as id, 'test' as name, 1 as age ---创建列头
  <foreach collection="list" separator=" " item="item">
   union all select #{item.id}, #{item.name}, #{item.age}
  </foreach>
) a on a.id = tt.id and a.id != -1
set
tt.name = a.name,
tt.age = a.age
where
xxx

加餐

union 和 union all的区别

union会把重复的数据给剔除掉,并且会对数据进行排序,会有一定的性能损耗。union all不会。

tips

这里只是一个普通的demo,并没有给出具体的数据对比,但是在我自己使用的过程中发现比mybatis-plus提供的batch、批量提交以及一条一条插入都要快。感兴趣到底快多少大家可以自行测试。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇