티스토리 뷰

Road to Beyrouth

JPA N+1 문제

LunaStratos 2022. 2. 15. 22:37

 

뜬금없지만 궁금해져서 찾아봐서 개인서버에 적용해봤다.

 

https://madplay.github.io/post/avoid-n+1-problem-in-jpa-using-querydsl-fetchjoin 

 

Querydsl: fetch join으로 JPA의 N+1 문제 해결하기

Querydsl의 fetchJoin을 사용하여 JPA의 N+1 문제를 회피해보자

madplay.github.io

 

로그를 보니 실제로 그런 증상이 나온다.

 

 

더보기

Hibernate: 
    select
        user0_.id as id1_11_,
        user0_.level as level2_11_,
        user0_.name as name3_11_,
        user0_.team_id as team_id4_11_ 
    from
        user user0_

 

Hibernate: 
    select
        team0_.id as id1_10_0_ 
    from
        team team0_ 
    where
        team0_.id=?

 

Hibernate: 
    select
        team0_.id as id1_10_0_ 
    from
        team team0_ 
    where
        team0_.id=?

 

Hibernate: 
    select
        team0_.id as id1_10_0_ 
    from
        team team0_ 
    where
        team0_.id=?

 

Hibernate: 
    select
        articlelis0_.user_id as user_id3_0_0_,
        articlelis0_.id as id1_0_0_,
        articlelis0_.id as id1_0_1_,
        articlelis0_.title as title2_0_1_,
        articlelis0_.user_id as user_id3_0_1_ 
    from
        article articlelis0_ 
    where
        articlelis0_.user_id=?

 

Hibernate: 
    select
        articlelis0_.user_id as user_id3_0_0_,
        articlelis0_.id as id1_0_0_,
        articlelis0_.id as id1_0_1_,
        articlelis0_.title as title2_0_1_,
        articlelis0_.user_id as user_id3_0_1_ 
    from
        article articlelis0_ 
    where
        articlelis0_.user_id=?

 

...

너무 길어서 접었지만 전체 실행 쿼리가 있고나서 자잘한 쿼리들이 나온다

이래서 1개 더 추가된 N+1 문제라고 하나 보다. 

 

해결방법은 fetch를 해주고 dictinct로 row의 중복을 제거하면된다.

실제로 스티커 보드쪽에 적용을 해보니 N+1문제를 발견할 수 있었다

 

게시물은 1, 리플은 N인 상태로 1:N 관계이다. 

 

 

Hibernate: 
    select
        stickerent0_.id as id1_6_0_,
        stickerrep1_.id as id1_7_1_,
        stickerent0_.auther as auther2_6_0_,
        stickerent0_.contents as contents3_6_0_,
        stickerent0_.created_at as created_4_6_0_,
        stickerent0_.file_folder as file_fol5_6_0_,
        stickerent0_.file_name_zip as file_nam6_6_0_,
        stickerent0_.search_tag as search_t7_6_0_,
        stickerent0_.title as title8_6_0_,
        stickerent0_.updated_at as updated_9_6_0_,
        stickerent0_.view_count as view_co10_6_0_,
        stickerrep1_.auther as auther2_7_1_,
        stickerrep1_.contents as contents3_7_1_,
        stickerrep1_.created_at as created_4_7_1_,
        stickerrep1_.sticker_id as sticker_5_7_1_,
        stickerrep1_.updated_at as updated_6_7_1_,
        stickerrep1_.sticker_id as sticker_5_7_0__,
        stickerrep1_.id as id1_7_0__ 
    from
        t_sticker stickerent0_ 
    left outer join
        t_sticker_reply stickerrep1_ 
            on stickerent0_.id=stickerrep1_.sticker_id

이것은 dictinct전의 fetchJoin을 한 상태 

 

Hibernate: 
    select
        distinct stickerent0_.id as id1_6_0_,
        stickerrep1_.id as id1_7_1_,
        stickerent0_.auther as auther2_6_0_,
        stickerent0_.contents as contents3_6_0_,
        stickerent0_.created_at as created_4_6_0_,
        stickerent0_.file_folder as file_fol5_6_0_,
        stickerent0_.file_name_zip as file_nam6_6_0_,
        stickerent0_.search_tag as search_t7_6_0_,
        stickerent0_.title as title8_6_0_,
        stickerent0_.updated_at as updated_9_6_0_,
        stickerent0_.view_count as view_co10_6_0_,
        stickerrep1_.auther as auther2_7_1_,
        stickerrep1_.contents as contents3_7_1_,
        stickerrep1_.created_at as created_4_7_1_,
        stickerrep1_.sticker_id as sticker_5_7_1_,
        stickerrep1_.updated_at as updated_6_7_1_,
        stickerrep1_.sticker_id as sticker_5_7_0__,
        stickerrep1_.id as id1_7_0__ 
    from
        t_sticker stickerent0_ 
    left outer join
        t_sticker_reply stickerrep1_ 
            on stickerent0_.id=stickerrep1_.sticker_id

이것은 distinct 후의 fetchJoin 상태이다. 

 

컬럼이 일부 줄어든걸 확인할수 있다 

 

'Road to Beyrouth' 카테고리의 다른 글

스웩이 아닌 Swagger 사용기  (0) 2022.02.11
코딩테스트 대비 1주차  (0) 2021.12.19
이대로는 안된다.  (0) 2021.12.01
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함