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

refactor: select in을 통해 n+1해소 #587

Open
wants to merge 1 commit into
base: BE/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yigongil.backend.domain.study.studyquery;

import static com.yigongil.backend.domain.member.QMember.member;
import static com.yigongil.backend.domain.study.QStudy.study;
import static com.yigongil.backend.domain.studymember.QStudyMember.studyMember;

Expand Down Expand Up @@ -32,13 +33,20 @@ public StudyQueryRepositoryImpl(JPAQueryFactory queryFactory) {
@Override
public Slice<Study> findStudiesByConditions(String search, ProcessingStatus status, Pageable page) {

List<Study> studies = queryFactory.selectFrom(study)
List<Long> studyIds = queryFactory.select(study.id)
.from(study)
.where(searchEq(search), statusEq(status))
.offset(page.getOffset())
.limit(page.getPageSize())
.orderBy(orderSpecifier(page))
.fetch();

List<Study> studies = queryFactory.selectFrom(study)
.join(study.studyMembers, studyMember).fetchJoin()
.join(studyMember.member, member).fetchJoin()
.where(study.id.in(studyIds))
.orderBy(orderSpecifier(page))
.fetch();
Comment on lines -35 to +49
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

궁금한 점이 두 가지 있습니다.

  1. 실제로 이렇게 쿼리를 두 번 날리는 것이 평균적으로 얼마나 더 빠른지
  2. 기존의 n+1 이 어떤 과정에서 일어나는지

실제 실행기록을 첨부해주실 수 있을까요?

return new SliceImpl<>(studies);
}

Expand Down
2 changes: 2 additions & 0 deletions backend/src/test/resources/features/find-study.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Feature: 스터디 목록 조회 기능
Given "jinwoo"가 제목-"자바1", 정원-"6"명, 최소 주차-"7"주, 주당 진행 횟수-"3"회, 소개-"스터디소개1"로 스터디를 개설한다.
Given "jinwoo"가 제목-"자바2", 정원-"6"명, 최소 주차-"7"주, 주당 진행 횟수-"3"회, 소개-"스터디소개1"로 스터디를 개설한다.
Given "jinwoo"가 제목-"자스", 정원-"6"명, 최소 주차-"7"주, 주당 진행 횟수-"3"회, 소개-"스터디소개1"로 스터디를 개설한다.
Given "jinwoo"가 제목-"자스", 정원-"6"명, 최소 주차-"7"주, 주당 진행 횟수-"3"회, 소개-"스터디소개1"로 스터디를 개설한다.
Given "jinwoo"가 제목-"자스", 정원-"6"명, 최소 주차-"7"주, 주당 진행 횟수-"3"회, 소개-"스터디소개1"로 스터디를 개설한다.
Comment on lines +32 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

두개가 늘어난 이유는 뭔가요?


When 스터디 목록을 "자바" 검색어와 조회한다.

Expand Down