如何使用executeBatch提高性能?

2023-12-20

我正在编写 Java 代码,用于将 45000 条记录插入表中 我正在使用以下代码:

//create Db Connection
List<String> sqlInsertQueries = getListOfInsertsQueries();
for (String currentsql : sqlInsertQueries)
{
  stmt.addBatch(currentsql);
}
stmt.executeBatch();
conn.commit();

这段代码很慢,差不多需要5分钟才能写完。

有什么办法让它工作得更快吗?


您应该确保自动提交为 false,并使用准备好的语句。 准备好的语句可用于插入和更新。

connection con.setAutoCommit(false);  
PreparedStatement prepStmt = con.prepareStatement("INSERT INTO table (val1,val2) VALUES (?,?)");

for all values:
  prepStmt.setString(1,val1);
  prepStmt.setString(2,val2);
  prepStmt.addBatch();    

stmt.executeBatch(); 
conn.commit(); 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用executeBatch提高性能? 的相关文章

随机推荐