JDBC批量插入性能我需要在mysql数据库中插入几亿条记录。我一次批量插入100万。请参阅下面的代码。这看起来很慢。有没有办法优化它?try {
// Disable auto-commit
connection.setAutoCommit(false);
// Create a prepared statement
String sql = "INSERT INTO mytable (xxx), VALUES(?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
Object[] vals=set.toArray();
for (int i=0; i<vals.length; i++) {
pstmt.setString(1, vals[i].toString());
pstmt.addBatch();
}
// Execute the batch
int [] updateCounts = pstmt.executeBatch();
System.out.append("inserted "+updateCounts.length);
3 回答
当年话下
TA贡献1890条经验 获得超9个赞
我有一个与mysql类似的性能问题,并通过在连接url中设置useServerPrepStmts和rewriteBatchedStatements属性来解决它。
Connection c = DriverManager.getConnection("jdbc:mysql://host:3306/db?useServerPrepStmts=false&rewriteBatchedStatements=true", "username", "password");
添加回答
举报
0/150
提交
取消