Sunday, 19 March 2017

Hibernate Query Cache

The query cache looks conceptually like an hash map where the key is composed by the query text and the parameter values, and the value is a list of entity Id's that match the query:

--------------------| Query Cache | -------------------------------------------------------
["from Person where firstName=?", ["Joey"] ] -> [1, 2] ] |

Some queries don't return entities, instead they return only primitive values. In those cases the values themselves will be stored in the query cache. The query cache gets populated when a cacheable JPQL/HQL query gets executed.


If a query has cached results, it returns a list of entity Id's, that is then resolved against the second level cache. If the entities with those Ids where not configured as cacheable or if they have expired, then a select will hit the database per entity Id.
For example if a cached query returned 1000 entity Ids, and those entities have expired and are no longer cached in the second level cache, then 1000 different "select by Id" queries will be issued against the database.

No comments:

Post a Comment