인프런 커뮤니티 질문&답변

송유진님의 프로필 이미지

작성한 질문수

스프링과 JPA 기반 웹 애플리케이션 개발

모임 목록 조회

EntityGraph 적용 시 account 조회 여부 다른 이유?

작성

·

179

0

안녕하세요! 강의 너무 잘 듣고 있습니다. 배울수록 기억이 날라가는지 슬슬 헷갈려지기 시작하네요 ㅎㅎ 첫 질문 남겨봅니다.

최종 리팩토링된 조회 쿼리 기준으로,
아래 두 쿼리의  account 조회결과의 차이가 어떤 이유로 달라지나요?
(EntityGraphType 을 바꾸는 것으로 테스트해보니 이건 아닌거같네요..)

- Study 조회할 때는 account 엔티티도 포함되어 조회됨

    select
        study0_.id as id1_6_0_,
        tag2_.id as id1_11_1_,
        zone4_.id as id1_12_2_,
        account6_.id as id1_0_3_,
        account8_.id as id1_0_4_,
        study0_.closed as closed2_6_0_,
        study0_.closed_date_time as closed_d3_6_0_,
        study0_.full_description as full_des4_6_0_,
        study0_.image as image5_6_0_,
        study0_.path as path6_6_0_,
        study0_.published as publishe7_6_0_,
        study0_.published_date_time as publishe8_6_0_,
        study0_.recruiting as recruiti9_6_0_,
        study0_.recruiting_updated_date_time as recruit10_6_0_,
        study0_.short_description as short_d11_6_0_,
        study0_.title as title12_6_0_,
        study0_.use_banner as use_ban13_6_0_,
        tag2_.title as title2_11_1_,
        tags1_.study_id as study_id1_9_0__,
        tags1_.tags_id as tags_id2_9_0__,
        zone4_.city as city2_12_2_,
        zone4_.local_name_of_city as local_na3_12_2_,
        zone4_.province as province4_12_2_,
        zones3_.study_id as study_id1_10_1__,
        zones3_.zones_id as zones_id2_10_1__,
        account6_.bio as bio2_0_3_,
        account6_.email as email3_0_3_,
        account6_.email_check_token as email_ch4_0_3_,
        account6_.email_check_token_generated_at as email_ch5_0_3_,
        account6_.email_verified as email_ve6_0_3_,
        account6_.joined_at as joined_a7_0_3_,
        account6_.location as location8_0_3_,
        account6_.nickname as nickname9_0_3_,
        account6_.occupation as occupat10_0_3_,
        account6_.password as passwor11_0_3_,
        account6_.profile_image as profile12_0_3_,
        account6_.study_created_by_email as study_c13_0_3_,
        account6_.study_created_by_web as study_c14_0_3_,
        account6_.study_enrollment_result_by_email as study_e15_0_3_,
        account6_.study_enrollment_result_by_web as study_e16_0_3_,
        account6_.study_updated_by_email as study_u17_0_3_,
        account6_.study_updated_by_web as study_u18_0_3_,
        account6_.url as url19_0_3_,
        managers5_.study_id as study_id1_7_2__,
        managers5_.managers_id as managers2_7_2__,
        account8_.bio as bio2_0_4_,
        account8_.email as email3_0_4_,
        account8_.email_check_token as email_ch4_0_4_,
        account8_.email_check_token_generated_at as email_ch5_0_4_,
        account8_.email_verified as email_ve6_0_4_,
        account8_.joined_at as joined_a7_0_4_,
        account8_.location as location8_0_4_,
        account8_.nickname as nickname9_0_4_,
        account8_.occupation as occupat10_0_4_,
        account8_.password as passwor11_0_4_,
        account8_.profile_image as profile12_0_4_,
        account8_.study_created_by_email as study_c13_0_4_,
        account8_.study_created_by_web as study_c14_0_4_,
        account8_.study_enrollment_result_by_email as study_e15_0_4_,
        account8_.study_enrollment_result_by_web as study_e16_0_4_,
        account8_.study_updated_by_email as study_u17_0_4_,
        account8_.study_updated_by_web as study_u18_0_4_,
        account8_.url as url19_0_4_,
        members7_.study_id as study_id1_8_3__,
        members7_.members_id as members_2_8_3__ 
    from
        study study0_ 
    left outer join
        study_tags tags1_ 
            on study0_.id=tags1_.study_id 
    left outer join
        tag tag2_ 
            on tags1_.tags_id=tag2_.id 
    left outer join
        study_zones zones3_ 
            on study0_.id=zones3_.study_id 
    left outer join
        zone zone4_ 
            on zones3_.zones_id=zone4_.id 
    left outer join
        study_managers managers5_ 
            on study0_.id=managers5_.study_id 
    left outer join
        account account6_ 
            on managers5_.managers_id=account6_.id 
    left outer join
        study_members members7_ 
            on study0_.id=members7_.study_id 
    left outer join
        account account8_ 
            on members7_.members_id=account8_.id 
    where
        study0_.path=?

- Event 조회 쿼리에서는 enrollment의 account id만 조회됨

    select
        event0_.id as id1_4_0_,
        enrollment1_.id as id1_3_1_,
        event0_.created_by_id as created10_4_0_,
        event0_.created_date_time as created_2_4_0_,
        event0_.description as descript3_4_0_,
        event0_.end_date_time as end_date4_4_0_,
        event0_.end_enrollment_date_time as end_enro5_4_0_,
        event0_.event_type as event_ty6_4_0_,
        event0_.limit_of_enrollments as limit_of7_4_0_,
        event0_.start_date_time as start_da8_4_0_,
        event0_.study_id as study_i11_4_0_,
        event0_.title as title9_4_0_,
        enrollment1_.accepted as accepted2_3_1_,
        enrollment1_.account_id as account_5_3_1_,
        enrollment1_.attended as attended3_3_1_,
        enrollment1_.enrolled_at as enrolled4_3_1_,
        enrollment1_.event_id as event_id6_3_1_,
        enrollment1_.event_id as event_id6_3_0__,
        enrollment1_.id as id1_3_0__ 
    from
        event event0_ 
    left outer join
        enrollment enrollment1_ 
            on event0_.id=enrollment1_.event_id 
    where
        event0_.study_id=? 
    order by
        event0_.start_date_time asc


N+1 관련내용은 이해가 되었는데 account 를 보니 좀 헷갈려서.. 확실히 알고 싶어 질문 드립니다!!

답변 2

1

백기선님의 프로필 이미지
백기선
지식공유자

일단 첫번째 쿼리는 아래처럼 엔티티 그래프가 설정되어 있으니 멤버와 매니저 정보까지 같이 읽어온 것입니다.

 @EntityGraph(attributePaths = {"members", "managers"})Study findStudyWithManagersAndMemebersById(Long id);

이벤트를 읽어올 때는 이벤트의 등록 정보만 읽어오도록 설정해 두었으니 이벤트 -> 등록 -> 참여자의 관계에서 "참여자"까진 가져오지 않은것이구요.

@EntityGraph(value = "Event.withEnrollments", type = EntityGraph.EntityGraphType.LOAD)
List<Event> findByStudyOrderByStartDateTime(Study study);

0

송유진님의 프로필 이미지
송유진
질문자

아하 답변 감사드립니다 :)!!