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!!
Statement.RETURN_GENERATED_KEYSas you don't use the returned generated keys.INSERToperation? 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?