我已经用Informix数据库在spring最新版本中开发了一个应用程序。有一些我想并行执行的任务。我有以下问题和问题。
jdbcTemplate.batchupdate()是通过线程并行化查询,还是通过异步编程并发运行查询,还是只一个一个地执行它们并按顺序执行?
private String query1, query2, query3;
public void executeQuery(JdbcTemplate jdbctemplate) {
jdbctemplate.batchupdate(query1, query2, query3)
}我确实在线程中删除了它们,但是我没有看到性能上的差异。知道为什么吗?
private void executeInThread(){
ExecutorService sommutExecutorService = Executors.newCachedThreadPool();
final CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> jdbcTemplate.update(query1), sommutExecutorService);
final CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> jdbcTemplate.update(query2), sommutExecutorService);
final CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> jdbcTemplate.update(query3), sommutExecutorService);
try {
CompletableFuture.allOf(future1, future2, future3).thenRun(() -> execute()).get();
} catch (InterruptedException | ExecutionException e) {
log(e.getMessage());
}finally {
sommutExecutorService.shutdown();
}
}发布于 2019-11-13 14:34:48
通过线程并行jdbcTemplate.batchupdate()查询吗?
不是的。它使用JDBC批处理更新将多个SQL语句作为一个批处理提交。
性能的好处来自于减少通信开销,而不是(客户端)并行。
如果连续N次执行单个SQL update语句,客户端步骤如下所示:
<代码>H 111JDBC执行调用返回H 212H 113的服务器接收响应到步骤1.直到你做了N次。
这里的瓶颈是:发送SQL、等待数据库处理请求和接收响应,并执行所有这些N次。
如果将多个SQL update语句作为批处理执行
<代码>H 125JDBC executeBatch调用返回H 226G 227
仍然存在瓶颈。然而:
- the network packets will contain more useful information,
- the granularity of acknowledgement is coarser, and therefore
- network round trip delays are squashed.
数据库可以并行地处理多个语句。
相反,如果要运行多个客户端线程--每个线程都有自己的JDBC连接,并且每个线程都发送单个SQL语句。
,
https://stackoverflow.com/questions/58817726
复制相似问题