Optimize query by doing build time generation in Panache and maybe Hibernate ORM #3292
Open
Description
Today queries are built at runtime. This cost might be minimal but it would be interesting to measure it. One easy optimisation would be the following.
Detect at build time the following pattern
public List<Book> findMatchingBooks(String title) {
return find("title like ?1", title).list();
}
And bytecode change it to
//put named query were it would be found or enlist it at build time
@NamedQuery(name="generated_findTitle", query="from Book where title like ?1")
public List<Book> public List<Book> findMatchingBooks(String title) {
return JpaOperations.namedQuery("generated_findTitle", title).list();
}
//on JpaOperations
public static PanacheQuery namedQuery(String namedQuery, Map> params, ...) {
// otherwise do what JpaOperations.find does
EntityManager em = JpaOperations.getEntityManager();
Query jpaQuery = em.createNamedQuery("generated_findTitle");
JpaOperation.bindParameters(...);
return new PanacheQueryImpl(...);
same for list, count etc.
It would be nice to make this happen on direct calls like this too across the code base. I suppose it's similar to the field to getter rewriting.
public List<Book> getBooks(String title) {
return Book.list("title like ?1", title);
}
Activity