欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中六種批量更新Mysql的方式效率對(duì)比分析

 更新時(shí)間:2025年07月15日 16:03:14   作者:九轉(zhuǎn)成圣  
文章比較了MySQL大數(shù)據(jù)量批量更新的多種方法,指出REPLACE INTO和ON DUPLICATE KEY效率最高但存在數(shù)據(jù)風(fēng)險(xiǎn),MyBatis-Plus分批更新較優(yōu),CASE WHEN因循環(huán)多效率較低,建議根據(jù)項(xiàng)目限制選擇合適方案并分批處理以避免SQL阻塞

先上結(jié)論吧,有空可以自測(cè)一下,數(shù)據(jù)量大時(shí)運(yùn)行一次還時(shí)挺耗時(shí)的

效率比較

小數(shù)據(jù)量時(shí)6中批量更新效率不太明顯,根據(jù)項(xiàng)目選擇合適的即可,以1萬(wàn)條為準(zhǔn)做個(gè)效率比較,效率從高到低一次排名如下

  1. replace intoON DUPLICATE KEY效率最高
  2. mybatis-plus 有取巧嫌疑,因?yàn)槭欠峙扛?其他幾種都是一次更新
  3. for循環(huán)憑借sql和JdbcTemplate相近,即使5萬(wàn)條,10萬(wàn)條效率也相近
  4. case when

然而有時(shí)候我們只能選擇case when,因?yàn)?code>replace into和ON DUPLICATE KEY公司不一定讓用,項(xiàng)目也不一定引入mybatis-plus,數(shù)據(jù)庫(kù)url中也不一定有allowMultiQueries=true參數(shù),算是一個(gè)兜底方案吧,不管用那種方式大數(shù)據(jù)量時(shí)都需要考慮分批

測(cè)試結(jié)構(gòu)

環(huán)境信息:mysql-8.0.35-winx64,本地win 10

依次為測(cè)試次數(shù)-平均耗時(shí)-最小耗時(shí)-最大耗時(shí),單位為毫秒

數(shù)據(jù)量forcase whenreplace intoON DUPLICATE KEYmybatis-plusJdbcTemplate
500100-61-41-1202100-66-57-426100-16-10-282100-15-10-293100-73-52-564100-87-59-1449
1000100-131-94-2018100-241-219-675100-28-18-376100-25-17-331100-117-98-599100-188-136-2397
5000100-852-735-8297100-11219-10365-13496100-95-83-569100-93-82-552100-618-517-1415100-1161-911-9334
1000010-3957-2370-1730410-45537-44465-48119100-191-171-762100-188-169-772100-1309-1085-5021100-3671-2563-31112
5000010-50106-34568-130651卡死不動(dòng)100-1026-919-1868100-1062-945-1934100-8062-6711-20841100-48744-35482-191011
10000010-160170-106223-264434卡死不動(dòng)10-2551-2292-368810-2503-2173-3579100-17205-14436-2488110-169771-110522-343278

心得:

sql語(yǔ)句for循環(huán)效率其實(shí)相當(dāng)高的,因?yàn)樗鼉H僅有一個(gè)循環(huán)體,只不過(guò)最后update語(yǔ)句比較多,量大了就有可能造成sql阻塞,同時(shí)在mysql的url上需要加上allowMultiQueries=true參數(shù),即 jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true(公司項(xiàng)目不一定加,我們也不一定有權(quán)限加)。

case when雖然最后只會(huì)有一條更新語(yǔ)句,但是xml中的循環(huán)體有點(diǎn)多,每一個(gè)case when 都要循環(huán)一遍list集合,所以大批量拼sql的時(shí)候會(huì)比較慢,所以效率問(wèn)題嚴(yán)重。使用的時(shí)候建議分批插入(我們公司一直用的就是這種,但是必須分批)。

duplicate key update可以看出來(lái)是最快的,但是公司一般都禁止使用replace into和INSERT INTO … ON DUPLICATE KEY UPDATE,這種sql有可能會(huì)造成數(shù)據(jù)丟失和主從上表的自增id值不一致。而且用這個(gè)更新時(shí),記得一定要加上id,而且values()括號(hào)里面放的是數(shù)據(jù)庫(kù)字段,不是java對(duì)象的屬性字段

根據(jù)效率,安全方面綜合考慮,選擇適合的很重要。

數(shù)據(jù)庫(kù)

CREATE TABLE `people` (
  `id` bigint(8) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(50) NOT NULL DEFAULT '',
  `last_name` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

初始化測(cè)試數(shù)據(jù)

//初始化10w數(shù)據(jù)
@Test
void init10wData() {
    for (int i = 0; i < 100000; i++) {
        People people = new People();
        people.setFirstName(UUID.randomUUID().toString());
        people.setLastName(UUID.randomUUID().toString());
        peopleDAO.insert(people);
    }
}

批量修改方案

第一種 for

<!-- 批量更新第一種方法,通過(guò)接收傳進(jìn)來(lái)的參數(shù)list進(jìn)行循環(huán)組裝sql -->
<update id="updateBatch" parameterType="java.util.List">
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update people
        <set>
            <if test="item.firstName != null">
                first_name = #{item.firstName,jdbcType=VARCHAR},
            </if>
            <if test="item.lastName != null">
                last_name = #{item.lastName,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{item.id,jdbcType=BIGINT}
    </foreach>
</update>

第二種 case when

<!-- 批量更新第二種方法,通過(guò) case when語(yǔ)句變相的進(jìn)行批量更新 -->
<update id="updateBatch2" parameterType="java.util.List">
    update people
    <set>
        <foreach collection="list" item="item">
            <if test="item.firstName != null">
                first_name = case when id = #{item.id} then #{item.firstName} else first_name end,
            </if>
            <if test="item.lastName != null">
                last_name = case when id = #{item.id} then #{item.lastName} else last_name end,
            </if>
        </foreach>
    </set>
    where id in
    <foreach collection="list" item="item" separator="," open="(" close=")">
        #{item.id}
    </foreach>
</update>

第三種 replace into

<!-- 批量更新第三種方法,通過(guò) replace into  -->
<update id="updateBatch3" parameterType="java.util.List">
    replace into people
    (id,first_name,last_name) values
    <foreach collection="list" index="index" item="item" separator=",">
        (#{item.id},
        #{item.firstName},
        #{item.lastName})
    </foreach>
</update>

第四種 ON DUPLICATE KEY UPDATE

<!-- 批量更新第四種方法,通過(guò) duplicate key update  -->
<update id="updateBatch4" parameterType="java.util.List">
    insert into people
    (id,first_name,last_name) values
    <foreach collection="list" index="index" item="item" separator=",">
        (#{item.id},
        #{item.firstName},
        #{item.lastName})
    </foreach>
    ON DUPLICATE KEY UPDATE
    id=values(id),first_name=values(first_name),last_name=values(last_name)
</update>

第五種mybatis-plus提供的的批量更新

default boolean updateBatchById(Collection<T> entityList) {
    return this.updateBatchById(entityList, 1000);
}
boolean updateBatchById(Collection<T> entityList, int batchSize);

mybatis-plus提供的批量更新是分批批量更新,默認(rèn)每批1000條,可以指定分批的條數(shù),每批執(zhí)行完成后提交一下事務(wù),不加@Transactional可能會(huì)出現(xiàn)第一批更新成功了,第二批更新失敗了的情況.

第六種JdbcTemplate提供的批量更新

測(cè)試代碼

/**
 * PeopleDAO繼承基類(lèi)
 */
@Mapper
@Repository
public interface PeopleDAO extends MyBatisBaseDao<People, Long> {

    void updateBatch(@Param("list") List<People> list);

    void updateBatch2(List<People> list);

    void updateBatch3(List<People> list);

    void updateBatch4(List<People> list);
}

@SpringBootTest
class PeopleMapperTest {
    @Resource
    PeopleMapper peopleMapper;
    @Resource
    PeopleService peopleService;
    @Resource
    JdbcTemplate jdbcTemplate;

    @Test
    void init10wData() {
        for (int i = 0; i < 100000; i++) {
            People people = new People();
            people.setFirstName(UUID.randomUUID().toString());
            people.setLastName(UUID.randomUUID().toString());
            peopleMapper.insert(people);
        }
    }


    @Test
    void updateBatch() {
        List<People> list = new ArrayList();
        int loop = 100;
        int count = 5000;
        Long maxCost = 0L;//最長(zhǎng)耗時(shí)
        Long minCost = Long.valueOf(Integer.MAX_VALUE);//最短耗時(shí)
        for (int j = 0; j < count; j++) {
            People people = new People();
            people.setId(ThreadLocalRandom.current().nextInt(0, 100000));
            people.setFirstName(UUID.randomUUID().toString());
            people.setLastName(UUID.randomUUID().toString());
            list.add(people);
        }

        Long startTime = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            Long curStartTime = System.currentTimeMillis();
            // peopleMapper.updateBatch4(list);
            // peopleService.updateBatchById(list);
            jdbcTemplateBatchUpdate(list);
            Long curCostTime = System.currentTimeMillis() - curStartTime;
            if (maxCost < curCostTime) {
                maxCost = curCostTime;
            }
            if (minCost > curCostTime) {
                minCost = curCostTime;
            }
        }
        System.out.println(loop + "-" + (System.currentTimeMillis() - startTime) / loop + "-" + minCost + "-" + maxCost );
    }

    private void jdbcTemplateBatchUpdate (List<People> list){
        String sql = "update people set first_name=?,last_name=? where id = ?";
        List<Object[]> params = list.stream().map(item -> new Object[]{item.getFirstName(), item.getLastName(), item.getId()}).collect(Collectors.toList());
        jdbcTemplate.batchUpdate(sql,params);
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論