• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

쿼리..질문

24.01.23 20:46 작성 조회수 123

0

안녕하세요,,

김영한님 강의를 열혈 수강하고 있는 김주영이라고합니다..

 

다름아니라, 제가 개인 프로젝트 중 아래 쿼리문을 작성을 했는데

DB조회 개수가 7개 일때, 아래의 쿼리를 실행시키면 select User문에서 쿼리가 7번 나가더라구요,,

 

도통 이유를 모르겠어서 질문 드립니다..

@Query("select distinct new ..패키지명...user.UserScrapUserQueryDto(us, coalesce(scrap.status, false), us.selectedFirstAt) " +
        "from User us " +
        "left join fetch Work work on work.user.id = us.id " +
        "left join fetch UserScrap scrap on scrap.targetId = us.id and scrap.user.id = :userId " +
        "where us.userStatus = 'PERMITTED' and " +
        "      us.id not in (select block.targetId from Block block where block.user.id = :userId and block.status = true) " +
        "and us.id != :userId and us.role = 'USER' " +
        "order by us.selectedFirstAt desc")
  • 내 userid에 해당하는 user는 제외,

  • block한 유저는 제외,

  • UserScarp과 Work는 fetch join으로 갖고오는 쿼리인데,

     

     

  • 혹시 왜 select절이 DB 반환 개수만큼 나오는지 알려주실 수 있을까요? ㅠㅠ

 

 

아래처럼 쿼리가 한번 나간 후에

Hibernate: 

    select

        distinct user0_.user_id as col_0_0_,

        coalesce(userscrap2_.status,

        false) as col_1_0_,

        user0_.selected_first_at as col_2_0_ 

    from

        "user" user0_ 

    left outer join

        "work" work1_ 

            on (

                work1_.user_id=user0_.user_id

            ) 

    left outer join

        "user_scrap" userscrap2_ 

            on (

                userscrap2_.target_id=user0_.user_id 

                and userscrap2_.user_id=?

            ) 

    where

        user0_.user_status='PERMITTED' 

        and (

            user0_.user_id not in  (

                select

                    block3_.target_id 

                from

                    "block" block3_ 

                where

                    block3_.user_id=? 

                    and block3_.status=true

            )

        ) 

        and user0_.user_id<>? 

        and user0_.role='USER' 

    order by

        user0_.selected_first_at desc

 

 

select 쿼리로 db에서 갖고온 개수만큼 반환합니다..

 

Hibernate: 
    select
        user0_.user_id as user_id1_7_0_,
        user0_.created_at as created_2_7_0_,
        user0_.modified_at as modified3_7_0_,
        user0_.behance as behance4_7_0_,
        user0_.detail as detail5_7_0_,
        user0_.device_token as device_t6_7_0_,
        user0_.email as email7_7_0_,
        user0_.first_work_id as first_wo8_7_0_,
        user0_.info as info9_7_0_,
        user0_.instagram as instagr10_7_0_,
        user0_.magazine_view_count as magazin11_7_0_,
        user0_.notion as notion12_7_0_,
        user0_.role as role13_7_0_,
        user0_.scrap_count as scrap_c14_7_0_,
        user0_.selected_first_at as selecte15_7_0_,
        user0_.tag as tag16_7_0_,
        user0_.user_name as user_na17_7_0_,
        user0_.user_status as user_st18_7_0_,
        user0_.view_count as view_co19_7_0_,
        user0_.work_thumb_nail as work_th20_7_0_ 
    from
        "user" user0_ 
    where
        user0_.user_id=?

 

답변 주시면 정말 감사하겠습니다..

답변 1

답변을 작성해보세요.

0

안녕하세요. 김주영님

도움을 드리고 싶지만 질문 내용만으로는 답변을 드리기 어렵습니다.

최대한 단순한 코드로 문제가 되는 부분을 재현할 수 있는 작은 코드를 새로 만들어주세요.

이렇게 만든 코드의 전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요

추가로 다음 내용도 코멘트 부탁드립니다.

1. 문제 영역을 실행할 수 있는 방법

2. 문제가 어떻게 나타나는지에 대한 상세한 설명

링크: 공식 서포터즈

링크: 자주하는 질문

감사합니다.