3

不知何故,我的 MySQL 插入真的很慢。即使我使用“批量插入”。大约 50 次插入最多需要 2000 毫秒。这是正常的还是有办法改进我的代码?

            String sql = "INSERT INTO wlw ( wlw_level, wlw_name, wlw_url, wlw_processing, wlw_finished, wlw_source )"
                        + "VALUES(?, ?, ?, ?, ?, ?)";
            PreparedStatement preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

            for(QueryStore qs : QueryStore) {
                preparedStatement.setInt(1, qs.level);
                preparedStatement.setString(2, qs.anchor);
                preparedStatement.setString(3, qs.url);
                preparedStatement.setInt(4, qs.processing);
                preparedStatement.setInt(5, qs.finished);
                preparedStatement.setLong(6, qs.id);
                preparedStatement.addBatch();
                System.out.println("Adding URL: " + qs.url);
            }
            System.out.println("Start saving ...");
            long then = System.currentTimeMillis(); 
            preparedStatement.executeBatch();
            long now = then - System.currentTimeMillis();
            System.out.println("SAVING END - Took me " + now + "ms");

感谢您的任何建议!

4

1 回答 1

5

尝试添加rewriteBatchedStatements=true到您的连接参数。

于 2013-04-22T20:45:39.403 回答