Tags
- delete
- Django
- Queue
- 트리
- SQL
- regexp
- migrations
- 통계학
- 스택
- DB
- distinct
- update
- 이진트리
- Tree
- 쟝고
- ORM
- N:1
- create
- count
- 백트래킹
- Article & User
- M:N
- 뷰
- 완전검색
- 큐
- Vue
- 그리디
- outer join
- drf
- stack
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Notice
Recent Posts
Link
데이터 분석 기술 블로그
DB에 대하여(25)_select_related & prefetch_related (feat. Django) 본문
문제 상황
prefetch_related 적용
문제 해결 1단계입니다.
게시글을 조회하면서 참조된 댓글까지 한 번에 조회합니다.
# views.py
def index_4(request):
# articles = Article.objects.order_by('-pk')
articles = Article.objects.prefetch_related('comment_set').order_by('-pk')
# articles = Article.objects.prefetch_related(
# Prefetch('comment_set', queryset=Comment.objects.select_related('user'))
# ).order_by('-pk')
"111 queries including 110 similar and 100 duplicated" → "102 queries including 100 similar and 100 duplicates"
select_related & prefetch_related 적용
문제 해결 2단계입니다.
게시글 + 각 게시글의 댓글 목록 + 댓글의 작성자를 한 번에 조회
# views.py
def index_4(request):
# articles = Article.objects.order_by('-pk')
# articles = Article.objects.prefetch_related('comment_set').order_by('-pk')
articles = Article.objects.prefetch_related(
Prefetch('comment_set', queryset=Comment.objects.select_related('user'))
).order_by('-pk')
"102 queries including 100 similar and 100 duplicats" → "2 queries"
'DB' 카테고리의 다른 글
DB에 대하여(24)_prefetch_related (feat. Django) (0) | 2024.05.11 |
---|---|
DB에 대하여(23)_select_related (feat. Django) (0) | 2024.05.10 |
DB에 대하여(22)_annotate (feat. Django) (0) | 2024.05.09 |
DB에 대하여(21)_Improve query (feat. Django) (0) | 2024.05.08 |
DB에 대하여(20)_Fixtures (feat. Django) (0) | 2024.05.07 |