MyBatis批量插入数据优化(3)
导读:MyBatis批量插入数据优化,mybatis-plus 的 saveBatch 方法 现在尝试 mybatis-plus 提供的 saveBatch 方法,期望它能够提高性能。 @Test public void MybatissaveBatch(){ SqlSession sqlSession = sqlSessionFactory.
MyBatis批量插入数据优化

- mybatis-plus 的 saveBatch 方法
现在尝试 mybatis-plus 提供的 saveBatch 方法,期望它能够提高性能。
@Test
public void MybatissaveBatch(){
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
List<Student> students = new ArrayList<>();
StopWatch stopWatch = new StopWatch();
stopWatch.start("mybatis plus save batch");
for (int i = 0; i < 100000; i++) {
Student student = new Student();
student.setStuid("6840"+i);
student.setName("zhangsan"+i);
student.setAge((i%100));
if(i%2==0){
student.setSex(0);
}else {
student.setSex(1);
}
student.setDept("计算机学院");
student.setAddress("广东省广州市番禺"+i+"号");
//一条一条插入
students.add(student);
}
studentService.saveBatch(students);
sqlSession.commit();
stopWatch.stop();
System.out.println("mybatis plus save batch:" + stopWatch.getTotalTimeMillis());
} finally {
sqlSession.close();
}
}
发现花费9204毫秒,比一条条插入数据性能提高十几倍

- 手动拼接 SQL:挑战传统的方式
<insert id="saveBatch2">
insert into springboot_mp.tb_student ( stuid, name, age, sex, dept, address)
values
<foreach collection="students" item="stu" index="index" separator=",">
( #{stu.stuid}, #{stu.name}, #{stu.age}, #{stu.sex}, #{stu.dept}, #{stu.address})
</foreach>
</insert>
发现花费10958毫秒,比一条条插入数据性能提高十几倍,但是和saveBatch性能相差不大

既然都验证都这了,我就在想,要不要使用JDBC批量插入进行验证一下,看会不会出现原始的才是最好的结果
- JDBC 的 executeBatch 方法
尝试直接使用 JDBC 提供的 executeBatch 方法,看是否有意外的性能提升。
@Test
public void JDBCSaveBatch() throws SQLException {
SqlSession sqlSession = sqlSessionFactory.openSession();
Connection connection = sqlSession.getConnection();
connection.setAutoCommit(false);
String sql ="insert into springboot_mp.tb_student ( stuid, name, age, sex, dept, address) values (?, ?, ?, ?, ?, ?);";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
List<Student> students = new ArrayList<>();
StopWatch stopWatch = new StopWatch();
stopWatch.start("mybatis plus JDBCSaveBatch");
for (int i = 0; i < 100000; i++) {
statement.setString(1,"6840"+i);
statement.setString(2,"zhangsan"+i);
statement.setInt(3,(i%100));
if(i%2==0){
statement.setInt(4,0);
}else {
statement.setInt(4,1);
}
statement.setString(5,"计算机学院");
statement.setString(6,"广东省广州市番禺"+i+"号");
statement.addBatch();
}
statement.executeBatch();
connection.commit();
stopWatch.stop();
System.out.println("mybatis plus JDBCSaveBatch:" + stopWatch.getTotalTimeMillis());
}
catch (Exception e){
System.out.println(e.getMessage());
}
finally {
sqlSession.close();
}
JDBC executeBatch 的性能会好点,耗费6667毫秒
相关阅读
-
如何创建一个网页前端 web创建一个简单网页教程
IT电脑小知识篇,关于如何创建一个网页前端和web创建一个简单网页教程的相关知识,接下来就是全面介绍。 首先我要说学习前端网页制作其实很简单! 今天我带着你踏入前端开发的大门,我
-
sql查询语句实例教程 sql查询语句大全及实例
最近在学习sql数据库教程的时候,顺便记录了一下笔记,相信很多人也在搜索: 常用的sql查询语句实例大全 ,经过我的学习实践总结,当然这是最基本的sql查询语句代码。 sql查询语句 SELECT
-
创建自己的个人网站怎么弄 网页设计制作网站
小编为大家讲一讲创建自己的个人网站怎么弄和网页设计制作网站的方法内容,很不错的方法小知识,建议收藏哦! 随着个人创业的流行,很多个人也需要一个比较详细的网站来展示自己,开
-
多租户SaaS平台的数据库方案详解
正文核心导读:多租户SaaS平台的数据库方案详解的教程内容,下面来一起了解一下吧。 随着云计算和SaaS(Software as a Service)模型的兴起,多租户系统成为了构建灵活、高效应用的重要架构。


