My problem is I'm having a table and 7 child table. I'm fetching details using querying the main table first then querying the child tables one by one sequentially. To improve performance I decided to execute all the seven queries parallely using completable future.
StoryDetail storyDetail = new StoryDetail();
CompletableFuture<IStoryDetail> iStoryDetailCompletableFuture = CompletableFuture
.supplyAsync(() -> storyRepository.getStoryDetails(id), executorService);
CompletableFuture<List<Comment>> iCommentFuture = CompletableFuture
.supplyAsync(() -> commentRepository.getComments(id), executorService);
CompletableFuture<List<Images>> iImageFuture = CompletableFuture
.supplyAsync(() -> imageRepository.getImagesByStoryId(id), executorService);
Here we are executing all the queries sequentially:
CompletableFuture.allOf(iCommentFuture, iStoryDetailCompletableFuture, iImageFuture)
;
And waits for all of them to finish then sets the value in the StoryDetail object:
storyDetail.setComments(iCommentFuture.get());
storyDetail.setImages(iImageFuture.get());
mapStoryDetail(iStoryDetailCompletableFuture.get(), storyDetail);
Is the approach correct?
JOINs is going to beat manually executing multiple per-table statements. That's actually the whole point of SQL - to abstract access patterns, in favor of declaring the connections required of the data. \$\endgroup\$