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
Le ven. 24 juin 2022 à 08:05, Jason a écrit :
>
>
> This is explicitly called out in the documentation at
> https://docs.djangoproject.com/en/4.0/topics/db/optimization/#understand-queryset-evaluation
> On Thursday, June 23, 2022 at 1:17:50 PM UTC-4 laurent...@gmail.com wrote:
>>
>> Hello,
>>
>> I made a simple test to check the number of queries done :
>>
>> # First part
>> order = Order.objects.get(id=2) # 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=2) # 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=2) # 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
>>
>>
>>
>>
> --
> 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+unsubscr...@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.
--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAB1LBmssEnOGAAiKJDdLbL226oguyryJTkMUsKNYLTg1%3D89Urg%40mail.gmail.com.