From a373f1d55c531cf795a16b2ebcc7eb02e5b826a2 Mon Sep 17 00:00:00 2001 From: Ole Laursen Date: Tue, 30 Jun 2020 12:46:36 +0000 Subject: [PATCH 1/2] Prefetch a couple of things in the agenda view to reduce the number of queries for IETF 106 from about 3800 to about 235. - Legacy-Id: 18103 --- ietf/meeting/helpers.py | 18 ++++++++++++++---- ietf/meeting/models.py | 12 ++++++------ .../meeting/edit_meeting_schedule_session.html | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/ietf/meeting/helpers.py b/ietf/meeting/helpers.py index e283f76db..c4f3e72c4 100644 --- a/ietf/meeting/helpers.py +++ b/ietf/meeting/helpers.py @@ -9,7 +9,7 @@ import re from tempfile import mkstemp from django.http import HttpRequest, Http404 -from django.db.models import Max, Q, Prefetch +from django.db.models import F, Max, Q, Prefetch from django.conf import settings from django.core.cache import cache from django.urls import reverse @@ -164,13 +164,17 @@ def get_schedule_by_name(meeting, owner, name): return meeting.schedule_set.filter(name = name).first() def preprocess_assignments_for_agenda(assignments_queryset, meeting, extra_prefetches=()): - assignments_queryset = assignments_queryset.select_related( - "timeslot", "timeslot__location", "timeslot__type", - ).prefetch_related( + assignments_queryset = assignments_queryset.prefetch_related( + 'timeslot', 'timeslot__type', 'timeslot__meeting', + 'timeslot__location', 'timeslot__location__floorplan', 'timeslot__location__urlresource_set', Prefetch( "session", queryset=add_event_info_to_session_qs(Session.objects.all().prefetch_related( 'group', 'group__charter', 'group__charter__group', + Prefetch('materials', + queryset=Document.objects.exclude(states__type=F("type"), states__slug='deleted').order_by('sessionpresentation__order').prefetch_related('states'), + to_attr='prefetched_active_materials' + ) )) ), *extra_prefetches @@ -212,6 +216,12 @@ def preprocess_assignments_for_agenda(assignments_queryset, meeting, extra_prefe if a.session and a.session.historic_group and a.session.historic_group.parent_id: a.session.historic_group.historic_parent = parent_replacements.get(a.session.historic_group.parent_id) + for d in a.session.prefetched_active_materials: + # make sure these are precomputed with the meeting instead + # of having to look it up + d.get_href(meeting=meeting) + d.get_versionless_href(meeting=meeting) + return assignments def read_session_file(type, num, doc): diff --git a/ietf/meeting/models.py b/ietf/meeting/models.py index 69c50f6a9..e4483e144 100644 --- a/ietf/meeting/models.py +++ b/ietf/meeting/models.py @@ -391,14 +391,14 @@ class Room(models.Model): return self.functional_name # audio stream support def audio_stream_url(self): - urlresource = self.urlresource_set.filter(name_id='audiostream').first() - return urlresource.url if urlresource else None + urlresources = [ur for ur in self.urlresource_set.all() if ur.name_id == 'audiostream'] + return urlresources[0].url if urlresources else None def video_stream_url(self): - urlresource = self.urlresource_set.filter(name_id__in=['meetecho', ]).first() - return urlresource.url if urlresource else None + urlresources = [ur for ur in self.urlresource_set.all() if ur.name_id in ['meetecho']] + return urlresources[0].url if urlresources else None def webex_url(self): - urlresource = self.urlresource_set.filter(name_id__in=['webex', ]).first() - return urlresource.url if urlresource else None + urlresources = [ur for ur in self.urlresource_set.all() if ur.name_id in ['webex']] + return urlresources[0].url if urlresources else None # class Meta: ordering = ["-id"] diff --git a/ietf/templates/meeting/edit_meeting_schedule_session.html b/ietf/templates/meeting/edit_meeting_schedule_session.html index 760cb2ea9..01f6cce1e 100644 --- a/ietf/templates/meeting/edit_meeting_schedule_session.html +++ b/ietf/templates/meeting/edit_meeting_schedule_session.html @@ -1,4 +1,4 @@ -
+
{{ session.scheduling_label }}
From 4678f0b799f5f1cd7d4a3723e7f55dbc0ab01fe8 Mon Sep 17 00:00:00 2001 From: Ole Laursen Date: Tue, 30 Jun 2020 12:58:40 +0000 Subject: [PATCH 2/2] Commit file missing from previous commit (fix a double prefetch). - Legacy-Id: 18104 --- ietf/meeting/views.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ietf/meeting/views.py b/ietf/meeting/views.py index eb72e0326..d927afcb1 100644 --- a/ietf/meeting/views.py +++ b/ietf/meeting/views.py @@ -1287,11 +1287,6 @@ def json_agenda(request, num=None ): # Update the assignments with historic information, i.e., valid at the # time of the meeting assignments = preprocess_assignments_for_agenda(assignments, meeting, extra_prefetches=[ - # sadly, these prefetches aren't enough to get rid of all implicit queries below - Prefetch("session__materials", - queryset=Document.objects.exclude(states__type=F("type"),states__slug='deleted').select_related("group").order_by("sessionpresentation__order"), - to_attr="prefetched_active_materials", - ), "session__materials__docevent_set", "session__sessionpresentation_set", "timeslot__meeting"