Skip to content

Commit

Permalink
[BE] 북마크 알림 리팩토링 (JOIN FETCH 사용한 N+1 문제 해결)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinwook94 committed Nov 22, 2023
1 parent 0c60d14 commit e02141f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Expand Up @@ -2,7 +2,9 @@

import com.bootme.bookmark.domain.CourseBookmark;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

Expand All @@ -14,4 +16,12 @@ public interface CourseBookmarkRepository extends JpaRepository<CourseBookmark,

List<CourseBookmark> findByBookmark_Id(Long bookmarkId);

@Query("SELECT cb FROM CourseBookmark cb " +
"JOIN FETCH cb.course c " +
"JOIN FETCH cb.bookmark b " +
"JOIN FETCH b.member m " +
"WHERE c.dates.registrationStartDate <= :date " +
"AND c.dates.registrationEndDate >= :date")
List<CourseBookmark> findAllWithCourseAndMemberOnDate(LocalDate date);

}
Expand Up @@ -67,10 +67,9 @@ public void notifyCourseRegistrationEnd() {
// todo: N+1 문제 → JOIN FETCH 사용한 해결 필요
@Transactional
public void notifyBookmarkCourses(NotificationEventType event, LocalDate date) {
List<CourseBookmark> courseBookmarks = courseBookmarkRepository.findAll();
List<CourseBookmark> courseBookmarks = courseBookmarkRepository.findAllWithCourseAndMemberOnDate(date);

List<Notification> notifications = courseBookmarks.stream()
.filter(cb -> cb.getCourse().isEventOnDate(event, date))
.map(cb -> notificationFactory.createCourseBookmarkNotification(cb.getBookmark().getMember(), event, cb))
.toList();

Expand Down

0 comments on commit e02141f

Please sign in to comment.