[Spring] QueryDSL 쿼리문 비교

Kyungmin·2024년 4월 17일
0

Spring

목록 보기
14/39

1. left join & fetch join

연관된 데이터를 즉시 로딩하기 위해 사용, 이를 사용하면 하이버네이트는 연관된 데이터를 함께 가져오도록 쿼리문을 생성, 나중에 접근할 때 발생할 수 있는 추가쿼리를 방지

  • post 엔티티와 관련된 user 엔티티를 패치조인으로 연결, post 데이터를 로드할 때 관련된 user 데이터도 함께 즉시 로딩

< 중요 - 레프트 조인만 한거랑 패치조인한거랑 쿼리 같아보이는 문제 왜? >

  • 두 쿼리 모두 post 데이터를 기준으로 조회하므로 결과집합 구조는 동일하다. 즉 얻어지는 post 리스트는 동일하다.
  • 결과가 시각적으로는 같지만 데이터에 접근하는 방식에서 차이가 있다. 패치조인을 사용한 경우에는 post 를 가져올 때 관련 user 데이터도 이미 로드되어 있으므로 데이터 접근 시 지연이 없다, 하지만 레프트조인만 사용한 경우에는 user 데이터에 처음 접근할 때 추가 로드가 필요할 수 있으 성능차이가 발생할 수 있다.
select
        post 
    from
        Post post   
    left join
        
    fetch
        post.user as user 
    where
        post.category = ?1 
    order by
        post.createdAt desc 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
 select
            p1_0.id,
            p1_0.category,
            p1_0.contents,
            p1_0.created_at,
            p1_0.modified_at,
            p1_0.district,
            p1_0.latitude,
            p1_0.longitude,
            u1_0.id,
            u1_0.city,
            u1_0.district,
            u1_0.email,
            u1_0.kakao_id,
            u1_0.nickname,
            u1_0.password,
            u1_0.user_rank,
            u1_0.role 
        from
            post p1_0 
        left join
            users u1_0 
                on u1_0.id=p1_0.user_id 
        where
            p1_0.category=? 
        order by
            p1_0.created_at desc 
        limit
            ?, ?

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        pi1_0.id,
        pi1_0.file_name,
        pi1_0.img_url,
        pi1_0.user_id 
    from
        profile_image pi1_0 
    where
        pi1_0.user_id=?

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        bm1_0.user_id,
        bm1_0.id,
        bm1_0.created_at,
        bm1_0.post_id,
        bm1_0.status 
    from
        book_mark bm1_0 
    where
        bm1_0.user_id=?
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        i1_0.post_id,
        i1_0.id,
        i1_0.file_name,
        i1_0.img_url 
    from
        post_image i1_0 
    where
        i1_0.post_id=?
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        c1_0.post_id,
        c1_0.id,
        c1_0.contents,
        c1_0.created_at,
        c1_0.modified_at,
        c1_0.parent_id,
        c1_0.user_id 
    from
        comment c1_0 
    where
        c1_0.post_id=?
Hibernate: 
    /* <criteria> */ 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

select
        count(l1_0.id) 
    from
        likes l1_0 
    left join
        post p1_0 
            on p1_0.id=l1_0.post_id 
    where
        p1_0.id=? 
        and l1_0.like_category_enum=?
Hibernate: 
    /* <criteria> */ 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
select
        count(l1_0.id) 
    from
        likes l1_0 
    left join
        post p1_0 
            on p1_0.id=l1_0.post_id 
    where
        p1_0.id=? 
        and l1_0.like_category_enum=?
Hibernate: 
    /* <criteria> */
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 select
        pi1_0.id,
        pi1_0.file_name,
        pi1_0.img_url,
        pi1_0.user_id 
    from
        profile_image pi1_0 
    left join
        users u1_0 
            on u1_0.id=pi1_0.user_id 
    where
        u1_0.id=?

2. left join

left join 은 게시물의 모든 데이터를 가져오며, 연결된 user 데이터가 있으면 이 데이터도 함께 가져온다. 그러나 이 쿼리에서는 user 엔티티의 데이터를 즉시 로드하지 않는다. 즉 user 데이터에 접근하려고 할 때, 필요에 따라 추가적인 쿼리가 실행될 수 있다.

select
        post 
    from
        Post post   
    left join
        post.user as user 
    where
        post.category = ?1 
    order by
        post.createdAt desc  
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
select
            p1_0.id,
            p1_0.category,
            p1_0.contents,
            p1_0.created_at,
            p1_0.modified_at,
            p1_0.district,
            p1_0.latitude,
            p1_0.longitude,
            p1_0.user_id 
        from
            post p1_0 
        where
            p1_0.category=? 
        order by
            p1_0.created_at desc 
        limit
            ?, ?


ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        u1_0.id,
        u1_0.city,
        u1_0.district,
        u1_0.email,
        u1_0.kakao_id,
        u1_0.nickname,
        u1_0.password,
        u1_0.user_rank,
        u1_0.role,
        bm1_0.user_id,
        bm1_0.id,
        bm1_0.created_at,
        bm1_0.post_id,
        bm1_0.status 
    from
        users u1_0 
    left join
        book_mark bm1_0 
            on u1_0.id=bm1_0.user_id 
    where
        u1_0.id=?

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        pi1_0.id,
        pi1_0.file_name,
        pi1_0.img_url,
        pi1_0.user_id 
    from
        profile_image pi1_0 
    where
        pi1_0.user_id=?

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        i1_0.post_id,
        i1_0.id,
        i1_0.file_name,
        i1_0.img_url 
    from
        post_image i1_0 
    where
        i1_0.post_id=?
 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        c1_0.post_id,
        c1_0.id,
        c1_0.contents,
        c1_0.created_at,
        c1_0.modified_at,
        c1_0.parent_id,
        c1_0.user_id 
    from
        comment c1_0 
    where
        c1_0.post_id=?
Hibernate: 
    /* <criteria> */ 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
select
        count(l1_0.id) 
    from
        likes l1_0 
    left join
        post p1_0 
            on p1_0.id=l1_0.post_id 
    where
        p1_0.id=? 
        and l1_0.like_category_enum=?
Hibernate: 
    /* <criteria> */
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 select
        count(l1_0.id) 
    from
        likes l1_0 
    left join
        post p1_0 
            on p1_0.id=l1_0.post_id 
    where
        p1_0.id=? 
        and l1_0.like_category_enum=?
Hibernate: 
    /* <criteria> */
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 select
        pi1_0.id,
        pi1_0.file_name,
        pi1_0.img_url,
        pi1_0.user_id 
    from
        profile_image pi1_0 
    left join
        users u1_0 
            on u1_0.id=pi1_0.user_id 
    where
        u1_0.id=?

3. 사용 x

select
        post 
    from
        Post post 
    where
        post.category = ?1 
    order by
        post.createdAt desc */ 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

select
            p1_0.id,
            p1_0.category,
            p1_0.contents,
            p1_0.created_at,
            p1_0.modified_at,
            p1_0.district,
            p1_0.latitude,
            p1_0.longitude,
            p1_0.user_id 
        from
            post p1_0 
        where
            p1_0.category=? 
        order by
            p1_0.created_at desc 
        limit
            ?, ?
Hibernate: 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        u1_0.id,
        u1_0.city,
        u1_0.district,
        u1_0.email,
        u1_0.kakao_id,
        u1_0.nickname,
        u1_0.password,
        u1_0.user_rank,
        u1_0.role,
        bm1_0.user_id,
        bm1_0.id,
        bm1_0.created_at,
        bm1_0.post_id,
        bm1_0.status 
    from
        users u1_0 
    left join
        book_mark bm1_0 
            on u1_0.id=bm1_0.user_id 
    where
        u1_0.id=?
Hibernate: 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        pi1_0.id,
        pi1_0.file_name,
        pi1_0.img_url,
        pi1_0.user_id 
    from
        profile_image pi1_0 
    where
        pi1_0.user_id=?
Hibernate: 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        i1_0.post_id,
        i1_0.id,
        i1_0.file_name,
        i1_0.img_url 
    from
        post_image i1_0 
    where
        i1_0.post_id=?
Hibernate: 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

    select
        c1_0.post_id,
        c1_0.id,
        c1_0.contents,
        c1_0.created_at,
        c1_0.modified_at,
        c1_0.parent_id,
        c1_0.user_id 
    from
        comment c1_0 
    where
        c1_0.post_id=?
Hibernate: 
    /* <criteria> */ 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

select
        count(l1_0.id) 
    from
        likes l1_0 
    left join
        post p1_0 
            on p1_0.id=l1_0.post_id 
    where
        p1_0.id=? 
        and l1_0.like_category_enum=?
Hibernate: 
    /* <criteria> */ 
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

select
        count(l1_0.id) 
    from
        likes l1_0 
    left join
        post p1_0 
            on p1_0.id=l1_0.post_id 
    where
        p1_0.id=? 
        and l1_0.like_category_enum=?
Hibernate: 
    /* <criteria> */ 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
select
        pi1_0.id,
        pi1_0.file_name,
        pi1_0.img_url,
        pi1_0.user_id 
    from
        profile_image pi1_0 
    left join
        users u1_0 
            on u1_0.id=pi1_0.user_id 
    where
        u1_0.id=?
profile
Backend Developer

0개의 댓글

관련 채용 정보