Skip to content

fix: 랭킹 조회 시 sort_buffer 초과로 발생하는 Out of sort memory 예외 수정#35

Draft
Jeongho0805 wants to merge 1 commit into
stagefrom
fix/PRACTICKET-5G-rank-query-oom
Draft

fix: 랭킹 조회 시 sort_buffer 초과로 발생하는 Out of sort memory 예외 수정#35
Jeongho0805 wants to merge 1 commit into
stagefrom
fix/PRACTICKET-5G-rank-query-oom

Conversation

@Jeongho0805

Copy link
Copy Markdown
Owner

변경 사항

  • PracticeRankRepositoryImpl.findRankingROW_NUMBER() OVER (PARTITION BY client_key ORDER BY total_duration_ms ASC, id ASC) 윈도우 함수를 CTE + GROUP BY 방식으로 교체
  • 윈도우 함수 정렬을 두 단계 hash aggregation(최소 duration 집계 → 동점 시 최소 id 집계)으로 분리하여 MySQL sort_buffer_size 의존 제거

배경

Sentry 이슈 PRACTICKET-5G: GET /api/practice/rank 요청에서 JpaSystemException → GenericJDBCException → SQLException: Out of sort memory, consider increasing server sort buffer size 예외 발생.

ROW_NUMBER() OVER (PARTITION BY client_key ORDER BY ...) 윈도우 함수는 WHERE type = ? AND started_at >= ? 조건을 만족하는 전체 행을 MySQL이 메모리에서 정렬해야 한다. practice_result 테이블이 커질수록 정렬 대상 데이터가 서버의 sort_buffer_size를 초과해 예외로 이어진다.

CTE + GROUP BY로 교체하면 MySQL 옵티마이저가 sort-based 집계 대신 hash-based 집계를 선택할 수 있어 sort_buffer 의존 없이 클라이언트별 최적 행을 도출한다. 근본적인 해결을 위해서는 (type, started_at, client_key, total_duration_ms, id) 복합 인덱스 생성도 병행 검토 필요.

ROW_NUMBER() 윈도우 함수를 CTE + GROUP BY 방식으로 교체해 MySQL sort_buffer_size 의존 제거
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