Improve statement caching efficiency

Relational database provides an Execution Plan cache:

Since query compilation takes time, Hibernate provides a QueryPlanCache for better performance. For native queries, Hibernate extracts information about the named parameters and query return type and stores it in the ParameterMetadata.

The IN clause parameter padding feature increases the chance of reusing an already generated Execution Plan, especially when using a large number of IN clause parameters.

In clause parameter padding:

  • spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true

Query plan cache configuration

  • hibernate.query.plan_cache_max_size – controls the maximum number of entries in the plan cache (defaults to 2048)
  • hibernate.query.plan_parameter_metadata_max_size – manages the number of ParameterMetadata instances in the cache (defaults to 128)

Increasing the number of queries that Hibernate is allowed to cache consequently reduces the compilation time.

Using Hibernate Statistics:

  • getQueryPlanCacheHitCount
  • getQueryPlanCacheMissCount

Optimization Analysis

QuickPerf 로 lazy loading 발생여부 확인 가능(실제로는 쿼리가 날라간 갯수를 테스트 코드로 검증가능)