Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Spring's jdbcTemplate.batchUpdate() so slow?

I'm trying to find the faster way to do batch insert.

I tried to insert several batches with jdbcTemplate.update(String sql), where sql was builded by StringBuilder and looks like:

INSERT INTO TABLE(x, y, i) VALUES(1,2,3), (1,2,3), ... , (1,2,3) 

Batch size was exactly 1000. I inserted nearly 100 batches. I checked the time using StopWatch and found out insert time:

min[38ms], avg[50ms], max[190ms] per batch 

I was glad but I wanted to make my code better.

After that, I tried to use jdbcTemplate.batchUpdate in way like:

    jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {         @Override         public void setValues(PreparedStatement ps, int i) throws SQLException {                        // ...         }         @Override         public int getBatchSize() {             return 1000;         }     }); 

where sql was look like

INSERT INTO TABLE(x, y, i) VALUES(1,2,3); 

and I was disappointed! jdbcTemplate executed every single insert of 1000 lines batch in separated way. I loked at mysql_log and found there a thousand inserts. I checked the time using StopWatch and found out insert time:

min[900ms], avg[1100ms], max[2000ms] per Batch

So, can anybody explain to me, why jdbcTemplate doing separated inserts in this method? Why method's name is batchUpdate? Or may be I am using this method in wrong way?

like image 806
user2602807 Avatar asked Dec 03 '13 20:12

user2602807


People also ask

Is JdbcTemplate Batchupdate transactional?

JdbcTemplate Batch Inserts Example Batch Insert/Update operations must be Transactional. For batch operations you can use batch update callback BatchPreparedStatementSetter to set parameter values.

What does JdbcTemplate Batchupdate return?

All batch update methods return an int array containing the number of affected rows for each batch entry. This count is reported by the JDBC driver and it's not always available in which case the JDBC driver simply returns a -2 value.

Which is better JdbcTemplate or hibernate?

Hibernate makes a lot of assumptions and forces you to think and code in a certain way. Using JdbcTemplate is easier because it's just a very thin wrapper around JDBC itself. The price here is that you will write thousands of lines of really boring code. Also, you will find that SQL strings are really hard to maintain.


1 Answers

These parameters in the JDBC connection URL can make a big difference in the speed of batched statements --- in my experience, they speed things up:

?useServerPrepStmts=false&rewriteBatchedStatements=true

See: JDBC batch insert performance

like image 74
teu Avatar answered Sep 21 '22 06:09

teu