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 문제 개선 #623

Merged
merged 1 commit into from
Oct 6, 2023
Merged
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
Expand Up @@ -38,4 +38,7 @@ Page<Study> findPageByMemberIdAndCreatedDate(
@Param("endDate") LocalDateTime endDate,
Pageable pageable
);

@Query("select s from Study s join s.participants p where p.member.id = :memberId")
List<Study> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package harustudy.backend.study.service;

import harustudy.backend.auth.dto.AuthMember;
import harustudy.backend.member.domain.Member;
import harustudy.backend.member.exception.MemberNotFoundException;
import harustudy.backend.member.repository.MemberRepository;
import harustudy.backend.participant.domain.Participant;
import harustudy.backend.participant.repository.ParticipantRepository;
import harustudy.backend.participantcode.domain.GenerationStrategy;
import harustudy.backend.participantcode.domain.ParticipantCode;
import harustudy.backend.participantcode.repository.ParticipantCodeRepository;
Expand All @@ -16,20 +11,19 @@
import harustudy.backend.study.exception.ParticipantCodeNotFoundException;
import harustudy.backend.study.exception.StudyNotFoundException;
import harustudy.backend.study.repository.StudyRepository;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
@Transactional
@Service
public class StudyService {

private final StudyRepository studyRepository;
private final ParticipantRepository participantRepository;
private final MemberRepository memberRepository;
private final GenerationStrategy generationStrategy;
private final ParticipantCodeRepository participantCodeRepository;

Expand All @@ -55,22 +49,10 @@ public StudiesResponse findStudyWithFilter(Long memberId, String code) {
}

private StudiesResponse findStudyByMemberId(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);
List<Participant> participants = participantRepository.findByMember(member);

List<Study> studies = mapToStudies(participants);

List<Study> studies = studyRepository.findByMemberId(memberId);
return StudiesResponse.from(studies);
}

// TODO: N+1
private List<Study> mapToStudies(List<Participant> participants) {
return participants.stream()
.map(Participant::getStudy)
.toList();
}

public Long createStudy(CreateStudyRequest request) {
Study study = new Study(request.name(), request.totalCycle(),
request.timePerCycle());
Expand Down