[1] Materialized Path 무한 댓글 조회 #1
Merged
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/2/3형태의 경로 문자열lft,rgt값으로 범위 표현ORDER BY depth, position❌ 채택하지 않은 이유 요약
✅ 최종 채택: Sort Key 기반 Materialized Path (단일 테이블)
동작 방식
1. sort_key = DFS 순서를 표현하는 문자열
/0001,/0002식으로 자식 순서를 이어붙임예시:
→
ORDER BY sort_key만으로 DFS 순서 정렬 및 페이지네이션 가능2. position + depth 조합으로 관리 단순화
부모.sort_key + '/' + position예시:
00010001/00010001/0001/00010001/0001/00023. DFS 순서 페이지네이션
첫 페이지
GET /api/comments?postId=1&size=10다음 페이지
GET /api/comments?postId=1&lastKey=0001/0003&size=10쿼리:
→ 단일 인덱스 정렬로 정렬·페이징 모두 해결
sort_key 설계 이슈 및 해결
자리수 오버플로우
String.format("%08d", id)사용 시 9자리 이상 깨짐→
"%016d"(16자리 패딩) 으로 해결깊은 depth로 인한 문자열 폭발
depth 10,000 × 16자리 = 160,000자→ VARCHAR 한계 초과→ depth 최대 50 제한 + position 4자리 고정
최종 길이:
4 * 50 + 49 = 249자→ 충분히 안전하고, 인덱스 사용 가능
최종 데이터 구조 예시
00010001/00010001/0001/00010001/0001/00020001/0001/0001/0001🧾 요약 정리
ORDER BY sort_key)결론
ORDER BY sort_key로 해결