#19578: lazy prefetch_related
----------------------------------------------+----------------------------
     Reporter:  g00fy                         |      Owner:  nobody
         Type:  New feature                   |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  master
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  prefetch_related
Easy pickings:  0                             |  Has patch:  0
                                              |      UI/UX:  0
----------------------------------------------+----------------------------
 I suggest, that prefetch_related should be lazy. What I mean is, it would
 only execute aditional query, when related model needs to be accessed.
 Example:


 {{{
 # this would do just one query - prefetch_related is lazy
 pizzas = Pizza.objects.all().prefetch_related('toppigns')
 for pizza in pizzas:
     print pizza.name
 }}}



 {{{
 # Proposal A

 # now we want to access the related objects,
 # 1 aditional query (intermediate m2m table) is executed
 # not 2 since, we only need the id of the related object (which is already
 in the m2m table)
 # the 2nd query would result in a set of 'lazy' objects
 for pizza in pizzas:
     for topping in pizza.toppings.all():
         print topping.id

 # now 3rd query is made
 for pizza in pizzas:
     for topping in pizza.toppings.all():
         print topping.name
 }}}


 {{{

 # Proposal B


 # 2nd query is made (with the current implementation - m2m table join)
 for pizza in pizzas:
     for topping in pizza.toppings.all():
         print topping.id
 }}}





 Usecase:

 let's consider logic like this:


 {{{
 pizzas = Pizza.objects.all()

 print len(pizzas)
 if pizzas[0].name == 'pepperoni':
     for pizza in pizzas:
         print [topping.name for topping in pizza.toppings.all()]
 else:
     for pizza in pizzas:
         print pizza.name

 }}}



 in otherwords, this would be useful, if there is some complex logic that
 sometimes may require prefetch_related (and this requirement can be only
 checked after the query was made - so there is no way to do the checking &
 then doing prefetch_related).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/19578>
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 post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to