博客 数据库批量插入数据的三种方法

数据库批量插入数据的三种方法

   数栈君   发表于 2023-08-18 10:29  948  0

一、准备工作

测试环境:SpringBoot项目+MybatisPlus框架+MySQL数据库+Lombok

二、导入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.5.3</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.20</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.22</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.1</version>
</dependency>

三、yml配置文件

server:
    port: 8081

spring:
    datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/mysql?useSSL=false&characterEncoding=utf-8&useUnicode=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true
        ##rewriteBatchedStatements=true 开启批处理模式

四、实体类(数据库表要有对应字段)

package com.cy.model;

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

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    private Integer id;

    private String username;

    private String password;

}

五、测试

方法一:for循环插入(单条)(总耗时:n分钟,每次都要获取连接Connection、释放连接和关闭资源等操作,比较耗时,这里就没测了)
@RequestMapping("/test3") //单个插入(慢)
public String test3(){
    long startTime=System.currentTimeMillis();
    for(int i=0;i<100000;i++){
        userService.save(new User(i+1,"张三","123456"));
    }
    long endTime=System.currentTimeMillis();
    long result=endTime-startTime;
    return "总耗时:"+(result/1000);
}

方法二:批量插入saveBatch(4~7秒,这里用到了MybatisPLus的saveBatch批量插入方法,实际也是for循环单条插入,只不过它是利用分片处理batchSize=1000和分批提交事务,从而提高了性能,不用在Connection上消费性能了)(推荐)
@RequestMapping("/test2") //批量插入saveBatch(分片处理batchSize=1000,分批提交事务) (次快)
public String test2(){
    List<User> userList=new ArrayList<>();
    long startTime=System.currentTimeMillis();
    for(int i=0;i<100000;i++){
        userList.add(new User(i+1,"张三","123456"));
    }
    userService.saveBatch(userList);
    long endTime=System.currentTimeMillis();
    long result=endTime-startTime;
    return "总耗时:"+(result/1000);
}

方法三:循环插入+开启批处理模式(4~7秒,开启批处理模式,关闭自动提交事务,共同用一个sqlsession,单个插入性能得到提升,由于用同一个sqlsession,极大的减少了对资源操作和对事务处理的时间,很好地提高了性能)(推荐)
@Resource
private SqlSessionFactory sqlSessionFactory;
@RequestMapping("/test1") //批量插入(最快)---循环插入+批处理模式 ( 开启批处理模式 rewriteBatchedStatements=true )
public String test1(){
    //开启批处理模式,关闭自动提交事务
    SqlSession sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH,false);
    long startTime=System.currentTimeMillis();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    for(int i=0;i<100000;i++){
        userMapper.insert(new User(i+1,"张三","123456"));
    }
    sqlSession.commit();//一次性提交事务
    sqlSession.close();//关闭资源
    long endTime=System.currentTimeMillis();
    long result=endTime-startTime;
    return "总耗时:"+(result/1000);
}

以上就是全部内容,希望对大家有帮助!


免责申明:


本文系转载,版权归原作者所有,如若侵权请联系我们进行删除!

《数据治理行业实践白皮书》下载地址:https://fs80.cn/4w2atu

《数栈V6.0产品白皮书》下载地址:
https://fs80.cn/cw0iw1

想了解或咨询更多有关袋鼠云大数据产品、行业解决方案、客户案例的朋友,浏览袋鼠云官网:
https://www.dtstack.com/?src=bbs

同时,欢迎对大数据开源项目有兴趣的同学加入「袋鼠云开源框架钉钉技术群」,交流最新开源技术信息,群号码:30537511,项目地址:
https://github.com/DTStack

0条评论
社区公告
  • 大数据领域最专业的产品&技术交流社区,专注于探讨与分享大数据领域有趣又火热的信息,专业又专注的数据人园地

最新活动更多
微信扫码获取数字化转型资料
钉钉扫码加入技术交流群