-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
refactoringCode refactoringCode refactoring
Description
작업 설명
chat_app/models.py의 ChatRoom 모델에 정의된 last_message 프로퍼티는 호출될 때마다 데이터베이스 쿼리를 실행합니다.
만약 여러 채팅방 목록을 조회하면서 각 채팅방의 마지막 메시지를 표시해야 할 경우, 채팅방 개수(N)만큼 추가적인 쿼리가 발생하여(N+1 문제) 성능 저하를 유발합니다.
- 파일 위치:
chat_app/models.py - 코드:
@property def last_message(self): return self.message_set.order_by('-timestamp').first()
채팅방 목록을 조회하는 ViewSet 또는 서비스 로직에서 prefetch_related 또는 annotate를 사용하여 쿼리 시점에 마지막 메시지 정보를 함께 가져오도록 최적화합니다.
prefetch_related예시:from django.db.models import Prefetch # ... ChatRoom.objects.prefetch_related( Prefetch('message_set', queryset=Message.objects.order_by('-timestamp'), to_attr='latest_message') ).all()
수정 이유 (선택 사항)
No response
Metadata
Metadata
Assignees
Labels
refactoringCode refactoringCode refactoring