@Test
public void testInsert() {
Connection con = null;
PreparedStatement ps = null;
try {
con = GetConnection();
String sql = "insert into goods(name) value(?)";
ps = con.prepareStatement(sql);
long start = System.currentTimeMillis();
for (int i = 0; i <= 20000; i++) {
ps.setObject(1,"name" + i);
ps.addBatch();
if (i % 500 == 0){
ps.executeBatch();
ps.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("花费的时间为:" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseResource(con,ps);
}
}
起初没有使用addBatch、executeBatch方法进行批量插入,使用之后时间也几乎和没使用一样。通过查找资料说是rewriteBatchedStatements没有设置为True,但设置之后并没有任何效果,也有说useServerPrepStmts要设置成false,但也没什么用。
我使用的是8.0.24的,是不是软件包的问题?
数据量小,效果不明显吧,你加大插入的次数看看。
执行速度跟电脑性能也有关系。
破案了嘉人们。insert into goods value这里少写了个s,代码不报错,但是插入还是一条一条的插
你可以参考这篇文章https://blog.csdn.net/weixin_34126557/article/details/93471972
另外时间比较小的话,其实你加上一个休眠的线程,让它执行慢一点就可以了,比如使用sleep(2000),让它开始后休息2秒在执行