Skip to content

Optimize query by doing build time generation in Panache and maybe Hibernate ORM #3292

Open
@emmanuelbernard

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions