2022年9月8日 作者 zeroheart

mybatis批量插入慢

1.批量插入的代码

public void batchInsertAll(List<Video> list) {
        int pointsDataLimit = 2000;
        int listSize = list.size();
        int maxSize = listSize - 1;
        List<Video> newList = new ArrayList<Video>();// 新建一个载体list
        StopWatch sw = new StopWatch();
        int j = 1;
        for (int i = 0; i < listSize; i++) {
            // 分批次处理
            newList.add(list.get(i));// 循环将数据填入载体list
            if (pointsDataLimit == newList.size() || i == maxSize) { // 载体list达到要求,进行批量操作
                sw.start("视频测试数据批次:" + j++);
                // 调用批量插入
                this.batchInsert(newList);
                newList.clear();// 每次批量操作后,清空载体list,等待下次的数据填入
                sw.stop();
            }
        }
        System.out.println(sw.prettyPrint());
    }
private void batchInsert(List<Video> list) {
        // 可以执行批量操作的sqlSession
        SqlSession openSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        VideoCustomMapper videoMapper = openSession.getMapper(VideoCustomMapper.class);
        try {
            list.forEach(video -> {
                videoMapper.insertNotWithId(video);
            });
            openSession.commit();
            openSession.clearCache();
        } catch (Exception e) {
            openSession.rollback();
        } finally {
            openSession.close();
        }
    }

2.插入慢的原因及处理

2.1 通过xrebel可以看出来,如果有以下代码返回id,会导致插入很慢,此场景下去除

<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
  SELECT LAST_INSERT_ID()
</selectKey>

2.2 链接参数,增加
@rewriteBatchedStatements=true