문서의 이전 판입니다!
join fetch
를 통해서 (inner join등 명시) N + 1 대신에 미리 join 문으로 객체를 로딩할 수 있다.
batch-size
설정은 OneToMany 등의 관계에서 Many쪽 Lazy Proxy 객체를 로딩할때, 한번에 로딩할 갯수를 지정한다.
예를들면 Cat 객체가 Kitten 객체의 리스트를 Lazy로 가지고 있을 때 List<Cat>
을 돌면서 getKittens()
를 실행하면 각각의 Cat에 대해 한번씩 쿼리가 날라간다.(n + 1 problem)
하지만 @BatchSize(size = 10)
처럼 해당 관계에 설정을 해 두면, Kitten 객체를 로딩할 때 Cat id 10개씩 in
Query로 한번에 로딩한다.
hibernate.default_batch_fetch_size=30
프라퍼티 형태로 일괄 지정도 가능하다.TBD
항상
true로 둘 것.
*
IN 절 쿼리에 대해서 2의 제곱단위로 쿼리를 생성한다.
* How to improve statement caching efficiency with IN clause parameter padding - Vlad Mihalcea
* 위 옵션을 켜지 않으면,
IN 절 쿼리가 Query Plan Cache를 모두 점유해서 메모리가 부족해질수 있고, 메모리가 남아돌더라도 캐시를 모두
IN 절이 점유해버리므로 별로 좋지 못하다.
* 또한 RDBMS 의 경우 그 자체 execution plan cache 가 있는데, 이 옵션을 켜면 그에 대한 hit 율도 높아지게 된다.
===== Hibernate Query Plan Cache =====
* A beginner’s guide to the Hibernate JPQL and Native Query Plan Cache - Vlad Mihalcea
*
hibernate.query.plan_cache_max_size 기본값은
2048 이다.
* 별문제 없으면 변경하지 말 것.
===== autoCommit 을 꺼서 성능 향상시키기 =====
* Connection Pool 의
auto commit=false을 항상 켜고, Hibernate 에서 직접
auto commit=false 하는 것을 막아서 불필요한
setAutoCommit() 호출로 인한 성능 저하를 줄이기
* Hibernate setAutoCommit 최적화를 통한 성능 튜닝
* Why you should always use hibernate.connection.provider_disables_autocommit for resource-local JPA transactions - Vlad Mihalcea
* 커넥션 풀 설정의
auto-commit: false로 지정. 이걸
true로 해버리면 트랜잭션이 안 먹음.
*
hibernate.connection.provider_disables_autocommit: true로 지정 : 커넥션 풀의 autocommit 을 무조건 따르겠다는 의미.
5.2.10 이상 버전
* SpringBoot 2 부터는 커넥션 풀의
auto-commit 설정값에 따라 자동으로
hibernate.connection.provider_disables_autocommit: true 설정을 해준다.
* Consider disabling auto-commit when wiring connection pool configs and setting hibernate.connection.provider_disables_autocommit for resource-local transactions · Issue #9261 · spring-projects/spring-boot
*
HibernateJpaConfiguration.java'' if (!vendorProperties.containsKey(PROVIDER_DISABLES_AUTOCOMMIT)) { configureProviderDisablesAutocommit(vendorProperties); } ... if (isDataSourceAutoCommitDisabled() && !isJta()) { vendorProperties.put(PROVIDER_DISABLES_AUTOCOMMIT, "true"); }
in Hibernate 5.2.10, we introduced the hibernate.connection.provider_disables_autocommit configuration property which tells Hibernate that the underlying JDBC Connections already disabled the auto-commit mode.