I have developed an application in spring boot latest version with Informix database. has some tasks that I want to execute them in parallel. I have the following question and problems.
Does jdbcTemplate.batchupdate()
parallelize queries via threads, run them concurrently via asynchronous programming or just execute them one by one and sequentially?
private String query1, query2, query3;
public void executeQuery(JdbcTemplate jdbctemplate) {
jdbctemplate.batchupdate(query1, query2, query3)
}
I do excut them in thread but I see no difference in performance. Any idea why?
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();
}
}
Does jdbcTemplate.batchupdate() parallelize queries via threads?
No. It uses JDBC batch updates to submit multiple SQL statements as a batch.
The performance benefit comes from reducing the communication overheads, not from (client-side) parallelism.
If you execute a sequence of single SQL update statements N times, the client-side steps are something like this:
Bottlenecks here are, sending the SQL, waiting for the database to process the request and receiving the response, and doing all of these N times.
If you execute multiple SQL update statements as a batch
There are still bottlenecks. However:
It is faster to send a 1 large message than N small messages containing the same SQL statements or the counts. This is because:
The database can potentially process the multiple SQL statements in parallel.
Since the database receives a large number of statements in the batch, it can potentially schedule them more efficient.
By contrast, if you were to run a number of client-side threads each with their own JDBC connection, and each sending single SQL statements.
You don't get the network efficiency... because each JDBC connection will use a separate TCP/IP connection
The database will be able to process SQL is parallel
The database won't be able to schedule the statements as well because it won't be able to see "what is coming next" on any connection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.