【发布时间】:2018-05-06 05:23:06
【问题描述】:
我正在尝试批量更新 3000 条记录。完成大约需要 15 秒,但插入会在 1 秒内完成。
还包括rewriteBatchedStatements=true。我还需要做什么吗?
以下是我的代码。(不包括异常处理以及成功和失败计数/id 部分。它们对于更新和插入都是相同的)。
使用的技术:spring,jdbctemplate,mysql,java,intellij idea
result = jdbcTemplate.batchUpdate(
//"insert into xxx(aaa,bbb,item_code) values(?,?,?)",
"update ignore xxx set aaa = ?, bbb= ? where item_code = ?",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps,int i) throws SQLException {
ps.setDouble(1, Double.parseDouble(new JSONObject(jsonArray.get(i).toString()).get("aaa").toString()));
ps.setDouble(2, Double.parseDouble(new JSONObject(jsonArray.get(i).toString()).get("bbb").toString()));
ps.setString(3, new JSONObject(jsonArray.get(i).toString()).get("code").toString());
}
public int getBatchSize() {
return jsonArray.length();
}
} );
【问题讨论】:
-
数据库中的 item_code 有索引吗?如果没有,您可以尝试创建一个。
-
@Vasan 这是一个唯一键列,所以它已经被索引了。
标签: java mysql spring-jdbc jdbctemplate