From 0ac216b7da160d77658a85e6a351d7d7623605d4 Mon Sep 17 00:00:00 2001 From: Sampo Kulonen Date: Wed, 9 Feb 2022 10:46:41 +0200 Subject: [PATCH] Optimization: cached content prefetches Generation CachedContent has been optimized by adding prefetches to its queryset. --- exercise/cache/content.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/exercise/cache/content.py b/exercise/cache/content.py index ee4a3b080..78e3973c8 100644 --- a/exercise/cache/content.py +++ b/exercise/cache/content.py @@ -1,5 +1,6 @@ from typing import Any, Dict, List, Optional, Type, Union +from django.db.models import Prefetch from django.db.models.base import Model from django.db.models.signals import post_save, post_delete from django.utils import timezone @@ -98,7 +99,15 @@ def recursion( # Collect each module. i = 0 - for module in instance.course_modules.all(): + for module in instance.course_modules.prefetch_related( + 'requirements', + 'requirements__threshold__passed_modules', + 'requirements__threshold__passed_categories', + 'requirements__threshold__passed_exercises', + 'requirements__threshold__passed_exercises__parent', + 'requirements__threshold__points', + Prefetch('learning_objects', LearningObject.objects.all()), + ): entry = { 'type': 'module', 'id': module.id,