Django查询集对象查询需要太多时间?

时间:2019-02-14 06:42:48

标签: django django-models

我正在使用一个工作模型来获取相关的工作数据,该数据也与其他模型建立了一对一或外键关系,我已经使用过select_related()并将内部模型传递给另一个模型,我使用prefetch_related()并在内部传递相对模型。我已经根据不同的条件在第2页上使用了此queryset两次,可以过滤一切正常的工作,但是1job queryset需要一些时间,而第二次工作很好,我无法理解我错过了什么以及如何解决它。如果有人了解,请提供帮助。

taking time

same query but doesn't taking time

模型查询集

jobs = Job.objects.filter(
        (Q(job_status__job_status_description='Booked') |
        Q(job_status__job_status_description='Allocated') |
        Q(job_status__job_is_done=True)) &
        Q(completed_date__gte=start.strftime("%Y-%m-%d"), completed_date__lte=week_end_date.strftime("%Y-%m-%d")) | 
        Q(allocated_date__gte=start.strftime("%Y-%m-%d"), allocated_date__lte=week_end_date.strftime("%Y-%m-%d")), 
        functools.reduce(operator.and_, jobs_q_condition)
    ).select_related(
        'customer',
        'job_status',
        'customer__book_with',
        'customer__frequency'
    ).prefetch_related(
        'customer__booking_road__area__franchise',
        'customer__booking_road__area'
    ).annotate(
        job_id=F('id'),
        job_window_cleaner=Concat(
            'window_cleaner__user__first_name',
            Value(' '),
            'window_cleaner__user__last_name'
        ),
        job_window_cleaner_booking_road=Concat(
            'customer__booking_road__area__default_cleaner__user__first_name',
            Value(' '),
            'customer__booking_road__area__default_cleaner__user__last_name'),                
        job_window_cleaner_id1=F('window_cleaner__user__id'),
        job_window_cleaner_id2=F('customer__booking_road__area__default_cleaner__user__id'),
        address=F('customer__address_line_1'),
        wc_wants=F('customer__booking_road__area__default_cleaner__requested_turnover'),
        action_on_check_in_str=F('action_on_check_in'),
        booking_info_str=F('customer__booking_info'),
        book_with_str=F('customer__book_with__book_with'),
        is_job_completed = F('job_status__job_is_done'),
        job_status_str = F('job_status__job_status_description'),
        area_str=F('customer__booking_road__area__area'),
        booking_road_str=F('customer__booking_road__booking_road'),
        price_str=Coalesce('price_on_day', 'set_price'),
        frequency_text_str=F('customer__frequency__frequency_text'),
        due_date_str=F('due_date'),
        allocated_date_str=F('allocated_date'),
        completed_date_str=F('completed_date')
    ).order_by(
        'job_window_cleaner','area_str','booking_road_str','due_date'
    )[:20]

due_jobs = Job.objects.filter(  
        functools.reduce(operator.and_, due_jobs_q_condition)
    ).select_related(
        'customer',
        'job_status',
        'customer__book_with',
        'customer__frequency'
    ).prefetch_related(
        'customer__booking_road__area__franchise',
        'customer__booking_road__area'
    ).annotate(
        job_id=F('id'),
        job_window_cleaner=Concat(
            'customer__booking_road__area__default_cleaner__user__first_name',
            Value(' '),
            'customer__booking_road__area__default_cleaner__user__last_name'),
        job_window_cleaner_id=F('customer__booking_road__area__default_cleaner__user__id'),
        address=F('customer__address_line_1'),
        wc_wants=F('customer__booking_road__area__default_cleaner__requested_turnover'),
        action_on_check_in_str=F('action_on_check_in'),
        booking_info_str=F('customer__booking_info'),
        book_with_str=F('customer__book_with__book_with'),
        is_job_completed = F('job_status__job_is_done'),
        job_status_str = F('job_status__job_status_description'),
        area_str=F('customer__booking_road__area__area'),
        booking_road_str=F('customer__booking_road__booking_road'),
        price_str=Coalesce('price_on_day', 'set_price'),
        frequency_text_str=F('customer__frequency__frequency_text'),
        due_date_str=F('due_date'),
    ).order_by(
        'job_window_cleaner','area_str','booking_road_str','due_date'
    )[:20]

0 个答案:

没有答案