Skip to content

Conversation

@byunhm02
Copy link
Collaborator

@byunhm02 byunhm02 commented Dec 26, 2025

🔥Pull requests

👷 과제 구현

필수과제

  • 앱잼신청
  • 기경 및 밋업 적극적 참여
  • 댓글기능 추가

선택과제

  • 캐싱으로 성능 최적화
  • 대용량 데이터 처리 및 정렬 최적화
  • 인프라 설계 시각화
  • 테스트코드 도입

구현한 내용에 대해서 설명해주세요

  • 댓글 CRUD 기능 구현
    • 댓글 작성, 조회, 수정, 삭제
    • 게시글 상세 조회 시 댓글 목록 함께 반환
    • 게시글별 댓글 목록 조회
  • Swagger UI 적용으로 API 문서화

1. Comment 엔티티

@Entity
@Table(name = "comments", indexes = {
    @Index(name = "idx_comment_article_id", columnList = "articleId"),
    @Index(name = "idx_comment_member_id", columnList = "memberId"),
    @Index(name = "idx_comment_article_created", columnList = "articleId, createdDate")
})
public class Comment {
    // 300자 제한
    @Column(nullable = false, length = 300)
    private String content;
}

2. Repository 최적화

// N+1 문제 해결: Fetch Join 활용
@Query("SELECT a FROM Article a JOIN FETCH a.member ORDER BY a.createdDate DESC")
List findAllWithMember();

@Query("SELECT DISTINCT a FROM Article a " +
       "JOIN FETCH a.member " +
       "LEFT JOIN FETCH a.comments c " +
       "LEFT JOIN FETCH c.member " +
       "WHERE a.id = :id")
Optional findByIdWithMemberAndComments(@Param("id") Long id);

3. Validation

// 댓글 300자 제한
@NotBlank(message = "댓글 내용은 필수입니다.")
@Size(max = 300, message = "댓글은 300자 이내로 작성해주세요.")
String content;

5. API 문서화

  • Swagger UI로 API 테스트 가능

구현하며 고민했던 내용을 적어주세요 (사소한 것도 좋아요)

고민한 지점

  • 어느 컬럼에 인덱스를 추가해야 효과적일까?
  • 복합 인덱스의 컬럼 순서는 어떻게 정해야 할까?
  • 인덱스 추가로 인한 쓰기 성능 저하는 감수할 만한가?

적용한 인덱스

-- Article 테이블
idx_article_member_id (memberId)              -- JOIN 최적화
idx_article_created_date (createdDate)        -- 정렬 최적화
idx_article_category (category)               -- 카테고리 필터링
idx_article_category_created (category, createdDate)  -- 복합 조건 최적화

-- Comment 테이블
idx_comment_article_id (articleId)            -- 게시글별 댓글 조회
idx_comment_member_id (memberId)              -- JOIN 최적화
idx_comment_article_created (articleId, createdDate)  -- 댓글 정렬

트레이드오프 분석

장점

  • 조회 성능 향상
  • ORDER BY 정렬 속도 향상
  • JOIN 성능 개선
  • 읽기 중심 서비스에 최적화

단점

  • 쓰기 성능 저하
    • INSERT 시 인덱스 테이블도 함께 갱신
  • 저장 공간 증가
    • 100만 건 기준: 원본 500MB + 인덱스 200MB
  • 인덱스 유지보수 비용 발생
  • 메모리 사용량 증가

3. N+1 문제 해결

문제 상황

// 기존 코드 - N+1 발생
List articles = articleRepository.findAll();
// 1번의 쿼리로 게시글 조회
// N번의 쿼리로 각 게시글의 Member 조회 (N+1 문제)

해결 방법

// Fetch Join 적용
@Query("SELECT a FROM Article a JOIN FETCH a.member ORDER BY a.createdDate DESC")
List findAllWithMember();
// 1번의 쿼리로 게시글과 Member를 함께 조회

효과

  • 쿼리 수: 1 + N → 1개로 감소


키워드 과제 정리내용



🚨 참고 사항

@byunhm02 byunhm02 linked an issue Dec 26, 2025 that may be closed by this pull request
@byunhm02 byunhm02 self-assigned this Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SEMINAR] 7주차 세미나

2 participants