使用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、批量提交以及一条一条插入都要快。感兴趣到底快多少大家可以自行测试。