Skip to content

Commit

Permalink
refactor: n+1 발생하는 쿼리 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
lilychoibb committed Sep 17, 2024
1 parent 8ade7dc commit 8161b67
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public DiscussionService(
}

public List<SummarizedDiscussionResponse> getSummaries(String mission, String hashTagName) {
return discussionRepository.findByMissionAndHashTagName(mission, hashTagName).stream()
return discussionRepository.findAllByMissionAndHashTagName(mission, hashTagName).stream()
.map(SummarizedDiscussionResponse::from)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ public interface DiscussionRepository extends JpaRepository<Discussion, Long> {
AND sht.name = :hashTag
))
""")
List<Discussion> findByMissionAndHashTagName(
List<Discussion> findAllByMissionAndHashTagName(
@Param("mission") String mission,
@Param("hashTag") String hashTagName
);

@Query("""
SELECT d
FROM Discussion d
JOIN FETCH d.mission m
JOIN FETCH d.member me
JOIN FETCH d.discussionHashTags.hashTags dhts
JOIN FETCH dhts.hashTag ht
WHERE me.id = :memberId
""")
List<Discussion> findAllByMember_Id(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,15 @@ void createWithUnknownHashTag() {

@Test
@DisplayName("나의 디스커션 리스트를 조회한다.")
@Transactional
void getDiscussionsByMemberId() {
Member member = memberRepository.save(MemberTestData.defaultMember().withId(1L).build());
Mission mission = missionRepository.save(MissionTestData.defaultMission().build());
HashTag hashTag = hashTagRepository.save(HashTagTestData.defaultHashTag().build());
Discussion discussion = DiscussionTestData.defaultDiscussion()
.withMember(member)
.withMission(mission)
.withHashTags(List.of(hashTag))
.build();

discussionRepository.save(discussion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void findAllDiscussion() {
createDiscussion(mission, hashTag);
createDiscussion(mission, hashTag);

List<Discussion> actual = discussionRepository.findByMissionAndHashTagName(
List<Discussion> actual = discussionRepository.findAllByMissionAndHashTagName(
"all",
"all"
);
Expand All @@ -63,7 +63,7 @@ void findAllDiscussionByHashTag() {
createDiscussion(mission, hashTag1);
createDiscussion(mission, hashTag2);

List<Discussion> discussions = discussionRepository.findByMissionAndHashTagName(
List<Discussion> discussions = discussionRepository.findAllByMissionAndHashTagName(
"all",
"JAVA"
);
Expand All @@ -85,7 +85,7 @@ void findAllDiscussionByMission() {
createDiscussion(mission1, hashTag);
createDiscussion(mission2, hashTag);

List<Discussion> discussions = discussionRepository.findByMissionAndHashTagName(
List<Discussion> discussions = discussionRepository.findAllByMissionAndHashTagName(
"루터회관 흡연단속",
"all"
);
Expand All @@ -97,18 +97,22 @@ void findAllDiscussionByMission() {

@Test
@DisplayName("멤버 식별자를 통해 디스커션을 조회한다.")
@Transactional
void findByMember_Id() {
Member member1 = memberRepository.save(MemberTestData.defaultMember().withId(1L).build());
Member member2 = memberRepository.save(MemberTestData.defaultMember().withId(2L).build());
Mission mission = missionRepository.save(MissionTestData.defaultMission().build());
HashTag hashTag = hashTagRepository.save(HashTagTestData.defaultHashTag().build());

Discussion discussionByMember1 = DiscussionTestData.defaultDiscussion()
.withMission(mission)
.withMember(member1)
.withHashTags(List.of(hashTag))
.build();
Discussion discussionByMember2 = DiscussionTestData.defaultDiscussion()
.withMission(mission)
.withMember(member2)
.withHashTags(List.of(hashTag))
.build();

discussionRepository.save(discussionByMember1);
Expand Down

0 comments on commit 8161b67

Please sign in to comment.