【问题标题】:Why update takes long time but not insert in spring jdbctemplate batchupdate?为什么更新需要很长时间但不能在spring jdbctemplate批量更新中插入?
【发布时间】: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


【解决方案1】:

还包括 useServerPrepStmts=false 。

这里提出了类似的问题: JDBC batch insert performance

Jdbc 性能提升小技巧: http://javarevisited.blogspot.in/2012/01/improve-performance-java-database.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多