我有一段代码在数据库上执行大约 500,000 次插入。它现在在每次迭代时调用 PreparedStatement 的 executeUpdate 的循环中完成。将所有 500,000 个插入添加到批处理中并仅调用一次 executeBatch 会更快吗?
最佳答案
将 PreparedStatement 与批量更新工具结合使用会产生最有效的结果(来自 Sun JDBC doc ):
// turn off autocommit
con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement(
"INSERT INTO employees VALUES (?, ?)");
stmt.setInt(1, 2000);
stmt.setString(2, "Kelly Kaufmann");
stmt.addBatch();
stmt.setInt(1, 3000);
stmt.setString(2, "Bill Barnes");
stmt.addBatch();
// submit the batch for execution
int[] updateCounts = stmt.executeBatch();
关于java - 什么时候在 JDBC 上使用批处理操作更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1289476/