#35309: Elide ordering of prefetch querysets for single valued relationships
-------------------------------------+-------------------------------------
     Reporter:  Laurent Lyaudet      |                    Owner:  Laurent
         Type:                       |  Lyaudet
  Cleanup/optimization               |                   Status:  assigned
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  prefetch single-     |             Triage Stage:  Accepted
  valued order_by                    |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Laurent Lyaudet:

Old description:

> While the ordering of multi-valued relationships must be preserved when
> prefetching relationships is it unnecessary when using `prefetch_related`
> against single valued relationships.
>
> For example, given the following models
>
> {{{#!python
> class Author(models.Model):
>     name = models.CharField(max_length=200)
>
>     class Meta:
>         ordering = ["name"]
>

> class Book(models.Model):
>     title = models.CharField(max_length=200)
>     author = models.ForeignKey(Author, related_name="books",
> on_delete=models.CASCADE)
>
>     class Meta:
>         ordering = ["title"]
> }}}
>
> The ordering of an author's books in
> `Author.objects.prefetch_related("books")` has a significance as multiple
> books might be associated with each authors.
>
> It's not the case for a book's author in
> `Book.objects.prefetch_related("author")` through as the relationship can
> only contain a single author and there is a single way to order the
> members of a singleton.
>
> In other words `sorted([element], key=sort_func)` will result in
> `[element]` for any `sort_func`.
>
> This property holds true for all the single valued relationships that the
> ORM supports (backward and forward 1:1 and forward 1:M) which allows the
> prefetching to elide any predefined ordering safely to avoid an
> unnecessary and possibly expensive ordering defined for the related model
> queryset.

New description:

 While the ordering of multi-valued relationships must be preserved when
 prefetching relationships is it unnecessary when using `prefetch_related`
 against single valued relationships.

 For example, given the following models

 {{{#!python
 class Author(models.Model):
     name = models.CharField(max_length=200)

     class Meta:
         ordering = ["name"]


 class Book(models.Model):
     title = models.CharField(max_length=200)
     author = models.ForeignKey(Author, related_name="books",
 on_delete=models.CASCADE)

     class Meta:
         ordering = ["title"]
 }}}

 The ordering of an author's books in
 `Author.objects.prefetch_related("books")` has a significance as multiple
 books might be associated with each authors.

 It's not the case for a book's author in
 `Book.objects.prefetch_related("author")` through as the relationship can
 only contain a single author and there is a single way to order the
 members of a singleton.

 In other words `sorted([element], key=sort_func)` will result in
 `[element]` for any `sort_func`.

 This property holds true for all the single valued relationships that the
 ORM supports (backward and forward 1:1 and forward 1:M) which allows the
 prefetching to elide any predefined ordering safely to avoid an
 unnecessary and possibly expensive ordering defined for the related model
 queryset.
 Currently the prefetch of authors will use the order by and add useless
 charge on the DB server.
 It would be useful to remove this order by.
 #ClimateChangeBrake

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35309#comment:12>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018e48c95662-589e23b4-843a-40c6-936c-07501ca83631-000000%40eu-central-1.amazonses.com.

Reply via email to