背景描述
通常如果需要一次更新多條數(shù)據(jù)有兩個(gè)方式:
(1)在業(yè)務(wù)代碼中循環(huán)遍歷逐條更新。
(2)一次性更新所有數(shù)據(jù)(更準(zhǔn)確的說(shuō)是一條sql語(yǔ)句來(lái)更新所有數(shù)據(jù),逐條更新的操作放到數(shù)據(jù)庫(kù)端,在業(yè)務(wù)代碼端展現(xiàn)的就是一次性更新所有數(shù)據(jù))。兩種方式各有利弊,下面將會(huì)對(duì)兩種方式的利弊做簡(jiǎn)要分析,主要介紹第二種方式在mybatis中的實(shí)現(xiàn)。
逐條更新
這種方式顯然是最簡(jiǎn)單,也最不容易出錯(cuò)的,即便出錯(cuò)也只是影響到當(dāng)條出錯(cuò)的數(shù)據(jù),而且可以對(duì)每條數(shù)據(jù)都比較可控,更新失敗或成功,從什么內(nèi)容更新到什么內(nèi)容,都可以在邏輯代碼中獲取。代碼可能像下面這個(gè)樣子:
updateBatch(List<MyData> datas){
? ? for(MyData data : datas){
? ? ? ? try{
? ? ? ? ? ? myDataDao.update(data);//更新一條數(shù)據(jù),mybatis中如下面的xml文件的update
? ? ? ? }
? ? ? ? catch(Exception e){
? ? ? ? ? ? ...//如果更新失敗可以做一些其他的操作,比如說(shuō)打印出錯(cuò)日志等
? ? ? ? }
? ? }
}
//mybatis中update操作的實(shí)現(xiàn)
<update>
? ? update mydata
? ? set ? ...
? ? where ...
</update>
這種方式最大的問(wèn)題就是效率問(wèn)題,逐條更新,每次都會(huì)連接數(shù)據(jù)庫(kù),然后更新,再釋放連接資源(雖然通過(guò)連接池可以將頻繁連接數(shù)據(jù)的效率大大提高,抗不住數(shù)據(jù)量大),這中損耗在數(shù)據(jù)量較大的時(shí)候便會(huì)體現(xiàn)出效率問(wèn)題。
這也是在滿足業(yè)務(wù)需求的時(shí)候,通常會(huì)使用上述提到的第二種批量更新的實(shí)現(xiàn)(當(dāng)然這種方式也有數(shù)據(jù)規(guī)模的限制,后面會(huì)提到)。
sql批量更新
一條sql語(yǔ)句來(lái)批量更新所有數(shù)據(jù),下面直接看一下在mybatis中通常是怎么寫(xiě)的(去掉mybatis語(yǔ)法就是原生的sql語(yǔ)句了,所有就沒(méi)單獨(dú)說(shuō)sql是怎么寫(xiě)的)。
<update id="updateBatch" parameterType="java.util.List">
? ? update mydata_table?
? ? set ?status=
? ? <foreach collection="list" item="item" index="index"?
? ? ? ? separator=" " open="case ID" close="end">
? ? ? ? when #{item.id} then #{item.status}
? ? </foreach>
? ? where id in
? ? <foreach collection="list" index="index" item="item"?
? ? ? ? separator="," open="(" close=")">
? ? ? ? #{item.id,jdbcType=BIGINT}
? ? </foreach>
?</update>
其中when...then...是sql中的"switch" 語(yǔ)法。這里借助mybatis的<foreach>語(yǔ)法來(lái)拼湊成了批量更新的sql,上面的意思就是批量更新id在updateBatch參數(shù)所傳遞List中的數(shù)據(jù)的status字段。還可以使用<trim>實(shí)現(xiàn)
同樣的功能,代碼如下:
<update id="updateBatch" parameterType="java.util.List">
? ? ? ? update mydata_table
? ? ? ? <trim prefix="set" suffixOverrides=",">
? ? ? ? ? ? <trim prefix="status =case" suffix="end,">
? ? ? ? ? ? ? ? <foreach collection="list" item="item" index="index">
? ? ? ? ? ? ? ? ? ? ?when id=#{item.id} then #{item.status}
? ? ? ? ? ? ? ? </foreach>
? ? ? ? ? ? </trim>
? ? ? ? </trim>
? ? ? ? where id in
? ? ? ? <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
? ? ? ? ? ? #{item.id,jdbcType=BIGINT}
? ? ? ? </foreach>
? ? </update>
<trim>屬性說(shuō)明
- 1.prefix,suffix 表示在trim標(biāo)簽包裹的部分的前面或者后面添加內(nèi)容
- 2.如果同時(shí)有prefixOverrides,suffixOverrides 表示會(huì)用prefix,suffix覆蓋Overrides中的內(nèi)容。
- 3.如果只有prefixOverrides,suffixOverrides 表示刪除開(kāi)頭的或結(jié)尾的xxxOverides指定的內(nèi)容。
上述代碼轉(zhuǎn)化成sql如下:
? ? update mydata_table?
? ? set status =?
? ? case
? ? ? ? when id = #{item.id} then #{item.status}//此處應(yīng)該是<foreach>展開(kāi)值
? ? ? ? ...
? ? end
? ? where id in (...);
當(dāng)然這是最簡(jiǎn)單的批量更新實(shí)現(xiàn),有時(shí)候可能需要更新多個(gè)字段,那就需要將
<trim prefix="status =case" suffix="end,">
? ? ?<foreach collection="list" item="item" index="index">
? ? ? ? ? when id=#{item.id} then #{item.status}
? ? ?</foreach>
</trim>
復(fù)制拷貝多次,更改prefix和when...then...的內(nèi)容即可.而如果當(dāng)需要為某個(gè)字段設(shè)置默認(rèn)值的時(shí)候可以使用else
<trim prefix="status =case" suffix="end,">
? ? ?<foreach collection="list" item="item" index="index">
? ? ? ? ? when id=#{item.id} then #{item.status}
? ? ?</foreach>
? ? ?else default_value
</trim>
還有更常見(jiàn)的情況就是需要對(duì)要更新的數(shù)據(jù)進(jìn)行判斷,只有符合條件的數(shù)據(jù)才能進(jìn)行更新,這種情況可以這么做:
<trim prefix="status =case" suffix="end,">
? ? ?<foreach collection="list" item="item" index="index">
? ? ? ? ?<if test="item.status !=null and item.status != -1">
? ? ? ? ? ? ?when id=#{item.id} then #{item.status}
? ? ? ? ?</if>
? ? ?</foreach>
</trim>
這樣的話只有要更新的list中status != null && status != -1的數(shù)據(jù)才能進(jìn)行status更新.其他的將使用默認(rèn)值更新,而不會(huì)保持原數(shù)據(jù)不變.如果要保持原數(shù)據(jù)不變呢?即滿足條件的更新,不滿足條件的保持原數(shù)據(jù)不變,簡(jiǎn)單的來(lái)做就是再加一個(gè)<if>,因?yàn)閙ybatis中沒(méi)有if...else...語(yǔ)法,但可以通過(guò)多個(gè)<if>實(shí)現(xiàn)同樣的效果,如下:
<trim prefix="status =case" suffix="end,">
? ? ?<foreach collection="list" item="item" index="index">
? ? ? ? ?<if test="item.status !=null and item.status != -1">
? ? ? ? ? ? ?when id=#{item.id} then #{item.status}
? ? ? ? ?</if>
? ? ? ? ?<if test="item.status == null or item.status == -1">
? ? ? ? ? ? ?when id=#{item.id} then mydata_table.status ? ? ?//這里就是原數(shù)據(jù)
? ? ? ? ?</if>
? ? ?</foreach>
</trim>
整體批量更新的寫(xiě)法如下:
<update id="updateBatch" parameterType="java.util.List">
? ? ? ? update mydata_table
? ? ? ? <trim prefix="set" suffixOverrides=",">
? ? ? ? ? ? <trim prefix="status =case" suffix="end,">
? ? ? ? ? ? ? ? ?<foreach collection="list" item="item" index="index">
? ? ? ? ? ? ? ? ? ? ?<if test="item.status !=null and item.status != -1">
? ? ? ? ? ? ? ? ? ? ? ? ?when id=#{item.id} then #{item.status}
? ? ? ? ? ? ? ? ? ? ?</if>
? ? ? ? ? ? ? ? ? ? ?<if test="item.status == null or item.status == -1">
? ? ? ? ? ? ? ? ? ? ? ? ?when id=#{item.id} then mydata_table.status//原數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ?</if>
? ? ? ? ? ? ? ? ?</foreach>
? ? ? ? ? ? </trim>
? ? ? ? </trim>
? ? ? ? where id in
? ? ? ? <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
? ? ? ? ? ? #{item.id,jdbcType=BIGINT}
? ? ? ? </foreach>
? ? </update>
這種批量跟心數(shù)據(jù)庫(kù)的方式可以在一次數(shù)據(jù)庫(kù)連接中更新所有數(shù)據(jù),避免了頻繁數(shù)據(jù)庫(kù)建立和斷開(kāi)連接的開(kāi)銷,可以很大程度的提高數(shù)據(jù)更新效率。
但是這樣的問(wèn)題是如果這個(gè)過(guò)程中更新出錯(cuò),將很難知道具體是哪個(gè)數(shù)據(jù)出錯(cuò),如果使用數(shù)據(jù)自身的事務(wù)保證,那么一旦出錯(cuò),所有的更新將自動(dòng)回滾。
而且通常這種方式也更容易出錯(cuò)。因此通常的使用的方案是進(jìn)行折中,也就是一次批量更新一部分(分頁(yè)進(jìn)行更新,比如說(shuō)一共有1000條數(shù)據(jù),一次更新100條)。
這樣可以分擔(dān)出錯(cuò)的概率,也更容易定位到出錯(cuò)的位置。
當(dāng)然如果數(shù)據(jù)量確實(shí)很大的時(shí)候,這種批量更新也一樣會(huì)導(dǎo)致更新效率低下(比如說(shuō)一次更新100條,那如果10億條數(shù)據(jù)呢,一樣要批量更新1000萬(wàn)次,建立和斷開(kāi)1000萬(wàn)次數(shù)據(jù)庫(kù),這個(gè)效率是無(wú)法承受的)。
這時(shí)候也許只能考慮其他方案了,比如引入緩存機(jī)制等。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持html5模板網(wǎng)。