Skip to content

Commit a290f9a

Browse files
authored
Merge pull request #168 from GoToBILL/fix
refactor: findByKeywordFullTex 쿼리 최적화
2 parents e72708b + 0447f0f commit a290f9a

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/main/java/com/example/cherrydan/campaign/repository/CampaignRepository.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ public interface CampaignRepository extends JpaRepository<Campaign, Long>, JpaSp
4343

4444
// 키워드 맞춤형 캠페인 FULLTEXT 검색 (지정 날짜 기준 전일)
4545
@Query(value = """
46-
SELECT * FROM campaigns
47-
WHERE MATCH(title) AGAINST(:keyword IN BOOLEAN MODE)
48-
AND is_active = 1
49-
AND DATE(created_at) = DATE(:date - INTERVAL 1 DAY)
50-
ORDER BY created_at DESC
46+
SELECT
47+
c.*
48+
FROM (
49+
SELECT
50+
id
51+
FROM campaigns FORCE INDEX(campaigns_created_at_is_active_IDX)
52+
WHERE created_at >= DATE(:date - INTERVAL 1 DAY) AND created_at < DATE(:date)
53+
AND is_active = 1
54+
) AS filtered
55+
STRAIGHT_JOIN campaigns AS c ON c.id = filtered.id
56+
WHERE MATCH(c.title) AGAINST(:keyword IN BOOLEAN MODE)
57+
ORDER BY c.id DESC
5158
LIMIT :offset, :limit
5259
""", nativeQuery = true)
5360
List<Campaign> findByKeywordFullText(
@@ -59,10 +66,17 @@ List<Campaign> findByKeywordFullText(
5966

6067
// 지정 날짜 기준 전일 생성된 키워드 맞춤형 캠페인 개수
6168
@Query(value = """
62-
SELECT COUNT(*) FROM campaigns
63-
WHERE MATCH(title) AGAINST(:keyword IN BOOLEAN MODE)
64-
AND is_active = 1
65-
AND DATE(created_at) = DATE(:date - INTERVAL 1 DAY)
69+
SELECT
70+
COUNT(*)
71+
FROM (
72+
SELECT
73+
id
74+
FROM campaigns FORCE INDEX(campaigns_created_at_is_active_IDX)
75+
WHERE created_at >= DATE(:date - INTERVAL 1 DAY) AND created_at < DATE(:date)
76+
AND is_active = 1
77+
) AS filtered
78+
STRAIGHT_JOIN campaigns AS c ON c.id = filtered.id
79+
WHERE MATCH(c.title) AGAINST(:keyword IN BOOLEAN MODE)
6680
""", nativeQuery = true)
6781
long countByKeywordAndCreatedDate(@Param("keyword") String keyword, @Param("date") LocalDate date);
6882
}

src/main/java/com/example/cherrydan/campaign/service/CampaignServiceImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ public Page<CampaignResponseDTO> getPersonalizedCampaignsByKeyword(String keywor
158158
);
159159

160160
long totalElements = campaignRepository.countByKeywordAndCreatedDate(fullTextKeyword, date);
161-
162161
// N+1 문제 해결: 벌크 조회로 북마크 여부 확인
163162
List<Long> campaignIds = campaigns.stream()
164163
.map(Campaign::getId)

0 commit comments

Comments
 (0)