Hi folks,
I'm working on a school timetable app. I want to fetch hundreds of thousands of
Lesson instances with prefetched m2m relations (e.g. subjects). My m2m
relations use through models (I'm not sure this actually makes a difference
here), and I'm running into performance issues because the prefetch query does
something along the lines of "SELECT ... FROM lesson_subjects WHERE lesson_id
IN [insane_list_of_lesson_ids]".
The initial query on Lesson uses a properly indexed filter on e.g. dates, so I
thought I'd try to use the same filter to get the related Subjects via the
relation to Lessons:
qs = Subject.objects.filter(lessons__school_id=8,
lessons__start__lt=datetime(2015, 8, 1))
Lesson.objects.filter(school_id=8, start__lt=datetime(2015, 8, 1))\
.prefetch_related(Prefetch('subjects', queryset=qs))
I can see that the extra filters are added to the prefetch query, but the huge
IN clause is still there.
Am I using Prefetch() wrong? Are there any other techniques to avoid the huge
IN clause in prefetch queries?
Thanks,
Erik
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/3A748402-9FAC-4670-BDD0-140CD4F080A4%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.