我一直在互联网+ stackoverflow上阅读关于为什么jdbc批量更新如此慢的信息。看起来正确的修复方法是rewriteBatchedStatements = true在连接字符串中设置。但我似乎无法让它为我工作。
我正在使用 springboot 和 spring-jdbc 我rewriteBatchedStatements = true在 application.properties 中设置
spring.datasource.url=jdbc:mysql://RDS_URL.us-west-2.rds.amazonaws.com/DATABASE?rewriteBatchedStatements=true
Run Code Online (Sandbox Code Playgroud)
我还设置了一个断点来验证是否?rewriteBatchedStatements=true反映在代码中
我将 General_log 设置为 true,在查看日志时我发现插入未正确批处理
这就是我的 sql 字符串的样子
private static String INSERT_USER_TO_GROUP_SQL = "INSERT INTO users (groupId, phoneNumber, accountId, source) VALUES(?, ?, ?, ?)";
日志中的行看起来都是这样的
45 Query INSERT INTO users (groupId, phoneNumber, accountId, source) VALUES('49', '99999999999', '123', 'web')
我执行批量插入的java代码是
executor.submit(() -> {
jdbcTemplate.batchUpdate(INSERT_USER_TO_GROUP_SQL, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Subscriber subscriber = subscribers.get(i); …Run Code Online (Sandbox Code Playgroud)