Spring Data Query Execution Optimization: Parallel Execution of Hibernate @Query Method in JpaRepository
<p>I have a Dashboard view, which requires small sets of data from tables all over the database. I optimized the database queries (e.g. removed sub-queries). There are now ~20 queries which are executed one after the other, and which are fetching different data sets from the database. Most of the HQL queries contain <code>GROUP BY</code> and <code>JOIN</code> clauses. With a Spring REST interface, the result is returned to the front-end.</p>

<p>How do I optimize the execution of the custom queries? My initial thought was to run the database queries in parallel. But how do I achieve that? After doing some research I found the annotation <code>@Async</code> which makes it possible to run methods in parallel. But does this work with Hibernate methods? Is there always a new database session created for every method annotated with <code>@Query</code> in a JpaRepository? Does running a database query have an effect on the overall execution time after all?</p>

<p>Another way to run the database calls in parallel, is splitting the Dashboard call into several single Ajax calls (every concern gets its own Ajax call). I didn't want to do that, because every time the dashboard is opened (or e.g. the date range is changed), another 20 Ajax calls are made to fetch the new data. And the same question remains: Does running SQL queries in parallel have an effect on the execution time of the database?</p>

<p>I currently did not yet add additional indices to the database. This will be the next thing, I definitely will be doing. However, I'm interested on the performance impacts of running the queries in parallel and on how to achieve this programmatically with Spring.</p>

<p>My project was initially generated by jHipster (Spring Boot, MariaDB, AngularJS etc.)</p>