Skip to content

fix: Article 1+4N 문제 해결#2268

Open
Soundbar91 wants to merge 4 commits into
developfrom
fix/2266-article-loading
Open

fix: Article 1+4N 문제 해결#2268
Soundbar91 wants to merge 4 commits into
developfrom
fix/2266-article-loading

Conversation

@Soundbar91
Copy link
Copy Markdown
Collaborator

🔍 개요

Image
  • 키워드 알림 API 요청값으로 들어온 articleId들을 통해 Article를 조회하는 과정에서 다음과 같이 쿼리 발생
# 쿼리 전문 횟수
1 select a1_0.id, a1_0.board_id, a1_0.content, a1_0.created_at, a1_0.hit, a1_0.is_deleted, a1_0.is_notice, a1_0.title, a1_0.updated_at from new_articles a1_0 where ( a1_0.is_deleted = ? ) and a1_0.id in ( ? ) 1
2 select k1_0.id, k1_0.article_id, k1_0.created_at, k1_0.is_deleted, k1_0.updated_at, k1_0.user_id from new_koin_articles k1_0 where k1_0.article_id = ? and ( k1_0.is_deleted = ? ) 4
3 select k1_0.id, k1_0.article_id, k1_0.author, k1_0.created_at, k1_0.is_deleted, k1_0.portal_hit, k1_0.portal_num, k1_0.registered_at, k1_0.updated_at, k1_0.url from new_koreatech_articles k1_0 where k1_0.article_id = ? and ( k1_0.is_deleted = ? ) 4
4 select l1_0.id, l1_0.article_id, l1_0.author_id, l1_0.category, l1_0.created_at, l1_0.found_at, l1_0.found_date, l1_0.found_place, l1_0.is_council, l1_0.is_deleted, l1_0.is_found, l1_0.type, l1_0.updated_at from lost_item_articles l1_0 where l1_0.article_id = ? 4
5 select k1_0.id, k1_0.admin_id, k1_0.article_id, k1_0.created_at, k1_0.is_deleted, k1_0.updated_at from koin_notice k1_0 where k1_0.article_id = ? and ( k1_0.is_deleted = ? ) 4
합계 17
  • new_articles과 new_koin_articles, new_koreatech_articles, lost_item_articles, koin_notice의 OneToOne 관계로 인해 발생하는 문제이다.

  • new_articles은 연관관계의 주인이 아니기 때문에 직접 참조하기 전까지는 연관 객체를 가지고 있는지 확인할 수 없다.

  • 그렇기 때문에 Hibernate에서는 new_articles에서 LAZY로 설정했더라도 조회하는 시점에서 연관 객체를 즉시 로딩한다.

  • 해당 문제를 개선한다.

  • close [공통] Article 1+4N 문제 해결 #2266


🚀 주요 변경 내용

  • Fetch Join
    • 키워드 알림 발송에는 new_articles의 id, board_id, title 필드만 필요하다
    • 4개의 자식 테이블을 모두 join하면 사용하지 않는 데이터까지 가져오게 된다.
    • 또한, OneToOne 4개에 대한 LEFT JOIN은 카테시안 곱과 NULL 컬럼 다수를 유발한다. 리소스 낭비라고 판단
  • DTO Projection
    • 필요한 id, board_id, title 만 골라 DTO로 직접 조회한다.
    • Article 엔티티를 로딩하지 않으므로 OneToOne의 즉시 로딩 메커니즘 자체가 트리거되지 않아 N+1을 원천 차단할 수 있다.
    • 또한 네트워크 전송량과 영속성 컨텍스트 부담도 함께 줄어든다.

-> DTO Project으로 Article를 조회하도록 수정


💬 참고 사항


✅ Checklist (완료 조건)

  • 코드 스타일 가이드 준수
  • 테스트 코드 포함됨
  • Reviewers / Assignees / Labels 지정 완료
  • 보안 및 민감 정보 검증 (API 키, 환경 변수, 개인정보 등)

@Soundbar91 Soundbar91 self-assigned this May 25, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 25, 2026

Warning

Review limit reached

@Soundbar91, we couldn't start this review because you've used your available PR reviews for now.

Your plan includes 1 review of capacity. Refill in 5 minutes and 51 seconds.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

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

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 898c3644-631c-476e-9f7e-48b8e535eede

📥 Commits

Reviewing files that changed from the base of the PR and between 872c03b and 228074a.

📒 Files selected for processing (3)
  • src/main/java/in/koreatech/koin/domain/community/article/model/readmodel/ArticleSummary.java
  • src/main/java/in/koreatech/koin/domain/community/article/repository/ArticleRepository.java
  • src/main/java/in/koreatech/koin/domain/community/keyword/service/KeywordService.java
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/2266-article-loading

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 and usage tips.

@github-actions github-actions Bot added 공통 백엔드 공통으로 작업할 이슈입니다. 버그 정상적으로 동작하지 않는 문제상황입니다. labels May 25, 2026
@github-actions github-actions Bot requested review from ImTotem and kih1015 May 25, 2026 05:44
@github-actions
Copy link
Copy Markdown

Unit Test Results

658 tests   655 ✔️  1m 16s ⏱️
163 suites      3 💤
163 files        0

Results for commit 228074a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

공통 백엔드 공통으로 작업할 이슈입니다. 버그 정상적으로 동작하지 않는 문제상황입니다.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[공통] Article 1+4N 문제 해결

1 participant