Skip to content

![high](https://www.gstatic.com/codereviewagent/high-priority.svg) #132

@huhdy32

Description

@huhdy32

high

현재 구현은 Solver 목록을 조회한 후, 각 Solver에 대해 mapToRankQueryResult 메소드 내에서 solver.getSolveLogs()를 접근하여 RankItemResult를 생성합니다. solveLogs는 지연 로딩(lazy-loading)으로 설정되어 있고 초기 쿼리에서 함께 조회되지 않기 때문에, 각 Solver의 로그를 가져오기 위해 별도의 쿼리가 실행됩니다. 이는 페이지 크기(N)만큼 추가적인 쿼리를 발생시키는 N+1 문제입니다.

제안:
Solver 엔티티를 조회한 후 지연 로딩에 의존하는 대신, 데이터베이스에서 직접 totalSubmittedCountsuccessCount를 계산하는 DTO 프로젝션을 사용하는 것을 고려해보세요. 이렇게 하면 solveLogs 컬렉션 전체를 메모리로 로드하는 것을 피하고 쿼리 수를 줄일 수 있습니다.

예를 들어, Solver 목록을 조회한 후 해당 solver들의 ID를 사용하여 아래와 같은 쿼리로 통계 정보를 한 번에 조회하고, 이를 조합하여 응답을 구성할 수 있습니다.

// 새로운 DTO 예시
public record RankItemStats(Long solverId, long totalSubmittedCount, long successCount) {}

// SolverRepository에 추가할 메소드 예시
@Query("""
    SELECT new kr.co.mathrank.domain.rank.dto.RankItemStats(
        s.id, 
        COUNT(sl.id), 
        SUM(CASE WHEN sl.success = true THEN 1 ELSE 0 END)
    )
    FROM Solver s LEFT JOIN s.solveLogs sl ON s.id = sl.solver.id
    WHERE s.id IN :solverIds
    GROUP BY s.id
""")
List<RankItemStats> findRankItemsStats(@Param("solverIds") List<Long> solverIds);

Originally posted by @gemini-code-assist[bot] in #129 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions