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

后端 未结 8 2384
终归单人心
终归单人心 2020-12-04 18:11

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

I tried to insert several batches with jdbcTemplate.update(String sql), wher

8条回答
  •  悲&欢浪女
    2020-12-04 19:07

    I found a major improvement setting the argTypes array in the call.

    In my case, with Spring 4.1.4 and Oracle 12c, for insertion of 5000 rows with 35 fields:

    jdbcTemplate.batchUpdate(insert, parameters); // Take 7 seconds
    
    jdbcTemplate.batchUpdate(insert, parameters, argTypes); // Take 0.08 seconds!!!
    

    The argTypes param is an int array where you set each field in this way:

    int[] argTypes = new int[35];
    argTypes[0] = Types.VARCHAR;
    argTypes[1] = Types.VARCHAR;
    argTypes[2] = Types.VARCHAR;
    argTypes[3] = Types.DECIMAL;
    argTypes[4] = Types.TIMESTAMP;
    .....
    

    I debugged org\springframework\jdbc\core\JdbcTemplate.java and found that most of the time was consumed trying to know the nature of each field, and this was made for each record.

    Hope this helps !

提交回复
热议问题