Suggestion for prefetch_related() documentation

40 views
Skip to first unread message

Laurent Lyaudet

unread,
Jun 23, 2022, 1:17:50 PMJun 23
to Django users
Hello,

I made a simple test to check the number of queries done :

# First part
order = Order.objects.get(id=22222)  # one query
items = list(order.items.all())  # one query
items = list(order.items.all())  # one query
items = list(order.items.all())  # one query

# Second part
order = Order.objects.prefetch_related("items").get(id=22222)  # two queries
items = list(order.items.all())  # no query
items = list(order.items.all())  # no query
items = list(order.items.all())  # no query

# Third part
order = Order.objects.get(id=22222)  # one query
prefetch_related_objects([order], "items")  # one query
items = list(order.items.all())  # no query
items = list(order.items.all())  # no query
items = list(order.items.all())  # no query
prefetch_related_objects([order], "items")  # no more query
prefetch_related_objects([order], "items")  # no more query

I was surprised that there was 4 queries for the First part of the test instead of 2 for the other parts, because I was expecting that .all() would also fill the cache and not only use it.
Maybe it is intended.
I think that this test or something else explaining this point could enhance the documentation on prefetch_related() and prefetch_related_objects().
I advised my colleagues to use prefetch_related_objects() when in doubt whether the objects given have been already extended with prefetching.

Best regards,
     Laurent Lyaudet




Jason

unread,
Jun 24, 2022, 2:04:51 AMJun 24
to Django users

Laurent Lyaudet

unread,
Jun 25, 2022, 6:31:37 AMJun 25
to django...@googlegroups.com
Hello,

Thanks for the link :)
I think the documentation would be better structured, then, if
https://docs.djangoproject.com/fr/4.0/ref/models/querysets/ would
reference your link.

I have not read all of Django documentation.
I do not know if it is expected that most developers would read everything.
Hence, I learn using Google top ranked pages inside Django
documentation when I search for a narrow topic on Django.
If I search
django prefetch
django prefetching
django prefetch_related
django prefetch_related_objets
I have https://docs.djangoproject.com/fr/4.0/ref/models/querysets/ as
first result but your link does not appear.
If I search
django cache
django caching
I get this
https://docs.djangoproject.com/fr/4.0/topics/cache/
but also your link as the third answer of Google.
Maybe, adding a link to /topics/db/optimization from
ref/models/querysets/ would help Google and people like me find this
information more easily :)

Best regards,
Laurent Lyaudet
> --
> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/CLnzKAcrE-A/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/01353b97-2b21-4d52-aab7-3046f93a5e1dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages