grep

Engineering

DBA와 개발자가 모두 행복해지는 Hibernate의 in_clause_parameter_padding 옵션

NHN

2019년 12월 13일

원문에서 보기 ↗

Java ORM 기술의 표준 명세인 JPA가 소개된 지 참 오래되었지만, 국내 현실상 대규모 시스템에서 적용되어 사용된 운영 경험이 충분히 쌓이지 않고 공유되지도 않는 것 같습니다. 대부분 JPA를 사용한다고 하면 Hibernate를 구현체로 사용하게 됩니다. 제가 담당하는 서비스 역시 Spring Data JPA를 활용하고 있고 JPA 구현체로 Hibernate를 사용하고 있습니다. 개발과 서비스를 운영하면서 겪은 일 중에 SQL의 in 절과 관련해서 발생한 문제를 해결하면서 알게 된 옵션과 그로 인한 효과를 소개해 드리고자 합니다.

증상

1.png

2.png

원인

public interface SampleRepository extends CrudRepository<Sample, Integer>{
    List<Sample> findByIdIn(List<Integer> ids);
}
select .... from Sample where id in (? ,? ,?)
1,2,3,4,5,6,7,8,9,10,11,12
1,2,3,4,5
6,7,8,9,10
11,12
select .... from Sample where id in (? ,? ,?, ?, ?);
select .... from Sample where id in (? ,? ,? ,? ,?);
select .... from Sample where id in (? ,? );
1,2,3,4,5,6,7,8,9,10,11,12,13
select .... from Sample where id in (? ,? ,?, ?, ?);
select .... from Sample where id in (? ,? ,? ,? ,?);
select .... from Sample where id in (? ,?, ?);

해결방법

1) in_clause_parameter_padding

<property>
    name="hibernate.query.in_clause_parameter_padding"
    value="true"
</property>
spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true
1,2,3
1,2,3,4
1,2,3,4,5
1,2,3,4,5,6
select .... from Sample where id in (1 ,2 ,3, 3);
select .... from Sample where id in (1 ,2 ,3, 4);
select .... from Sample where id in (1 ,2 ,3, 4, 5, 5, 5, 5);
select .... from Sample where id in (1 ,2 ,3, 4, 5, 6, 6, 6);

2) padding programmatically

3) execution plan cache 사이즈 조정

<property
    name="hibernate.query.plan_cache_max_size"
    value="2048"
/>
<property
    name="hibernate.query.plan_parameter_metadata_max_size"
    value="128"
/>

참고 링크