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
提交
取消