django - 如何消除循环中django查询的低效率?

标签 django performance django-queryset

如何使以下代码更高效(例如,如何用查询替换循环)?

def get_question(datetime_now, questions_queryset, user):
    best_schedule = None
    best_question = None
    # HOW TO ELIMINATE THE FOLLOWING LOOP AND REPLACE WITH A QUERY?
    for question in questions_queryset:
        try:
            schedule = (Schedule.objects
                        .filter(question=question, user=user)
                        .latest(field_name='datetime_added')
        except ObjectDoesNotExist:
            schedule = None
        if (schedule and (schedule.date_show_next >= datetime_now) and
                ((not best_schedule) or
                 (schedule.datetime_added >= best_schedule.datetime_added))):
            best_schedule = schedule
            best_question = question

    return best_question



模型.py

from django.contrib.auth.models import User
from django.db.models import DateTimeField, ForeignKey, Model, TextField

class Question(Model):
    question = TextField()

class Schedule(Model):
    datetime_added = DateTimeField(auto_now_add=True)
    datetime_show_next = DateTimeField(null=True, default=None)
    question = ForeignKey(Question)
    user = ForeignKey(User, null=True)

最佳答案

您可以使用 Subquery就像这个答案 https://stackoverflow.com/a/43883397/3627387或使用 Prefetch https://stackoverflow.com/a/31237026/3627387

这是使用 Prefetch 实现此目的的一种方法:

schedules_prefetch = Prefetch(
        'schedule_set',
        queryset=Schedule.objects.filter(user=user))
for question in questions_queryset.prefetch_related(schedules_prefetch):
    try:
        # using max here so it wouldn't do another DB hit
        schedule = max(question.schedule_set.all(),
                       key=lambda x: x.datetime_added)
    except ValueError:
        schedule = None

这是一个使用 Subquery 的示例(它可能实际上不起作用,但会给您一个大致的思路):

from django.db.models import OuterRef, Subquery
schedules = (Schedule.objects
             .filter(user=user, question=OuterRef('pk'))
             .order_by('datetime_added'))
questions_queryset = (questions_queryset
                    .annotate(latest_schedule=Subquery(schedules[:1])))
for question in questions_queryset:
    schedule = question.latest_schedule

关于django - 如何消除循环中django查询的低效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44897598/

相关文章:

python - 测试失败,返回 `django.db.utils.IntegrityError`

python - Numpy 按元素进行运算

java - Box2D静态体碰撞性能问题

django - if 和 if exists() 的区别?

python - 如何使用计算过滤查询集?

python - Heroku 将应用程序从 cedar-10 迁移到 cedar-14

c++ stringstream 太慢了,怎么提速?

python - Django 中对象集合的类查询集过滤

python - Django 打印我的查询集

django - 在 Heroku 中将 wwwhisper 用于 Django 应用程序