詳解mybatis批量插入10萬條數(shù)據(jù)的優(yōu)化過程
數(shù)據(jù)庫 在使用mybatis插入大量數(shù)據(jù)的時(shí)候,為了提高效率,放棄循環(huán)插入,改為批量插入,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);}
實(shí)體類:
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)); }}
為了復(fù)現(xiàn)bug,我限制了JVM內(nèi)存:
執(zhí)行測試類報(bào)錯(cuò)如下:
java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.Arrays.copyOf(Arrays.java:3746)
可以看到,Arrays在申請內(nèi)存的時(shí)候,導(dǎo)致棧內(nèi)存溢出
改進(jìn)方法,分批新增:
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流表達(dá)式,skip表示跳過前i*10000條記錄,limit表示讀取當(dāng)前流的前10000條記錄 testMapper.testBatchInsert(list.stream().skip(i*10000).limit(10000).collect(Collectors.toList()));} }}
還有一種方法是調(diào)高JVM內(nèi)存,不過不建議使用,不僅吃內(nèi)存,而且數(shù)據(jù)量過大會導(dǎo)致sql過長報(bào)錯(cuò)
到此這篇關(guān)于詳解mybatis批量插入10萬條數(shù)據(jù)的優(yōu)化過程的文章就介紹到這了,更多相關(guān)mybatis批量插入10萬數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 用腳本和查詢主動(dòng)監(jiān)視Oracle 9i性能2. DB2數(shù)據(jù)庫更新執(zhí)行計(jì)劃的幾個(gè)常見的方法3. 實(shí)例講解Oracle數(shù)據(jù)庫自動(dòng)增加ID的sql4. 全面解析IBM DB2 9中的查詢優(yōu)化新特性5. SQL Server 2005日志文件損壞的處理方法6. IBM DB2 Connect簡介(1)7. Mysql入門系列:MYSQL創(chuàng)建、刪除和選擇數(shù)據(jù)庫8. DB2 9的九大新特性9. MySQL如何解決幻讀問題10. sql server刪除數(shù)據(jù)庫文件的方法
