为了账号安全,请及时绑定邮箱和手机立即绑定

JDBC批量插入性能

JDBC批量插入性能

绝地无双 2019-08-06 17:13:48
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中设置useServerPrepStmtsrewriteBatchedStatements属性来解决它。

Connection c = DriverManager.getConnection("jdbc:mysql://host:3306/db?useServerPrepStmts=false&rewriteBatchedStatements=true", "username", "password");


查看完整回答
反对 回复 2019-08-06
  • 3 回答
  • 0 关注
  • 838 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信