8 个答案:
答案 0 :(得分:12)
JDBC连接URL中的这些参数可以对批量语句的速度产生很大影响 - 根据我的经验,它们可以加快速度:
useServerPrepStmts =假安培; rewriteBatchedStatements =真
答案 1 :(得分:10)
我也遇到了与Spring JDBC模板相同的问题。可能在Spring Batch中,语句在每个插件或块上执行并提交,这会减慢速度。
我已使用原始JDBC批量插入代码替换了jdbcTemplate.batchUpdate()代码,并找到了主要性能改进。
DataSource ds = jdbcTemplate.getDataSource();
Connection connection = ds.getConnection();
connection.setAutoCommit(false);
String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
PreparedStatement ps = connection.prepareStatement(sql);
final int batchSize = 1000;
int count = 0;
for (Employee employee: employees) {
ps.setString(1, employee.getName());
ps.setString(2, employee.getCity());
ps.setString(3, employee.getPhone());
ps.addBatch();
++count;
if(count % batchSize == 0 || count == employees.size()) {
ps.executeBatch();
ps.clearBatch();
}
}
connection.commit();
ps.close();
答案 2 :(得分:7)
只需使用交易。在方法上添加@Transactional。
如果使用多个数据源@Transactional(“dsTxManager”),请务必声明正确的TX管理器。我有一个插入60000记录的情况。大约需要15秒。没有其他调整:
@Transactional("myDataSourceTxManager")
public void save(...) {
...
jdbcTemplate.batchUpdate(query, new BatchPreparedStatementSetter() {
@Override
public void setValues(Prepared