mybatis批量插入10万条数据的优化过程

Mybatis-Plus 专栏收录该内容
40 篇文章 2 订阅

在使用mybatis插入大量数据的时候,为了提高效率,放弃循环插入,改为批量插入,mapper如下:

package com.lcy.service.mapper;
 
import com.lcy.service.pojo.TestVO;
import org.apache.ibatis.annotations.Insert;
 
import java.util.List;
 
/**
 * 功能描述:
 *
 * @author liuchaoyong
 * @version 1.0
 * @date 2019-06-26 13:18
 */
public interface TestMapper {
 
    @Insert("<script>" +
            "insert into test (t1,t2,t3,t4,t5) values " +
            "<foreach collection='list' item='obj' separator=','>" +
            "(#{obj.t1},#{obj.t2},#{obj.t3},#{obj.t4},#{obj.t5})" +
            "</foreach>" +
            "</script>")
    Integer testBatchInsert(List<TestVO> list);
 
}

实体类:

package com.lcy.service.pojo;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
/**
 * 功能描述:
 *
 * @author liuchaoyong
 * @version 1.0
 * @date 2019-06-26 13:20
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestVO {
 
    private String t1;
 
    private String t2;
 
    private String t3;
 
    private String t4;
 
    private String t5;
 
}

测试类如下:

import com.lcy.service.TestApplication;
import com.lcy.service.mapper.TestMapper;
import com.lcy.service.pojo.TestVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 功能描述:
 *
 * @author liuchaoyong
 * @version 1.0
 * @date 2019-05-17 16:42
 */
@SpringBootTest(classes = TestApplication.class)
@RunWith(SpringRunner.class)
public class TestDemo {
 
    @Autowired
    private TestMapper testMapper;
 
    @Test
    public void insert() {
        List<TestVO> list = new ArrayList<>();
        for (int i = 0; i < 200000; i++) {
            list.add(new TestVO(i + "," + i, i + "," + i, i + "," + i, i + "," + i, i + "," + i));
        }
        System.out.println(testMapper.testBatchInsert(list));
    }
 
}

为了复现bug,我限制了JVM内存:

在这里插入图片描述
执行测试类报错如下:

java.lang.OutOfMemoryError: Java heap space
 
	at java.base/java.util.Arrays.copyOf(Arrays.java:3746)

可以看到,Arrays在申请内存的时候,导致栈内存溢出

改进方法,分批新增:

import com.lcy.service.TestApplication;
import com.lcy.service.mapper.TestMapper;
import com.lcy.service.pojo.TestVO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 功能描述:
 *
 * @author liuchaoyong
 * @version 1.0
 * @date 2019-05-17 16:42
 */
@SpringBootTest(classes = TestApplication.class)
@RunWith(SpringRunner.class)
public class TestDemo {
 
    @Autowired
    private TestMapper testMapper;
 
    @Test
    public void insert() {
        List<TestVO> list = new ArrayList<>();
        for (int i = 0; i < 200000; i++) {
            list.add(new TestVO(i + "," + i, i + "," + i, i + "," + i, i + "," + i, i + "," + i));
        }
        int index = list.size() / 10000;
        for (int i=0;i< index;i++){
            //stream流表达式,skip表示跳过前i*10000条记录,limit表示读取当前流的前10000条记录
            testMapper.testBatchInsert(list.stream().skip(i*10000).limit(10000).collect(Collectors.toList()));
        }
    }
 
}

有一种方法是调高JVM内存,不过不建议使用,不仅吃内存,而且数据量过大会导致sql过长报错

  • 3
    点赞
  • 1
    评论
  • 3
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值