最近在实现大数据量批量入库时采用mybatis-plus的saveBatch
发现执行效率非常慢,当我测试插入10万条数据时,测试用例跑了半个小时,如果是每次执行insert
效率确实会比较慢,但saveBatch
是用于批量新增,在批量新增上效率应该是较快的,但跟insert
的速度相同,通过控制台的SQL打印发现了批量新增没有生效,预期的批量新增执行的SQL应该是:
insert into t_student (id, name,address) values(1,'zs','北京'),(2,'ls','上海');
但控制台的SQL却是:
insert into t_student (id, name,address) values(1,'zs','北京');
insert into t_student (id, name,address) values(2,'ls','上海');
这是因为批量新增没有生效,还是采用默认的单次新增,这样每次新增时都会进行数据库的连接和释放,非常影响效率,解决的方法就是在JDBC的url参数增加:rewriteBatchedStatements=true
这样就能在批量新增时进行一次数据库的连接和释放。
url: jdbc:mysql://localhost:33306/student?userSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&allowMultiQueries=true&rewriteBatchedStatements=true