I have a complex SQL query that joins several tables and calls some function for some data calculation. Even, I've turned it into a View in order to build a comfortable JPA implementation to obtain the data. The base SQL query takes 2 seconds when it runs on the Database. However, it spends 80 seconds when it is performed from a Spring Boot application.
I did the test in several ways and I got the same result:
- By building a native query object from JPA entityManager
Query typedQuery = entityManager.createNativeQuery(sql, Result.class);
- By creating a mapped entity to the View and querying using CriteriaSpecification
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<ViewResult> query = builder.createQuery(ViewResult.class);
Root<ViewResult> r = query.from(ViewResult.class);
Predicate predicate = builder.conjunction();
...
query.where(predicate);
TypedQuery<ViewResult> typedQuery = entityManager.createQuery(query);
return typedQuery.getResultList();
- By using JDBC and Resulset implementation (iterating the resultset for building the desired list)
Is there a technique to achieve the results with a time close to that which is delayed when running in a database manager?