Skip to content

perf: 지연 로딩 연관을 배치로 읽어 회원 목록의 N+1 쿼리를 없앤다 - #150

Merged
ff1451 merged 2 commits into
mainfrom
perf/batch-fetch
Oct 18, 2026
Merged

ff1451 merged 2 commits into
mainfrom
perf/batch-fetch

Conversation

@ff1451

@ff1451 ff1451 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

요약

hibernate.default_batch_fetch_size가 설정돼 있지 않아, 목록을 돌며 지연 로딩 연관을 읽는 곳마다 행 수만큼 쿼리가 더 나갔다(N+1). 대표적으로 회원 명부는 회원마다 직책(@ManyToMany) 쿼리를 하나씩 보냈다. 클라이언트가 size=2000도 보낼 수 있어 회원 수가 늘수록 커진다.

변경

  • spring.jpa.properties.hibernate.default_batch_fetch_size: 100 — 같은 종류의 지연 로딩을 최대 100개씩 IN 쿼리 하나로 묶는다. 응답·트랜잭션 동작은 그대로이고 쿼리 수만 줄어든다. 멘토·모집 이력·관리자 배정 목록·커리큘럼 트리 등 같은 패턴의 다른 목록에도 함께 적용된다.

측정

MemberDirectoryQueryCountIntegrationTest — 회원 21명(직책·트랙 포함)을 관리자 명부로 조회할 때 Hibernate가 준비한 SQL 수:

SQL 수
이전 40
이후 10

테스트는 15개 미만을 요구해, 회원마다 쿼리가 늘어나는 회귀를 막는다.

테스트

  • 로컬 전체 ./gradlew test: 536건 중 535 통과, 1 스킵.

Summary by CodeRabbit

  • Performance
    • Improved loading efficiency for member directory pages by fetching related member information in batches. This can reduce the number of database queries needed to display a page of results, particularly when the directory contains many members.

hibernate.default_batch_fetch_size가 없어, 회원 명부처럼 목록을 돌며 직책(@manytomany)·트랙을
읽는 곳이 회원마다 쿼리를 하나씩 더 보냈다. 회원 21명 관리자 명부 조회가 SQL 40개에서 10개로 준다.
@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c2e1a6e3-efe3-450b-bc3e-cca9a946cb15
📥 Commits

Reviewing files that changed from the base of the PR and between b3af697 and 3006f02.

📒 Files selected for processing: 2
  • src/main/resources/application.yml
  • src/test/java/com/bcsdlab/bcsdinternalapiv2/member/MemberDirectoryQueryCountIntegrationTest.java

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

📝 Walkthrough

Hibernate now uses a default batch fetch size of 100 for lazy associations. A new integration test requests the member directory and checks the response size and prepared statement count.

Changes: Member directory batch fetching

Layer / File(s) Summary
Batch-fetch configuration and query-count test
src/main/resources/application.yml, src/test/java/com/bcsdlab/bcsdinternalapiv2/member/MemberDirectoryQueryCountIntegrationTest.java
Hibernate's default batch fetch size is set to 100. The test authenticates as an admin, requests the directory with page size 50, checks for 21 members, and asserts fewer than 15 prepared statements.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: hgjwilly

Merge Risk: ⚪ Minimal · up to 3006f

The batch-fetch change has no identified merge-blocking issue after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 1 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: batching lazy-loaded associations to reduce N+1 queries in the member list.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 1 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@ff1451
ff1451 merged commit db9cb8f into main Oct 18, 2026
2 checks passed
@ff1451
ff1451 deleted the perf/batch-fetch branch October 18, 2026 13:51
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.

1 participant