Django is creating duplicate queries for prefetch_related

230 views
Skip to first unread message

Dheerendra Rathor

unread,
Feb 12, 2016, 10:18:22 PM2/12/16
to Django users
Hello,

Recently I was monitoring db queries created by Django and a particular block was behaving unusual.
Here is the code block:

        from django.db import connection, reset_queries
        reset_queries()
        group = get_object_or_404(
            Group.objects.select_related('course').prefetch_related(
                Prefetch(
                    'concepts',
                    queryset=Concept.objects.filter(is_published=True).prefetch_related(
                        'videos', 'pages',
                    ),
                ),
            ).only('course', 'playlist'),
            pk=pk,
        )

        s4 = connection.queries
        k4 = len(s4)
        reset_queries()



Now, the interesting part is that prefetch queries are made twice for videos and pages. Hereis the query dump https://paste.kde.org/psrp4orn6

It is making same queries twice with exactly same parameters. 

Can someone please explain it for me? In my view, it should not happen. I'm missing something here? 

Tim Graham

unread,
Feb 13, 2016, 9:49:44 PM2/13/16
to Django users
Maybe https://code.djangoproject.com/ticket/25546 fixes it? This will be in Django 1.10.

Dheerendra Rathor

unread,
Feb 13, 2016, 10:19:33 PM2/13/16
to Django users
Thanks Tim, from the ticket and Patch it looks like this was my issue. 

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a8a7e2f6-999f-4eaa-a045-bcf187c11b94%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages