亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis批量插入數(shù)據(jù)的兩種方式總結(jié)與對比

 更新時間:2023年01月30日 09:18:26   作者:Neo?Yang  
批量插入功能是我們?nèi)粘9ぷ髦斜容^常見的業(yè)務(wù)功能之一,下面這篇文章主要給大家介紹了關(guān)于Mybatis批量插入數(shù)據(jù)的兩種方式總結(jié)與對比的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

總體描述

軟件開發(fā)過程中需要批量插入數(shù)據(jù)的場景有幾種:

  • 從離線文件(excel, csv等)導(dǎo)入大批量數(shù)據(jù)到系統(tǒng)。
  • 從其它系統(tǒng)定時或者人工同步大批量數(shù)據(jù)到系統(tǒng)。
  • 程序自身的某些算法執(zhí)行時會生成大批量數(shù)據(jù)保存到數(shù)據(jù)庫。

上面這些場景都是長時間的處理過程,在軟件設(shè)計(jì)時需要將其設(shè)計(jì)成帶進(jìn)度展示的異步任務(wù)(同步任務(wù)微服務(wù)有http請求超時的風(fēng)險)。異步任務(wù)可以使用消息框架。

使用批量插入技術(shù)能提升數(shù)據(jù)持久化的性能。用mybatis有兩種批量插入數(shù)據(jù)的方式可選:1. 拼接批量插入多條數(shù)據(jù)的SQL. 2. 使用Batch Insert技術(shù)。

方式一:拼接插入多條數(shù)據(jù)的SQL

mapper接口代碼

    /**
     * 插入數(shù)據(jù)列表
     *
     * @param dataList 數(shù)據(jù)列表
     */
    void insertDataList(@Param("list") List<BatchData> dataList);

XML文件配置

    <insert id="batchInsertData" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO t_batch_data (
            column1,
            column2,
            column3,
            column4,
            column5,
            column6,
            column7,
            column8,
            column9,
            column10
        ) VALUES
        <foreach item="data" collection="list" separator=",">
            (
                #{data.column1},
                #{data.column2},
                #{data.column3},
                #{data.column4},
                #{data.column5},
                #{data.column6},
                #{data.column7},
                #{data.column8},
                #{data.column9},
                #{data.column10}
            )
        </foreach>
    </insert>

可以看到,XML配置文件使用 foreach 對多條數(shù)據(jù)做了拼接,Value部分用逗號分隔。拼接后的SQL樣式:

INSERT INTO t_batch_data (
            column1,
            column2,
            column3,
            column4,
            column5,
            column6,
            column7,
            column8,
            column9,
            column10
        ) VALUES
          
            (
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?
            )
         , 
            (
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?
            )
         , 
            (
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?,
                ?
            )

可以看到,拼接的SQL長度跟批量插入數(shù)據(jù)的條數(shù)和單條數(shù)據(jù)的字段數(shù)相關(guān)。對于像postgres這樣限定了參數(shù)個數(shù)的數(shù)據(jù)庫,需要提前對大批量數(shù)據(jù)做拆分處理。

下面的示例代碼對批量數(shù)據(jù)按200條一組做拆分,然后再入庫。

    public long foreachBatchInsert(@PathVariable("amount") int amount) {
        long beginTime = System.currentTimeMillis();
        List<BatchData> dataList = buildDataList(amount);

        // 大數(shù)據(jù)分批處理入庫
        List<List<BatchData>> dataGroup = ListUtil.splitList(dataList, 200);
        for (List<BatchData> group : dataGroup) {
            batchInsertMapper.insertDataList(group);
        }

        return System.currentTimeMillis() - beginTime;
    }

方式二: 使用Batch Insert技術(shù)

Mapper接口代碼

    /**
     * 插入單條數(shù)據(jù)
     *
     * @param data PO數(shù)據(jù)
     */
    void insertData(@Param("data") BatchData data);

XML文件配置

    <insert id="insertData" useGeneratedKeys="true" keyProperty="data.id" keyColumn="id">
        INSERT INTO t_batch_data (
            column1,
            column2,
            column3,
            column4,
            column5,
            column6,
            column7,
            column8,
            column9,
            column10
        ) VALUES (
            #{data.column1},
            #{data.column2},
            #{data.column3},
            #{data.column4},
            #{data.column5},
            #{data.column6},
            #{data.column7},
            #{data.column8},
            #{data.column9},
            #{data.column10}
        )
    </insert>

映射實(shí)例接口和SQL代碼與插入單個對象無異。關(guān)鍵代碼在應(yīng)用層。

應(yīng)用層代碼

    public long mybatisBatchInsert(@PathVariable("amount") int amount) {
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        long beginTime = System.currentTimeMillis();

        try {
            BatchInsertMapper insertMapper = session.getMapper(BatchInsertMapper.class);

            List<BatchData> dataList = buildDataList(amount);
            for (BatchData data : dataList) {
                insertMapper.insertData(data);
            }

            session.commit();
            session.clearCache();
        } catch (Exception e) {
            session.rollback();
        } finally {
            session.close();
        }

        return System.currentTimeMillis() - beginTime;
    }

查看打印出執(zhí)行的SQL語句:

INSERT INTO t_batch_data (
            column1,
            column2,
            column3,
            column4,
            column5,
            column6,
            column7,
            column8,
            column9,
            column10
        ) VALUES (
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?,
            ?
        )

攔截StatementHandler的prepare執(zhí)行方法,可以看到只執(zhí)行了一次預(yù)編譯。批量插入不會出現(xiàn)參數(shù)個數(shù)超限或者SQL語句超長的問題。

對比分析

性能對比

在postgres數(shù)據(jù)庫中新建了一個包含10個text類型字段的表(t_batch_data)驗(yàn)證了一下,插入20萬條數(shù)據(jù)時間都在15秒左右,相差不大。方案1必須做分組(參數(shù)個數(shù)超過限制);方案二本身是調(diào)用的mapper的插入單個對象的接口, 不需要做分批。

應(yīng)用場景分析

如表字段是固定的,字段數(shù)量也不大可以使用方案一;如表字段數(shù)量不固定(元數(shù)據(jù)驅(qū)動)推薦使用第二種方案。第二種方案在代碼執(zhí)行到session.commit()時數(shù)據(jù)才真正入庫,如果在這之前使用數(shù)據(jù)庫的數(shù)據(jù)或者回填的自增ID是有問題的。

實(shí)際產(chǎn)品開發(fā)過程中,即使采用第二種方案也建議對大數(shù)量做分組處理,將單次操作數(shù)據(jù)庫的時間控制在2秒以內(nèi)。

Demo代碼地址: https://github.com/ylforever/elon-postgres.git

總結(jié)

到此這篇關(guān)于Mybatis批量插入數(shù)據(jù)的兩種方式總結(jié)與對比的文章就介紹到這了,更多相關(guān)Mybatis批量插入數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論