From 84691d3a00b01f94752e8a8420f9396d826910ba Mon Sep 17 00:00:00 2001 From: Jimmy Ihalainen Date: Tue, 7 Nov 2023 16:51:02 +0200 Subject: [PATCH] Optimize inefficient database query in the deviation list view The course module name is no longer queried separately for each deviation. This reduces the number of performed queries significantly. Additionally, some large unnecessary attributes are no longer loaded. Fixes #1302 --- deviations/viewbase.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/deviations/viewbase.py b/deviations/viewbase.py index 5e9e4b711..6f4f2a47d 100644 --- a/deviations/viewbase.py +++ b/deviations/viewbase.py @@ -257,13 +257,18 @@ def get_deviation_groups( 'submitter', 'submitter__user', 'granter', 'granter__user', 'exercise', 'exercise__course_module', + 'exercise__course_module__course_instance', ) # parent is prefetched because there may be multiple ancestors, and # they are needed for building the deviation's URL. + .defer( + 'exercise__exercise_info', + 'exercise__description', + 'exercise__course_module__course_instance__description', + ) .prefetch_related('exercise__parent') .order_by('submitter', 'exercise__course_module') ) - deviation_groups = groupby( ordered_deviations, lambda obj: (obj.submitter, obj.exercise.course_module),