0

我有一个根据年级提供课程的教育网站。

  1. GradeLevel 表存储所有可能的 Grade Levels。然后我有 LessonCategories 和 LessonCurriculum 表。

  2. GradeLevel 表与课程表和类别表建立了反向关系。

  3. 我遍历 GradeLevel 表中的每个等级(8 个等级),并沿途获取相应的课程和类别。

  4. 完成后,我将所有收集到的课程和类别填充到一个列表中,并将其传递给我的模板。

现在,问题是 Django 至少对每个查询进行两次评估。一次用于初始请求,第二次用于我将其放入列表中。(我正在使用 itertools 来链接结果。Itertools 导致查询再次运行。)这会降低我的服务器速度到爬行的不利影响。

我的问题是,是否有人可以查看我的模型和查询,并就更好的查询方式提出建议,以避免和/或减轻这个主要的性能瓶颈。

年级模型:

class GradeLevel(models.Model):
    title = models.CharField('Grade',max_length=10, null=True, blank=True, db_index=True)
    fullname = models.CharField('Description',max_length=100, null=True, blank=True, db_index=True)

课程类别模型:

class LessonCategory(models.Model):
    title = models.CharField(max_length=255, null=True, blank=True, db_index=True)
    ...
    gradelevel = models.ManyToManyField(GradeLevel, related_name='grade_cats', null=True, blank=True)

课程课程:

class LessonCurriculum(models.Model):         
    title = models.CharField(max_length=255, null=True, blank=True, db_index=True)
    ...
    gradelevel = models.ManyToManyField(GradeLevel, related_name='grade_curriculum', null=True, blank=True)

我的观点:

from itertools import chain
from operator import attrgetter

def my_view(request):
    grade_pk =  GradeLevel.objects.prefetch_related().get(title='pre-k')
    grade_pk_categories = grade_pk.grade_cats.filter(active=True,featured=True)
    grade_pk_galleries = grade_pk.grade_curriculum.filter(active=True,featured=True)

    grade_k =  GradeLevel.objects.prefetch_related().get(title='k')
    grade_k_categories = grade_k.grade_cats.filter(active=True,featured=True)
    grade_k_galleries = grade_k.grade_curriculum.filter(active=True,featured=True)

    grade_1 =  GradeLevel.objects.prefetch_related().get(title='1')
    grade_1_categories = grade_1.grade_cats.filter(active=True,featured=True)
    grade_1_galleries = grade_1.grade_curriculum.filter(active=True,featured=True)

    grade_2 =  GradeLevel.objects.prefetch_related().get(title='2')
    grade_2_categories = grade_2.grade_cats.filter(active=True,featured=True)
    grade_2_galleries = grade_2.grade_curriculum.filter(active=True,featured=True)

    grade_3 =  GradeLevel.objects.prefetch_related().get(title='3')
    grade_3_categories = grade_3.grade_cats.filter(active=True,featured=True)
    grade_3_galleries = grade_3.grade_curriculum.filter(active=True,featured=True)

    grade_4 =  GradeLevel.objects.prefetch_related().get(title='4')
    grade_4_categories = grade_4.grade_cats.filter(active=True,featured=True)
    grade_4_galleries = grade_4.grade_curriculum.filter(active=True,featured=True)

    grade_5 =  GradeLevel.objects.prefetch_related().get(title='5')
    grade_5_categories = grade_5.grade_cats.filter(active=True,featured=True)
    grade_5_galleries = grade_5.grade_curriculum.filter(active=True,featured=True)

    grade_6 =  GradeLevel.objects.prefetch_related().get(title='6')
    grade_6_categories = grade_6.grade_cats.filter(active=True,featured=True)
    grade_6_galleries = grade_6.grade_curriculum.filter(active=True,featured=True)

    grade_7 =  GradeLevel.objects.prefetch_related().get(title='7')
    grade_7_categories = grade_7.grade_cats.filter(active=True,featured=True)
    grade_7_galleries = grade_7.grade_curriculum.filter(active=True,featured=True)

    grade_8 =  GradeLevel.objects.prefetch_related().get(title='8')
    grade_8_categories = grade_8.grade_curriculum.filter(active=True,featured=True)
    grade_8_galleries = grade_8.grade_curriculum.filter(active=True,featured=True)

    gallery_list = list(set(sorted(chain(grade_pk_categories,grade_pk_galleries,grade_k_categories,grade_k_galleries,grade_1_categories,grade_1_galleries,grade_2_categories,grade_2_galleries,grade_3_categories,grade_3_galleries,grade_4_categories,grade_4_galleries,grade_5_categories,grade_5_galleries,grade_6_categories,grade_6_galleries,grade_7_categories,grade_7_galleries,grade_8_categories,grade_8_galleries), key=attrgetter('display_order'))))
4

2 回答 2

4

我会进一步优化@NathanVillaescusa的答案

grade_titles = ['pre-k', 'k', '1', '2', '3', '4', '5', '6', '7', '8']

# Turn list into list of Q items
queries = [models.Q(title=grade_title) for grade_title in grade_titles]

# Take one item of the list
query = queries.pop()

# OR the Q objects with the ones remaining in the list
for item in queries:
    query |= item

grades = GradeLevel.objects.prefetch_related().filter(query)
# grades should have everything you need and operation is more CPU
# and less DB bound now
于 2012-10-29T19:07:21.333 回答
1

您要使用的是chain.from_iterable。这将防止查询多次运行。

gallery_list = tuple(chain.from_iterable(grade_pk_categories,grade_pk_galleries,grade_k_categories,grade_k_galleries,grade_1_categories,grade_1_galleries,grade_2_categories,grade_2_galleries,grade_3_categories,grade_3_galleries,grade_4_categories,grade_4_galleries,grade_5_categories,grade_5_galleries,grade_6_categories,grade_6_galleries,grade_7_categories,grade_7_galleries,grade_8_categories,grade_8_galleries))

您还希望在通过 a 运行项目之后进行排序set,而不是之前。

gallery_list = list(sorted(frozenset(gallery_list), key=attrgetter('display_order')))

我会重新考虑您将如何获取所有这些数据。当您可能用一个或两个查询替换所有数据时,您正在为每个年级进行两次查询。

至少您可以使用 afor loop来减少您拥有的代码量:

grade_titles = ['pre-k', 'k', '1', '2', '3', '4', '5', '6', '7', '8']
gallery_list = []
for grade_title in grade_titles:
    grade = GradeLevel.objects.prefetch_related().get(title=grade_title)
    grade_categories = grade_pk.grade_cats.filter(active=True,featured=True)
    grade_galleries = grade_pk.grade_curriculum.filter(active=True,featured=True)
    gallery_list.extend(grade_categories)
    gallery_list.extend(grade_galleries)
gallery_list = list(sorted(frozenset(gallery_list), key=attrgetter('display_order')))
于 2012-10-29T18:38:16.883 回答