Skip to content

Commit

Permalink
refactor: 성능 개선
Browse files Browse the repository at this point in the history
1. 쿼리 1회 감소
2. select 2번 -> join 1번 쿼리
  • Loading branch information
mikekks committed Oct 31, 2024
1 parent c20587f commit 7870e6a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.sopt.makers.crew.main.global.exception.ErrorStatus.NOT_FOUND_MEETING;

import java.util.List;
import java.util.Optional;

import org.sopt.makers.crew.main.global.exception.BadRequestException;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -28,6 +29,17 @@ default Meeting findByIdOrThrow(Integer meetingId) {
.orElseThrow(() -> new BadRequestException(NOT_FOUND_MEETING.getErrorCode()));
}

default Meeting findByIdWithUserOrThrow(Integer meetingId) {
return findByIdWithUser(meetingId)
.orElseThrow(() -> new BadRequestException(NOT_FOUND_MEETING.getErrorCode()));
}

@Query("SELECT m "
+ "FROM Meeting m "
+ "JOIN fetch m.user "
+ "WHERE m.id =:meetingId ")
Optional<Meeting> findByIdWithUser(Integer meetingId);

@Query("SELECT m FROM Meeting m JOIN FETCH m.user ORDER BY m.id DESC LIMIT 20")
List<Meeting> findTop20ByOrderByIdDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ public MeetingV2CreateMeetingResponseDto createMeeting(MeetingV2CreateMeetingBod
@Override
@Transactional
public MeetingV2ApplyMeetingResponseDto applyMeeting(MeetingV2ApplyMeetingDto requestBody, Integer userId) {
Meeting meeting = meetingRepository.findByIdOrThrow(requestBody.getMeetingId());
User user = userRepository.findByIdOrThrow(userId);
Meeting meeting = meetingRepository.findByIdWithUserOrThrow(requestBody.getMeetingId());
User user = meeting.getUser();
CoLeaders coLeaders = new CoLeaders(coLeaderRepository.findAllByMeetingId(meeting.getId()));

List<Apply> applies = applyRepository.findAllByMeetingId(meeting.getId());
Expand Down Expand Up @@ -379,18 +379,16 @@ public AppliesCsvFileUrlResponseDto getAppliesCsvFileUrl(Integer meetingId, List

@Override
public MeetingV2GetMeetingByIdResponseDto getMeetingById(Integer meetingId, Integer userId) {
User user = userRepository.findByIdOrThrow(userId);

Meeting meeting = meetingRepository.findByIdOrThrow(meetingId);
User meetingLeader = userRepository.findByIdOrThrow(meeting.getUserId());
Meeting meeting = meetingRepository.findByIdWithUserOrThrow(meetingId);
User meetingLeader = meeting.getUser();
CoLeaders coLeaders = new CoLeaders(coLeaderRepository.findAllByMeetingId(meetingId));

Applies applies = new Applies(
applyRepository.findAllByMeetingIdWithUser(meetingId, List.of(WAITING, APPROVE, REJECT), ORDER_ASC));

Boolean isHost = meeting.checkMeetingLeader(user.getId());
Boolean isApply = applies.isApply(meetingId, user.getId());
Boolean isApproved = applies.isApproved(meetingId, user.getId());
Boolean isHost = meeting.checkMeetingLeader(userId);
Boolean isApply = applies.isApply(meetingId, userId);
Boolean isApproved = applies.isApproved(meetingId, userId);
boolean isCoLeader = coLeaders.isCoLeader(meetingId, userId);
long approvedCount = applies.getApprovedCount(meetingId);

Expand Down

0 comments on commit 7870e6a

Please sign in to comment.