我对 django 比较陌生,我在 django 中使用select_related()和prefetch_related()来减少对数据库的访问。
这是我的views.py文件的一部分:
topic = Topic.objects.filter(id=topic_id).select_related('followed_by')
questions = Question.objects.filter(topics=topic).prefetch_related('answers','question_comments','answers__created_by__userprofile','answers__answer_comments')
没有 select_related 和 prefetch_related 视图在 23.36MS 中执行 42 个查询,并且添加这些查询后,它在 7.91MS 中减少到 10 个查询,查看这些数据,我认为这些功能真的很棒。
所以我的问题是:使用这两个功能有什么缺点吗?我不应该一口气加入这么多桌子吗?