Java - 为什么我的插入这么慢?

时间:2013-04-22 20:41:42

标签: java mysql

不知何故,我的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");

感谢您的任何建议!!

1 个答案:

答案 0 :(得分:5)

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