探討批量更新數據三種寫法的效率問題。
實現方式有三種,
一種用for循環通過循環傳過來的參數集合,循環出N條sql,
另一種 用mysql的case when 條件判斷變相的進行批量更新
還有一個是用ON DUPLICATE KEY UPDATE進行批量更新
下面進行實現。
注意第一種方法要想成功,需要在db鏈接url後面帶一個參數 &allowMultiQueries=true
即: jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true
其實這種東西寫過來寫過去就是差不多一樣的代碼,不做重複的贅述,直接上代碼。
<!-- 批量更新第一種方法,通過接收傳進來的參數list進行循環着組裝sql -->
<update id="updateBatch" parameterType="java.util.List" >
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update standard_relation
<set >
<if test="item.standardFromUuid != null" >
standard_from_uuid = #{item.standardFromUuid,jdbcType=VARCHAR},
</if>
<if test="item.standardToUuid != null" >
standard_to_uuid = #{item.standardToUuid,jdbcType=VARCHAR},
</if>
<if test="item.gmtModified != null" >
gmt_modified = #{item.gmtModified,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{item.id,jdbcType=BIGINT}
</foreach>
</update>
<!-- 批量更新第二種方法,通過 case when語句變相的進行批量更新 -->
<update id="updateBatch" parameterType="java.util.List" >
update standard_relation
<trim prefix="set" suffixOverrides=",">
<trim prefix="standard_from_uuid =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.standardFromUuid!=null">
when id=#{i.id} then #{i.standardFromUuid}
</if>
</foreach>
</trim>
<trim prefix="standard_to_uuid =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.standardToUuid!=null">
when id=#{i.id} then #{i.standardToUuid}
</if>
</foreach>
</trim>
<trim prefix="gmt_modified =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.gmtModified!=null">
when id=#{i.id} then #{i.gmtModified}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id=#{i.id}
</foreach>
</update>
批量更新第三種方法,用ON DUPLICATE KEY UPDATE
<insert id="updateBatch" parameterType="java.util.List">
insert into standard_relation(id,relation_type, standard_from_uuid,
standard_to_uuid, relation_score, stat,
last_process_id, is_deleted, gmt_created,
gmt_modified,relation_desc)VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id,jdbcType=BIGINT},#{item.relationType,jdbcType=VARCHAR}, #{item.standardFromUuid,jdbcType=VARCHAR},
#{item.standardToUuid,jdbcType=VARCHAR}, #{item.relationScore,jdbcType=DECIMAL}, #{item.stat,jdbcType=TINYINT},
#{item.lastProcessId,jdbcType=BIGINT}, #{item.isDeleted,jdbcType=TINYINT}, #{item.gmtCreated,jdbcType=TIMESTAMP},
#{item.gmtModified,jdbcType=TIMESTAMP},#{item.relationDesc,jdbcType=VARCHAR})
</foreach>
ON DUPLICATE KEY UPDATE
id=VALUES(id),relation_type = VALUES(relation_type),standard_from_uuid = VALUES(standard_from_uuid),standard_to_uuid = VALUES(standard_to_uuid),
relation_score = VALUES(relation_score),stat = VALUES(stat),last_process_id = VALUES(last_process_id),
is_deleted = VALUES(is_deleted),gmt_created = VALUES(gmt_created),
gmt_modified = VALUES(gmt_modified),relation_desc = VALUES(relation_desc)
</insert>
@Override
public void updateStandardRelations() {
List<StandardRelation> list=standardRelationMapper.selectByStandardUuid("xiemingjieupdate");
for(StandardRelation tmp:list){
tmp.setStandardFromUuid(tmp.getStandardFromUuid()+"update");
tmp.setStandardToUuid(tmp.getStandardToUuid()+"update");
}
long begin=System.currentTimeMillis();
standardRelationManager.updateBatch(list);
long end=System.currentTimeMillis();
System.out.print("當前的批量更新的方法用時"+(end-begin)+"ms");
}
sql語句for循環效率其實相當高的,因爲它僅僅有一個循環體,只不過最後update語句比較多,量大了就有可能造成sql阻塞。
case when雖然最後只會有一條更新語句,但是xml中的循環體有點多,每一個case when 都要循環一遍list集合,所以大批量拼sql的時候會比較慢,所以效率問題嚴重。使用的時候建議分批插入。
duplicate key update可以看出來是最快的,但是一般大公司都禁用,公司一般都禁止使用replace into和INSERT INTO … ON DUPLICATE KEY UPDATE,這種sql有可能會造成數據丟失和主從上表的自增id值不一致。而且用這個更新時,記得一定要加上id,而且values()括號裏面放的是數據庫字段,不是java對象的屬性字段。
根據效率,安全方面綜合考慮,選擇適合的很重要。
參考原文:https://blog.csdn.net/xu1916659422/article/details/77971696/