`select_related` relation control

34 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Roger Gammans

ungelesen,
03.12.2020, 05:24:4203.12.20
an django-d...@googlegroups.com
Hi

A recent question on one of the other django mailing lists reminded me of this: I have a very edge (and almost certainly off the end of 'supported') use case, where I would like to mark some relations on a model, as having select_related() disabled.

More specifically the requirement is that qs.select_related() (with no args) doesn't include the marked relations. It doesn't look like there is a way of doing this in the ORM as it stands - is this correct?

I could probably work up this feature as a field flag, but given that it's completely niche, I wondered what the chance of the core team accepting it was. (with unittests)?


-- 
Roger Gammans <rgam...@gammascience.co.uk>
Gamma Science Ltd. (GB Nr. 07356014 )

Adam Johnson

ungelesen,
03.12.2020, 06:58:3203.12.20
an django-d...@googlegroups.com
What prevents you from using select_related with an explicit list of relations that you *do* want? That's more maintainable as it prevents overfetching when you add new relations. If it were up to me I'd remove the "select_related()-selects-all-relations-behaviour" - it's too implicit and can lead to massive queries without much thought.

In many situations prefetch_related() outperforms select_related() as it doesn't pull back repeat copies of low-cardinality related objects. I covered this a bit in my N+1 blog post here: https://adamj.eu/tech/2020/09/01/django-and-the-n-plus-one-queries-problem/ .

I'd also point you to django-auto-prefetch which performs prefetches across a queryset's objects on first access, eliminating nearly 90%+ of the need to manually tune select or prefetch_related calls. That's also covered in the blog post.


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/0d9b15f264065bf090e7413e80f27d022d21a227.camel%40gammascience.co.uk.


--
Adam

Roger Gammans

ungelesen,
03.12.2020, 07:43:5303.12.20
an django-d...@googlegroups.com
On Thu, 2020-12-03 at 11:58 +0000, Adam Johnson wrote:
What prevents you from using select_related with an explicit list of relations that you *do* want?

well the fact the select_related() call is in third party code. In the most common case for us this is contrib.admin.

So we are forced to be a bit more explicit in the ModelAdmin Meta definition than we would normally be. It no
bad thing., but it occasionally catches team members out. It fails fast here, so it's not going to go unnoticed like
a performance problem can.

Thanks for the blog link and the reference to django-auto-prefetch, there is some good stuff in there to think about.
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten