3

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!!

5
  • One possible optim. is not using Statement.RETURN_GENERATED_KEYS as you don't use the returned generated keys. Commented Apr 22, 2013 at 20:45
  • 1
    it's an innoDB table? Commented Apr 22, 2013 at 20:48
  • 1
    Are you using a connection pool or creating the connection manually every time you call the method which execute the INSERT operation? Has your table any trigger on insert or is any monitor for your tables? How many data are you inserting? How many indexes your table manages? Does it use lot of foreign keys? Commented Apr 22, 2013 at 20:54
  • @Khalil Yes, is that a problem? Commented Apr 22, 2013 at 21:10
  • @Crayl i think its a problem because when you insert a row into an InnoDB table, innodb engin build the pk and update indexes for that row, and this can slow down your inserts considerably Commented Apr 22, 2013 at 21:21

1 Answer 1

5

Try adding rewriteBatchedStatements=true to your connection parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

Amazing. Now it's very fast. Thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.