Crayl
Crayl

Reputation: 1911

Java - Why are my inserts so slow?

Somehow my MySQL Inserts are really slow. Even when I use "Batch-Inserts". About 50 inserts take up to 2000ms.. Is this normal or is there a way to improve my code?

            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");

Thanks for any suggestions!!

Upvotes: 3

Views: 992

Answers (1)

Joshua Martell
Joshua Martell

Reputation: 7212

Try adding rewriteBatchedStatements=true to your connection parameters.

Upvotes: 5

Related Questions