#36606: Optimize QuerySet.values_list(flat=True) with no fields
-------------------------------------+-------------------------------------
     Reporter:  Adam Johnson         |                     Type:
                                     |  Cleanup/optimization
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  dev                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 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.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36606>
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/0107019938785a34-4b10f867-46f2-42fa-9811-e2697d3bd5f6-000000%40eu-central-1.amazonses.com.

Reply via email to