Skip to content

Commit

Permalink
♻️ Refactor: 친구 목록 조회시 성능 개선
Browse files Browse the repository at this point in the history
<body>
N+1 문제 방지 및 성능 개선을 위한 friend 정보 fetch join 처리

- 관련 : #384
  • Loading branch information
hosung-222 committed Oct 23, 2024
1 parent 557c4c5 commit 2d0e16f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.namo.spring.db.mysql.domains.user.dto.FriendBirthdayQuery;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.namo.spring.db.mysql.domains.user.entity.Friendship;
import com.namo.spring.db.mysql.domains.user.type.FriendshipStatus;
Expand Down Expand Up @@ -51,5 +52,8 @@ List<FriendBirthdayQuery> findBirthdayVisibleFriendIdsByPeriod(

Optional<Friendship> findByMemberIdAndFriendIdAndStatus(Long memberId, Long friendId, FriendshipStatus status);

Page<Friendship> findAllByMemberIdAndStatus(Long memberId, FriendshipStatus status, Pageable pageable);
@Query("SELECT f FROM Friendship f "
+ "JOIN FETCH f.friend "
+ "WHERE f.member.id = :memberId AND f.status = :status")
Page<Friendship> findAllByMemberIdAndStatusFetchJoin(@Param("memberId") Long memberId, @Param("status") FriendshipStatus status, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Page<Friendship> readAllReceivedFriendshipByStatus(Long memberId, Friends
* memberId(나)에서 시작한 친구 관계로 탐색
*/
public Page<Friendship> readAllRequestFriendshipByStatus(Long memberId, FriendshipStatus status, Pageable pageable) {
return friendshipRepository.findAllByMemberIdAndStatus(memberId, status, pageable);
return friendshipRepository.findAllByMemberIdAndStatusFetchJoin(memberId, status, pageable);
}

public Optional<Friendship> readFriendshipByStatus(Long friendshipId, FriendshipStatus status){
Expand Down

0 comments on commit 2d0e16f

Please sign in to comment.