Spring

[Query Dsl] exists 쿼리 결과 존재여부 확인

hwijin97 2021. 9. 8. 15:57

jpa의 existsBy는

count(*) from table where ...

형식의 쿼리가 날라간다.

 

이 경우 table row 전체와 col 전체를 조회하기 때문에 크면 클수록 성능이 저하된다.

따라서

jpaQueryFactory
                .select(table.id)
                .from(table)
                .where(expression)
                .fetchFirst();
                
select id from table where expression limit ?

와 같은 query dsl 을 사용해서 select로 col의 개수를 제한하고,

fetchFirst로 limit 사용을 유도한다.

결과가 없으면 null, 있으면 id type으로 반환된다.