Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 상품에 대한 리뷰 목록 조회 N+1 문제 완전히 해결 #837

Open
70825 opened this issue Oct 26, 2023 · 0 comments · May be fixed by #838
Open

[BE] 상품에 대한 리뷰 목록 조회 N+1 문제 완전히 해결 #837

70825 opened this issue Oct 26, 2023 · 0 comments · May be fixed by #838

Comments

@70825
Copy link
Member

70825 commented Oct 26, 2023

어떤 기능을 구현하나요?

#607 에서 완전히 해결하지 못했던 N+1 문제를 해결했습니다.

스크린샷 2023-10-27 오전 1 43 55

위 사진에서 Review를 통해 ReviewTag까지는 가져올 수 있었지만, Tag는 못가져왔던 문제를 해결했어요
그래서 쿼리 14개가 날아가던걸 3개로 줄였습니다

  • 최종적으로 개선된 쿼리 수 : 92개 → 14개 → 3개

쿼리문

Hibernate: 
    select
        product0_.id as id1_4_0_,
        product0_.average_rating as average_2_4_0_,
        product0_.category_id as category8_4_0_,
        product0_.content as content3_4_0_,
        product0_.image as image4_4_0_,
        product0_.name as name5_4_0_,
        product0_.price as price6_4_0_,
        product0_.review_count as review_c7_4_0_ 
    from
        product product0_ 
    where
        product0_.id=?
Hibernate: 
    select
        review0_.id as col_0_0_,
        member1_.nickname as col_1_0_,
        member1_.profile_image as col_2_0_,
        review0_.image as col_3_0_,
        review0_.rating as col_4_0_,
        review0_.content as col_5_0_,
        review0_.re_buy as col_6_0_,
        review0_.favorite_count as col_7_0_,
        reviewfavo2_.favorite as col_8_0_,
        review0_.created_at as col_9_0_ 
    from
        review review0_ 
    inner join
        member member1_ 
            on review0_.member_id=member1_.id 
    left outer join
        review_favorite reviewfavo2_ 
            on review0_.id=reviewfavo2_.review_id 
            and (
                reviewfavo2_.member_id=?
            ) 
    where
        review0_.product_id=? 
    order by
        review0_.created_at desc,
        review0_.id desc limit ?
Hibernate: 
    select
        reviewtag0_.id as id1_11_0_,
        review1_.id as id1_9_1_,
        tag2_.id as id1_12_2_,
        reviewtag0_.review_id as review_i2_11_0_,
        reviewtag0_.tag_id as tag_id3_11_0_,
        review1_.content as content2_9_1_,
        review1_.created_at as created_3_9_1_,
        review1_.favorite_count as favorite4_9_1_,
        review1_.image as image5_9_1_,
        review1_.member_id as member_i8_9_1_,
        review1_.product_id as product_9_9_1_,
        review1_.rating as rating6_9_1_,
        review1_.re_buy as re_buy7_9_1_,
        tag2_.name as name2_12_2_,
        tag2_.tag_type as tag_type3_12_2_ 
    from
        review_tag reviewtag0_ 
    inner join
        review review1_ 
            on reviewtag0_.review_id=review1_.id 
    inner join
        tag tag2_ 
            on reviewtag0_.tag_id=tag2_.id 
    where
        reviewtag0_.review_id in (
            ? , ? , ?
        )

응답 데이터

{
    "reviews": [
        {
            "id": 3,
            "userName": "member3",
            "profileImage": "www.member3.com",
            "image": "3.png",
            "rating": 3,
            "tags": [
                {
                    "id": 1,
                    "name": "맛있어요",
                    "tagType": "TASTE"
                },
                {
                    "id": 2,
                    "name": "간식",
                    "tagType": "ETC"
                },
                {
                    "id": 5,
                    "name": "갓성비",
                    "tagType": "QUANTITY"
                }
            ],
            "content": "test",
            "rebuy": false,
            "favoriteCount": 0,
            "favorite": false,
            "createdAt": "2023-10-27T02:04:45.914167"
        },
        {
            "id": 2,
            "userName": "member2",
            "profileImage": "www.member2.com",
            "image": "2.png",
            "rating": 4,
            "tags": [
                {
                    "id": 1,
                    "name": "맛있어요",
                    "tagType": "TASTE"
                },
                {
                    "id": 3,
                    "name": "단짠딴짠",
                    "tagType": "TASTE"
                },
                {
                    "id": 5,
                    "name": "갓성비",
                    "tagType": "QUANTITY"
                }
            ],
            "content": "test",
            "rebuy": true,
            "favoriteCount": 0,
            "favorite": false,
            "createdAt": "2023-10-27T02:04:45.884962"
        },
        {
            "id": 1,
            "userName": "member1",
            "profileImage": "www.member1.com",
            "image": "1.png",
            "rating": 2,
            "tags": [
                {
                    "id": 1,
                    "name": "맛있어요",
                    "tagType": "TASTE"
                },
                {
                    "id": 4,
                    "name": "아침식사",
                    "tagType": "ETC"
                }
            ],
            "content": "test",
            "rebuy": true,
            "favoriteCount": 0,
            "favorite": false,
            "createdAt": "2023-10-27T02:04:45.836016"
        }
    ],
    "hasNext": false
}

일정

  • 추정 시간: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant