当我使用mybatis将100000条记录插入到mysql中的表中时,1.当我在windows(我的pc,16G+i7)中运行应用程序(springboot+mybatis)时需要大约14s,2.但当我在centos7 (产品环境,4Core+8G ECS Server)中运行相同的应用程序时需要1244s。
它们都连接到相同的mysql服务器(也在centos7上运行)。在centos7 (产品环境)中网络连接较好。CPU性能几乎相同(我已经测试过了)。该应用程序简单,运行时只需1G内存。
我的应用程序中的库版本: openjdk版本"1.8.0_212",Spring boot 2.1.6,spring-boot-starter-tomcat-2.1.6.RELEASE.jar,spring-jdbc-5.0.7.RELEASE.jar,druid 1.1.19.jar,mybatis-3.5.2.jar,mybatis-spring-2.0.2.jar,mybatis-spring-boot-starter-2.1.0.jar,mybatis-spring-boot-autoconfigure-2.1.0.jar,mysql-connector-java-5.1.38.jar,
有人知道原因吗?提前谢谢。
==
由Foreach插入(max_allowed_packet已设置为200M):
<insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="false">
insert into table_product
(id,
code,
status,
type,
create_time,
update_time)
values
<foreach collection="products" item="product" index="index" separator=",">
(#{product.id},
#{product.code},
#{product.status},
#{product.type},
#{product.createTime},
#{product.updateTime})
</foreach>
</insert>==
由ExecutorType.BATCH插入:
public void batchInsert(List<Product> products){
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
BatchTableDao batchTableDao = session.getMapper(BatchTableDao.class);
try {
int i=0;
for (Product product : products) {
batchTableDao.insert(product);
if (i % 1000 == 0 || i == products.size()-1) {
session.flushStatements();
session.clearCache();
}
i++;
}
session.commit();
} catch (Exception e) {
Log.warn("error : "+e.getMessage());
} finally{
session.close();
}
}==
在Windows10中,使用'foreach‘插入100000行大约需要14秒。使用'ExecutorType.BATCH‘插入100000行大约需要2500秒,这太慢了,无法接受。
发布于 2019-08-16 15:24:49
首先,如何更改为the official document中指定的版本?我认为你需要了解更多关于代码的知识。
https://stackoverflow.com/questions/57519531
复制相似问题