亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

詳解mybatis批量插入10萬條數據的優化過程

瀏覽:4日期:2023-10-19 13:45:35

數據庫 在使用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@AllArgsConstructorpublic 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內存:

詳解mybatis批量插入10萬條數據的優化過程

執行測試類報錯如下:

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萬數據內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 8x华人永久免费 | 丝袜美腿视频一区二区三区 | 日韩国产成人 | jiz欧美高清 | 在线播放国产真实女同事 | 欧美在线观看视频免费 | 成人a视频在线观看 | 成人国产日本亚洲精品 | 欧美色视频日本片高清在线观看 | 美女视频一区二区 | 国产精品揄拍100视频最近 | 成人免费大片黄在线观看com | 国产精品www视频免费看 | 五月国产综合视频在线观看 | 国产成年女一区二区三区 | 久久h| 欧美激情片网站 | 国产成人免费无庶挡视频 | 国产精品福利在线观看 | 日本一级特黄aa毛片免费观看 | 国产精品成人一区二区不卡 | 深夜你懂的在线网址入口 | 亚洲欧美日韩网站 | 一级毛片不卡免费看老司机 | 91精品国产综合久久婷婷 | 中文字幕在线观 | 国产精品久久久久久久久电影网 | 欧美日韩精品一区二区三区不卡 | 国产裸舞凸点福利小视频 | 国产成人精品亚洲午夜麻豆 | 日韩精品另类天天更新影院 | 国产黄色片网站 | 欧美超级碰碰 | 插入综合网 | 日本三级黄色 | 一本大道一卡二卡入口2021 | 99久久精品国产自免费 | 青青在线精品2018国产 | 亚洲成人黄色在线观看 | 国产成人久久精品激情91 | 欧美日韩亚洲第一页 |