Skip to content

Commit b3dab1a

Browse files
committed
refactor: CheerService 에서 SQL 쿼리 횟수 최적화
1 parent bef141b commit b3dab1a

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/main/java/eatda/repository/cheer/CheerRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
public interface CheerRepository extends JpaRepository<Cheer, Long> {
1515

16-
@EntityGraph(attributePaths = {"store", "member", "cheerTags"})
16+
@EntityGraph(attributePaths = {"store", "member", "cheerTags.values"})
1717
List<Cheer> findAllByOrderByCreatedAtDesc(Pageable pageable);
1818

19-
@EntityGraph(attributePaths = {"member", "cheerTags"})
20-
List<Cheer> findAllByStoreOrderByCreatedAtDesc(Store store, Pageable pageable);
19+
@EntityGraph(attributePaths = {"member", "cheerTags.values"})
20+
List<Cheer> findAllByStoreIdOrderByCreatedAtDesc(Long storeId, Pageable pageable);
2121

2222
@Query("""
2323
SELECT c.imageKey FROM Cheer c

src/main/java/eatda/service/cheer/CheerService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ private CheersResponse toCheersResponse(List<Cheer> cheers) {
7575

7676
@Transactional(readOnly = true)
7777
public CheersInStoreResponse getCheersByStoreId(Long storeId, int page, int size) {
78-
Store store = storeRepository.getById(storeId);
79-
List<Cheer> cheers = cheerRepository.findAllByStoreOrderByCreatedAtDesc(store, PageRequest.of(page, size));
78+
List<Cheer> cheers = cheerRepository.findAllByStoreIdOrderByCreatedAtDesc(storeId, PageRequest.of(page, size));
8079

8180
List<CheerInStoreResponse> cheersResponse = cheers.stream()
8281
.map(CheerInStoreResponse::new)
83-
.toList(); // TODO N+1 문제 해결
82+
.toList();
8483
return new CheersInStoreResponse(cheersResponse);
8584
}
8685
}

0 commit comments

Comments
 (0)