iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解mybatis批量插入10万条数据的优化过程
  • 568
分享到

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

2024-04-02 19:04:59 568人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

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

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


package com.lcy.service.mapper;

import com.lcy.service.pojo.TestVO;
import org.apache.ibatis.annotations.Insert;

import java.util.List;

public interface TestMapper {

    @Insert("")
    Integer testBatchInsert(List list);
}

实体类:


package com.lcy.service.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@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;

@SpringBootTest(classes = TestApplication.class)
@RunWith(SpringRunner.class)
public class TestDemo {

    @Autowired
    private TestMapper testMapper;

    @Test
    public void insert() {
        List 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;

@SpringBootTest(classes = TestApplication.class)
@RunWith(SpringRunner.class)
public class TestDemo {

    @Autowired
    private TestMapper testMapper;

    @Test
    public void insert() {
        List 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过长报错

到此这篇关于详解mybatis批量插入10万条数据的优化过程的文章就介绍到这了,更多相关mybatis批量插入10万数据内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解mybatis批量插入10万条数据的优化过程

本文链接: https://www.lsjlt.com/news/123728.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作