Repository navigation
feat: 일별 미션 수행 통계 — 조회 비용을 누적량 비례에서 조회 기간 비례로 전환 - #176
Merged
Merged
Conversation
- (user_id, mission_id, completed_date) 유니크 제약 — outbox 멱등성 키와 같은 구조를 원본 테이블 제약으로 승격, 동시 요청 레이스 백스톱 확보 - validateSameMission을 '평생 1회'에서 '같은 날 1회'로 변경 — 멱등성 키의 날짜 포함 설계와 모순 해소, 다른 날 재수행 허용 - exists 체크를 동시에 통과한 요청은 saveCompletion에서 제약 위반을 MISSION_ALREADY_COMPLETED로 변환, 트랜잭션 전체 롤백 - outbox completedDate를 created_at 파생이 아닌 completed_date 컬럼에서 직접 사용 - 운영 반영 시 백필 필요: UPDATE user_mission_completion SET completed_date = DATE(created_at) 후 유니크 인덱스 생성 (기존 중복 행 정리 선행) (cherry picked from commit f6baa310088784275a1fc347e56c725d09beef98)
- JdbcTemplateLockProvider + usingDbTime(인스턴스 시계 오차 무관), shedlock 테이블은 ddl-auto 생성용 엔티티로 선언 - 임대(lease) 방식이라 만료 엣지의 중복 실행까지는 막지 못함 — 락이 확률을 줄이고 각 작업의 멱등성이 정확성을 보장하는 이중 구조
- 조회를 확정 구간(사전 집계된 daily_completion_stats)과 당일 구간(원본 실시간 집계)으로 분리해 UNION ALL 한 문장으로 병합 — 조회 비용이 누적 데이터량이 아닌 조회 기간(날짜 수)에 비례 - 새벽 2시 확정 배치: '마지막 확정일 다음날 ~ 어제'를 절대값 재집계해 DELETE+INSERT(멱등) — 재실행·동시 실행 무해, 캐치업 내장 - 확정된 날짜는 동결 — 이후 원본이 삭제돼도 통계는 소급 변경 안 함 - 경계를 '오늘'이 아닌 마지막 확정일로 잡아 00:00~02:00 사이 어제 증발 방지 - 성능 비교용 원본 직접 집계 API(/raw)와 통계 조회 화이트리스트(permitAll) 포함
- 01-seed: user_mission_completion 1,000만 건(1년 균등) 시드 - 02-experiment: 풀스캔 vs completed_date 인덱스 vs 통계 테이블 하이브리드 3단 비교 (EXPLAIN ANALYZE) - 04-oltp-contention: 무거운 통계 집계가 고객 트래픽 레이턴시에 전파되는지 관측 - 측정: 1년 조회 원본 직접 집계 ~5,000ms(수백만 행 스캔) → 하이브리드 ~23ms
|
Caution Review failedThe pull request is closed.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
한 줄 요약
통계 조회 비용을 누적 데이터량 비례 → 조회 기간(날짜 수) 비례로 전환.
누적 1,000만 건 기준 1년 통계 조회 ~5,000ms → ~23ms.
무엇이 문제였나
과거 추이와 오늘 수치를 함께 보여주는 미션 수행 일별 통계가, 매 조회마다 원본
user_mission_completion을 전량 스캔해 날짜별로 집계하는 구조였다.(통계는 어드민/대시보드 성격인데 OLTP 트래픽을 끌어내림 —
04-oltp-contention.sql에서 재현).어떻게 해결했나
핵심 관찰 두 가지:
→ 조회를 확정 구간과 당일 구간으로 분리한다.
daily_completion_stats(날짜당 1행)에서 읽는다 →읽는 양이 행 수가 아니라 날짜 수에 비례.
UNION ALL네이티브 쿼리 한 문장으로 병합해 DB 왕복 1번.결과
누적 미션 수행 기록 1,000만 건 기준 1년 통계 조회:
/raw)조회 비용이 누적량과 무관하게 조회 기간(날짜 수)에만 비례하는 구조 확보.
구현 메모
completed_date컬럼 +(user_id, mission_id, completed_date)유니크 제약을 원본에 추가 —날짜 범위 집계용 단일 인덱스 겸, 같은 날 중복 완료를 DB 제약으로 막는 백스톱.
단, 배치 자체가 멱등이라 락은 중복 '확률'을 줄이는 층이고 정확성은 멱등성이 보장(이중 구조).
/raw)를 함께 두어 before/after를 같은 포맷으로 측정 가능.scripts/perf-test/daily-stats/비고
feature/PICK-247브랜치에 뒤섞여 쌓여 있던 작업 중 통계 부분만 분리한 것.completed_date백필 필요:UPDATE ... SET completed_date = DATE(created_at)후 유니크 인덱스 생성(기존 중복 정리 선행).