为什么更新需要很长时间,而不是在SpringJDBCTemplateBatchUpdate中插入?

w8ntj3qf  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(270)

我正在尝试对3000条记录进行批量更新。大约需要15秒才能完成,但插入只需1秒。
包括 rewriteBatchedStatements=true 也。我还需要做什么吗?
下面是我的代码(不包括异常处理和成功与失败计数/ids部分)。它们对于更新和插入都是相同的)。
使用的技术: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();
                    }
                } );
hgtggwj0

hgtggwj01#

还包括useserverprestmts=false。
这里也提出了类似的问题:jdbc批插入性能
jdbc性能改进提示:http://javarevisited.blogspot.in/2012/01/improve-performance-java-database.html

相关问题