Commit ff0ba09a authored by fred's avatar fred
Browse files

performance: rewrite /listen to minimize queries

This cuts down the page from 521 queries to 3 (in my local dataset).
parent 55f8980d
......@@ -427,27 +427,27 @@ class Listen(TemplateView):
def get_context_data(self, **kwargs):
context = super(Listen, self).get_context_data(**kwargs)
context['sectionName'] = "Listen"
context['sounds'] = SoundFile.objects.all()[:15]
listenable = []
x = 0
for episode in Episode.objects.all().extra(select={
'first_diffusion': 'emissions_diffusion.datetime',
},
select_params=(False, True),
where=['''datetime = (SELECT MIN(datetime)
FROM emissions_diffusion
WHERE episode_id = emissions_episode.id)'''],
tables=['emissions_diffusion'],
).order_by('-first_diffusion'):
if x >= 60:
break
elif episode.main_sound is None:
continue
else:
x += 1
listenable.append(episode)
context['episodes'] = Episode.objects.filter(
soundfile__podcastable=True, soundfile__fragment=False) \
.select_related().extra(select={
'first_diffusion': 'emissions_diffusion.datetime', },
select_params=(False, True),
where=['''datetime = (SELECT MIN(datetime)
FROM emissions_diffusion
WHERE episode_id = emissions_episode.id)'''],
tables=['emissions_diffusion'],).order_by('-first_diffusion') [:60]
# get all related soundfiles in a single query
soundfiles = {}
for soundfile in SoundFile.objects.select_related().filter(podcastable=True,
fragment=False, episode__in=[x.id for x in context['episodes']]):
soundfiles[soundfile.episode_id] = soundfile
# replace dynamic property by a static attribute, to avoid database
# lookups
for episode in context['episodes']:
episode.main_sound = soundfiles.get(episode.id)
context['episodes'] = listenable
return context
listen = Listen.as_view()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment