请问我做错了什么,我在mysql中执行了350个插入操作,但需要40秒才能完成。
以下是代码:
long t0 = System.currentTimeMillis();
Connection con = connectionProvider.getConnection();
PreparedStatement s = con.prepareStatement("insert into domkee.friends(idFriends,friend1Id,friend2Id,friend2Name) values(?,?,?,?)");
con.setAutoCommit(false);
for (Friend f : friends) {
s.setLong(1, 0);
s.setLong(2, f.getFriend1Id());
s.setLong(3, f.getFriend2Id());
s.setString(4, f.getFriend2Name());
s.addBatch();
}
long t1 = System.currentTimeMillis() - t0;
s.executeBatch();
long t2 = System.currentTimeMillis()-t0;
con.commit();
long t3 = System.currentTimeMillis()-t0;
s.close();
con.close();
long t4 = System.currentTimeMillis()-t0;
System.out.println(((double)t1/1000) + ";" + ((double)t2/1000) + ";" + ((double)t3/1000) + ";" + ((double)t4/1000));
以下是控制台输出:
0.156;39.251;39.376;39.486
执行.executeBatch()
需要大约40秒钟的时间,可能有什么问题呢?