title: Spring JDBC - Batch DELETE and INSERT
body: <p>In my application, I have a table with around 200K records that I need to update in the database. Instead of checking for every record if a matching record exists in DB and then inserting or updating, I thought a faster approach would be to just delete all the matching records in DB and insert them. I am using the Spring JDBC framework.
  To delete, I used the Jdbctemplate batchUpdate method along with a ParameterizedPreparedStatementSetter and for insert I am using SimplJdbcInsert.
  The insert works fine, however, the batch delete performance is very slow.
  I am not quite sure as what other approach I should take to delete the records in DB and insert them. Any suggestions would be very helpful. I am using SQL Server 2008 R2</p>
  
  <pre><code>ParameterizedPreparedStatementSetter&lt;Order&gt; vSetter = 
              new ParameterizedPreparedStatementSetter&lt;Order&gt;() {
                  @Override
                  public void setValues(PreparedStatement ps,
                          Order order) throws SQLException {
                      ps.setInt(1, order.getOrderNum());
                  }
      };
  
  getJdbcTemplate().batchUpdate("DELETE FROM Order WHERE OrderNum = ?",
              aDemandOrders,
              50000,
              vSetter);
  </code></pre>