Skip to content

Refactor: resolve N+1 query problem in ChapterService using aggregation#197

Merged
SolfE merged 1 commit intodevelopfrom
refactor/improve-chapter-query-performance
Oct 14, 2025
Merged

Refactor: resolve N+1 query problem in ChapterService using aggregation#197
SolfE merged 1 commit intodevelopfrom
refactor/improve-chapter-query-performance

Conversation

@SolfE
Copy link
Copy Markdown
Member

@SolfE SolfE commented Oct 14, 2025

Summary

chapter 목록 조회 N+1 문제 해결

Problem

GET /api/books/{bookId}/chapters API 호출 시, 챕터 목록을 조회하는 과정에서 각 챕터별로 청크 개수(chunk count)를 조회하는 추가 쿼리가 발생하여 N+1 문제가 나타났습니다.

Solution

MongoDB의 Aggregation Pipeline을 사용하여, 조회 대상인 모든 챕터의 난이도별 청크 개수를 단 한 번의 쿼리로 미리 조회하도록 변경했습니다.

Related Issues

closes #195

@SolfE
Copy link
Copy Markdown
Member Author

SolfE commented Oct 14, 2025

k6 부하 테스트 결과

요약

  • 테스트 시간: 1분 40.9초 (개선 전) / 1분 40.0초 (개선 후)
  • 가상 유저(VUs): 최대 20명
  • 총 요청 수: 683회 (개선 전) / 1,525회 (개선 후)
  • 성공률: ✅ 100%

상세 결과 (개선 전 vs 개선 후)

항목 (Metric) 개선 전 (Before) 개선 후 (After)
평균 응답 시간 (avg) 1.38s 56.8ms
최소 응답 시간 (min) 533.11ms 24.21ms
중앙값 응답 시간 (med) 1.41s 56.62ms
최대 응답 시간 (max) 2.54s 161.11ms
p(90) 1.83s 65.88ms
p(95) 2.17s 70.57ms
초당 요청 수 (RPS) 6.77 req/s 15.25 req/s
데이터 수신 45 MB 100 MB
데이터 송신 121 kB 270 kB

@SolfE SolfE merged commit 381ecde into develop Oct 14, 2025
1 check passed
@SolfE SolfE deleted the refactor/improve-chapter-query-performance branch October 14, 2025 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GET chapters api 성능 개선

1 participant