#36606: Optimize QuerySet.values_list(flat=True) with no fields
-------------------------------------+-------------------------------------
     Reporter:  Adam Johnson         |                    Owner:  (none)
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Adam Johnson:

Old description:

> Currently, `QuerySet.values_list()` ensures that no more than 1 field is
> set
> ([https://github.com/django/django/blob/41bc48ac1ed1d515977ebe965993b1ef83eafd02/django/db/models/query.py#L1417-L1421
> source]):
>
> {{{#!python
>         if flat and len(fields) > 1:
>             raise TypeError(
>                 "'flat' is not valid when values_list is called with more
> than one "
>                 "field."
>             )
> }}}
>
> However, it also allows the case where *no* fields are declared, for
> which all fields are fetched, only to throw away all but the first one
> ([https://github.com/django/django/blob/41bc48ac1ed1d515977ebe965993b1ef83eafd02/django/db/models/query.py#L266-L278
> source]):
>
> {{{#!python
> class FlatValuesListIterable(BaseIterable):
>     """
>     Iterable returned by QuerySet.values_list(flat=True) that yields
> single
>     values.
>     """
>
>     def __iter__(self):
>         queryset = self.queryset
>         compiler = queryset.query.get_compiler(queryset.db)
>         for row in compiler.results_iter(
>             chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size
>         ):
>             yield row[0]
> }}}
>
> I think we can optimize this case to select only the first field in the
> model instead, maintaining semantics while avoiding overfetching.

New description:

 Currently, `QuerySet.values_list()` ensures that no more than 1 field is
 set
 
([https://github.com/django/django/blob/41bc48ac1ed1d515977ebe965993b1ef83eafd02/django/db/models/query.py#L1417-L1421
 source]):

 {{{#!python
         if flat and len(fields) > 1:
             raise TypeError(
                 "'flat' is not valid when values_list is called with more
 than one "
                 "field."
             )
 }}}

 However, it also allows the case where *no* fields are declared, for which
 all fields are fetched, only to throw away all but the first one
 
([https://github.com/django/django/blob/41bc48ac1ed1d515977ebe965993b1ef83eafd02/django/db/models/query.py#L266-L278
 source]):

 {{{#!python
 class FlatValuesListIterable(BaseIterable):
     """
     Iterable returned by QuerySet.values_list(flat=True) that yields
 single
     values.
     """

     def __iter__(self):
         queryset = self.queryset
         compiler = queryset.query.get_compiler(queryset.db)
         for row in compiler.results_iter(
             chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size
         ):
             yield row[0]
 }}}

 I think we can optimize this case to select only the first field in the
 model instead, maintaining semantics while avoiding overfetching.

 This case also seems untested with the `values_list()` tests in
 `tests/lookup/tests.py`, so we'd want to add a test there.

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36606#comment:1>
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019938798d46-68ac0ef8-bbc7-4281-96e6-79ea4bf7beeb-000000%40eu-central-1.amazonses.com.

Reply via email to