[英]Batch insert using jdbcTemplate.batchUpdate confusion
Does jdbcTemplate.batchUpdate
execute multiple single insert statements OR 1 multi value list insert on the database server? jdbcTemplate.batchUpdate
是否在数据库服务器上执行多个单插入语句或1 个多值列表插入?
I know that it sends the complete query payload at once to the server but am not sure how the execution takes place.我知道它会立即将完整的查询有效负载发送到服务器,但不确定执行是如何发生的。
Can someone please explain/help?有人可以解释/帮助吗?
From question:从问题:
Does
jdbcTemplate.batchUpdate
execute multiple single insert statements OR 1 multi value list insert on the database server?jdbcTemplate.batchUpdate
是否在数据库服务器上执行多个单插入语句或1 个多值列表插入?
I was curious about
int[] org.springframework.jdbc.core.JdbcTemplate.batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes)
我很好奇
int[] org.springframework.jdbc.core.JdbcTemplate.batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes)
TL;DR: It executes 1 multi-valued list. TL;DR:它执行 1 个多值列表。
Spring Framework is open-source, so it's easy to look at the source code and see that is actually does. Spring 框架是开源的,所以很容易查看源代码并看到它确实如此。
batchUpdate(String sql, List<Object[]> batchArgs, final int[] argTypes)
@Override
public int[] batchUpdate(String sql, List<Object[]> batchArgs, final int[] argTypes) throws DataAccessException {
if (batchArgs.isEmpty()) {
return new int[0];
}
return batchUpdate(
sql,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Object[] values = batchArgs.get(i);
int colIndex = 0;
for (Object value : values) {
colIndex++;
if (value instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) value;
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
}
else {
int colType;
if (argTypes.length < colIndex) {
colType = SqlTypeValue.TYPE_UNKNOWN;
}
else {
colType = argTypes[colIndex - 1];
}
StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value);
}
}
}
@Override
public int getBatchSize() {
return batchArgs.size();
}
});
}
As can be seen, it calls the following method.可以看出,它调用了以下方法。
batchUpdate(String sql, final BatchPreparedStatementSetter pss)
@Override
public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException {
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL batch update [" + sql + "]");
}
int[] result = execute(sql, (PreparedStatementCallback<int[]>) ps -> {
try {
int batchSize = pss.getBatchSize();
InterruptibleBatchPreparedStatementSetter ipss =
(pss instanceof InterruptibleBatchPreparedStatementSetter ?
(InterruptibleBatchPreparedStatementSetter) pss : null);
if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
for (int i = 0; i < batchSize; i++) {
pss.setValues(ps, i);
if (ipss != null && ipss.isBatchExhausted(i)) {
break;
}
ps.addBatch();
}
return ps.executeBatch();
}
else {
List<Integer> rowsAffected = new ArrayList<>();
for (int i = 0; i < batchSize; i++) {
pss.setValues(ps, i);
if (ipss != null && ipss.isBatchExhausted(i)) {
break;
}
rowsAffected.add(ps.executeUpdate());
}
int[] rowsAffectedArray = new int[rowsAffected.size()];
for (int i = 0; i < rowsAffectedArray.length; i++) {
rowsAffectedArray[i] = rowsAffected.get(i);
}
return rowsAffectedArray;
}
}
finally {
if (pss instanceof ParameterDisposer) {
((ParameterDisposer) pss).cleanupParameters();
}
}
});
Assert.state(result != null, "No result array");
return result;
}
As can be seen, it creates a single PreparedStatement
, enters a loop calling addBatch()
, and finally calls executeBatch()
.可以看出,它创建了一个
PreparedStatement
,进入一个调用addBatch()
的循环,最后调用executeBatch()
。
So, the short answer is: 1 multi-valued list .所以,简短的回答是: 1 multi-valued list 。
The full answer is that it likely sends one SQL statement and a multi-valued list to the database server, however it is entirely up to the JDBC driver how it actually implements batching, mostly limited by what the communication protocol supports, so the only way to know for sure is to trace the communication with the server.完整的答案是它可能会向数据库服务器发送一个 SQL 语句和一个多值列表,但是它完全取决于 JDBC 驱动程序如何实际实现批处理,主要受通信协议支持的限制,所以唯一的方法确定是跟踪与服务器的通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.