You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
커뮤니티 응답 생성 경로에 N+1 이 세 군데 있다. 결과는 맞지만 게시글·댓글 수에 비례해
쿼리가 늘어난다.
1. 작성자 프록시 초기화
CommunityMapper.toPostResponse 가 post.getAuthor().getId() 를 호출한다. User 는 필드 접근 엔티티라 Hibernate 가 식별자 게터를 가로채지 못하고, getId() 호출만으로 프록시가 초기화된다. 게시글 수만큼 사용자 조회가 나간다.
2. 태그 컬렉션
post.getTags() 가 지연 로딩이라 게시글마다 1회.
페이징 쿼리에 컬렉션을 join fetch 하면 Hibernate 가 전체 행을 읽어 메모리에서 페이징하므로
(HHH000104) 그 방향은 쓸 수 없다.
3. 댓글 답글 재귀
toCommentResponse 가 comment.getReplies() 를 재귀 호출한다.
댓글 트리의 노드 수만큼 쿼리가 나가고, 깊이가 깊을수록 급격히 나빠진다.
Activity