(05.31) 수정됨.
게시글에 달린 댓글 수를 annotate하려는데, 자꾸 이상한 값이 나왔다.
내가 원하는 결과는 다음과 같이 post_id가 중복되는 수를 세는 것이었는데,
<QuerySet [{'post_id': 1, 'count': 3}, {'post_id': 2, 'count': 3}, {'post_id': 3, 'count': 2},
{'post_id': 4, 'count': 3}, {'post_id': 5, 'count': 1}, {'post_id': 6, 'count': 4},
{'post_id': 7, 'count': 1}, {'post_id': 8, 'count': 3}]>
자꾸만 다음과 같은 결과만 나오는 것이었다.
<QuerySet [{'post_id': 1, 'count': 1}, {'post_id': 1, 'count': 1}, {'post_id': 1, 'count': 1},
{'post_id': 2, 'count': 1}, {'post_id': 2, 'count': 1}, {'post_id': 2, 'count': 1},
{'post_id': 3, 'count': 1}, {'post_id': 4, 'count': 1}, {'post_id': 4, 'count': 1},
{'post_id': 4, 'count': 1}, {'post_id': 5, 'count': 1}, {'post_id': 6, 'count': 1}
{'post_id': 6, 'count': 1}, {'post_id': 6, 'count': 1}, {'post_id': 7, 'count': 1},
{'post_id': 3, 'count': 1}, {'post_id': 8, 'count': 1}, {'post_id': 8, 'count': 1},
{'post_id': 8, 'count': 1}, {'post_id': 6, 'count': 1}]>
코드
코드는 다음과 같이 작성하였고, 이미 다른 곳에서도 사용하는 코드이기에 코드를 잘못 짠 것은 아니었다.
from django.db.models import Count
values = Comment.objects.values('post_id').annotate(count=Count('post_id'))
print(values)
쿼리 확인
어차피 사용하는 값은 post_id 필드 1개이기때문에, post_id 필드값만 가져오게 한 다음, db에 요청한 쿼리값을 확인해보았다.
from django.db.models import Count
queryset = Comment.objects.get_queryset()
values = queryset.only('post_id').values('post_id').annotate(count=Count('post_id'))
print(values.query)
그 결과는 다소 충격적이었는데, post_id만 가져오라고 명령했음에도 불구하고, post_id 외 다른 필드의 데이터도 가져오고 있었다.
그 이유는 내가 get_queryset을 override하며 추가한 prefetch들과 annotate, 그리고 Meta class에서 선언한 ordering을 적용하기 위해 필요한 필드 데이터를 함께 가져오고 있었기 때문이었다.
prefetch, annotate, ordering 제거하기
- prefetch는 preftech_related(None)을 선언하면 모든 prefetch가 제거된다.
(장고 공식 문서의 ".prefetch_related(None)" 설명 참고) - annotate는 prefetch처럼 일괄 제거가 불가능하다. 따라서 자신이 annotate로 부여한 필드를 새로운 값으로 덮어씌워줘야만 한다.
하나하나 제거하는게 번거롭기 때문에, get_queryset() mehtod에서 annotate() 작업을 제거하고, 필요한 경우 annotate() 작업을 수행하는 것으로 변경했다. - ordering의 경우, 현재 쿼리에서 사용하는 필드를 기준으로 정렬하도록 하면 기존 ordering을 무시하게 된다.
또는 order_by()를 수행하면 정렬을 하지 않는다.
from django.db.models import Value
queryset = Comment.objects.get_queryset()
# prefetch 제거
queryset = queryset.prefetch_related(None)
# annotate 제거
queryset = queryset.annoate(
a=Vlaue(0),
b=Vlaue(0),
c=Vlaue(0),
...
)
# ordering 변경
queryset = queryset.only('post_id').order_by('post_id')
# ordering 제거
queryset = queryset.only('post_id').order_by()
이를 적용하면 db에 요청하는 쿼리 속도에도 영향을 준다.
가져오는 데이터의 양이 감소하기 때문이다.
이런 내용을 방문 통계에도 적용한다면 방문 통계의 로딩 속도를 조금 더 빠르게 할 수 있을 것 같다.