Skip to content

perf: PublicCourseService.deletePublicCourses N+1 제거 - #247

Merged
unam98 merged 1 commit into
mainfrom
perf/fix-public-course-delete-n-plus-1
Aug 12, 2026
Merged

perf: PublicCourseService.deletePublicCourses N+1 제거#247
unam98 merged 1 commit into
mainfrom
perf/fix-public-course-delete-n-plus-1

Conversation

@unam98

Copy link
Copy Markdown
Collaborator

작업 배경

  • 이전 대화에서 나온 N+1 후보(PublicCourseRepository.findByIdIn())를 실제로 검증하지 않고 남겨뒀던 항목
  • 코드 추적 결과 PublicCourseService.deletePublicCourses에서 findByIdIn으로 조회한 PublicCourse 목록을 순회하며 getCourse()(OneToOne LAZY), getCourse().getRunnectUser()(ManyToOne LAZY), getRecords()(OneToMany LAZY)를 각각 호출 — 삭제 대상이 N개면 최대 3N+1 쿼리가 나가는 구조였음

변경 사항

영역내용
PublicCourseRepository.javafindByIdInWithCourseAndRecords 추가 — course/course.runnectUser/records를 JOIN FETCH로 한 번에 즉시로딩
PublicCourseService.javadeletePublicCourses에서 findByIdInfindByIdInWithCourseAndRecords 교체
PublicCourseServiceTest.java위 변경에 맞춰 기존 4개 테스트의 mock 대상 메서드명 갱신

선택지 및 근거

  • 새 쿼리는 즉흥적으로 만든 게 아니라, 같은 리포지토리의 findById()가 이미 프로덕션에서 쓰고 있는 JOIN FETCH pc.course 패턴을 그대로 확장한 것 — JOIN FETCH c.runnectUser, LEFT JOIN FETCH pc.records 추가는 이미 검증된 패턴의 자연스러운 연장

영향 범위

  • deletePublicCourses(공개 코스 삭제 API) 쿼리 수만 변경, 응답/동작은 동일
  • 대량 삭제 시일수록 쿼리 수 절감 효과가 커짐 (N개 삭제 시 최대 3N+1 → 1)

검증 관련 특이사항

  • 로컬 @DataJpaTest로 실제 DB에 대해 JOIN FETCH 즉시로딩 여부를 검증하려 했으나, 로컬 Docker Postgres 컨테이너의 PostGIS 공유 라이브러리 결함($libdir/postgis-3 없음 — 이 세션에서 이미 겪은 것과 동일한 로컬 전용 이슈)과 PgJDBC 드라이버의 타입 조회 방식이 얽혀 course 테이블에 새 row를 INSERT하는 것 자체가 로컬에서 막힘. raw psql/PREPARE·EXECUTE로는 동일 SQL이 정상 동작해, 코드 로직이 아니라 로컬 환경 결함으로 판단해 해당 통합 테스트는 제외함.
  • 대신 다음 두 가지로 검증을 대체: (1) ServerApplicationTests.contextLoads()가 새 @Query JPQL을 엔티티 메타모델 기준으로 파싱/검증하며 통과 (2) 서비스 계층 mock 테스트 4건 갱신 후 통과

Test Plan

  • 로컬 전체 테스트 254개 통과 (./gradlew test)
  • 코드 추적으로 N+1 실재 여부 확인 (FetchType.LAZY 3곳)
  • 실제 DB 대상 JOIN FETCH 즉시로딩 통합 테스트 — 로컬 환경 결함으로 보류 (근본 원인은 코드가 아닌 로컬 PostGIS 라이브러리)

🤖 Generated with Claude Code

findByIdIn으로 조회한 PublicCourse의 course(OneToOne LAZY)/
runnectUser(ManyToOne LAZY)/records(OneToMany LAZY)를 각각
순회하며 지연로딩을 트리거해 최대 3N+1 쿼리가 나가고 있었음.
JOIN FETCH로 한 번에 즉시로딩하는 findByIdInWithCourseAndRecords로 교체.
기존 findById()에 이미 쓰이던 JOIN FETCH 패턴을 그대로 확장한 것.
@coderabbitai

Copy link
Copy Markdown

Warning

Review limit reached

@unam98, you've reached your PR review limit, so we couldn't start this review.

Next review available in:10 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ee752686-2737-4261-a31f-5e340f91b93e

📥 Commits

Reviewing files that changed from the base of the PR and between 2bedaa3 and d019ebd.

📒 Files selected for processing (3)
  • src/main/java/org/runnect/server/publicCourse/repository/PublicCourseRepository.java
  • src/main/java/org/runnect/server/publicCourse/service/PublicCourseService.java
  • src/test/java/org/runnect/server/publicCourse/service/PublicCourseServiceTest.java

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@unam98
unam98 merged commit 91d7566 into mainAug 12, 2026
2 checks passed
@unam98
unam98 deleted the perf/fix-public-course-delete-n-plus-1 branch August 12, 2026 09:17
Sign up for freeto 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.

2 participants

@unam98@alh0409