【发布时间】:2016-09-05 16:19:42
【问题描述】:
我正在尝试在 Django 中优化类似提要的查询,在该查询中,我得到了我关注的用户评论的独特项目。
queryset_comments = Item.objects.distinct() \
.prefetch_related('comments', 'comments__user') \
.filter(comments__user__in=self.request.user.following.all()) \
.prefetch_related(
Prefetch('comments', queryset=Comment.objects.filter(
user__in=self.request.user.following.all()
).order_by('-created_on'), to_attr='activity')
) \
.all()
这给了我一个相关的activity 对象,其中包含我关注的用户创建的所有 cmets 的列表,最近的评论在前。现在我只需要第一个,所以我循环其他查询集并设置我需要的值:
for item in queryset_comments:
item.feed_user = item.activity[0].user
item.feed_date = item.activity[0].created_on
item.feed_activity = 'commented'
但是这个循环只需要大约 500 个项目就需要超过 2 秒...我尝试使用切片进行预取(通过将 [0] 或 .first() 添加到 Prefetch 查询集,但这两种方法都不支持by Django ORM。
有什么建议可以加快速度吗?
【问题讨论】:
-
您是否已经尝试过
earliest或latest? -
尝试在
for循环之后使用latest_activity = item.activity.first(),然后使用此latest_activity对象在item上设置其他值。 -
@2ps: 得到与我使用切片时相同的错误:
AttributeError: 'Item' object has no attribute '_add_hints' -
@Rahul:
item.activity是一个列表,我得到AttributeError: 'list' object has no attribute 'first'。我试过latest_activity = item.activity[0],它可以工作,但需要同样的时间。 -
最后,我将进行缓存/redis 非规范化以加快提要检索。