#35317: Add the possibility to do prefetches for only a subset of instances
-------------------------------------+-------------------------------------
     Reporter:  Laurent Lyaudet      |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  5.0
  (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
-------------------------------------+-------------------------------------
Comment (by Laurent Lyaudet):

 I have been able to make a very fast proof of concept right now in Django
 shell.
 Here is the redacted transcript. I will prepare a PR :)

 {{{#!python
 import random

 from django.db import connection
 from django.db.models import query


 old_prefetch_one_level = query.prefetch_one_level
 def new_prefetch_one_level(instances, prefetcher, lookup, level):
     if hasattr(lookup, "filter_callback") and lookup.filter_callback is
 not None:
         instances = [instance for instance in instances if
 lookup.filter_callback(instance)]
     if len(instances) == 0:
         return [], ()
     return old_prefetch_one_level(instances, prefetcher, lookup, level)


 query.prefetch_one_level = new_prefetch_one_level


 from DeleevCore.orders.models import Order, OrderProduct

 from django.db.models import Prefetch


 some_random_prefetch = Prefetch("items",
 queryset=OrderProduct.objects.all())
 some_random_prefetch.filter_callback = lambda x: bool(random.randint(0,
 1))

 orders =
 list(Order.objects.all().prefetch_related(some_random_prefetch)[:10])

 print(connection.queries)
 [{'sql': "SELECT t.oid, typarray FROM pg_type t JOIN pg_namespace ns ON
 typnamespace = ns.oid WHERE typname = 'hstore'",
   'time': '0.001'},
  {'sql': "SELECT typarray FROM pg_type WHERE typname = 'citext'",
   'time': '0.000'},
  {'sql': 'SELECT @redacted@ FROM "orders_order" ORDER BY
 "orders_order"."shipping_time" ASC  LIMIT 10',
   'time': '0.019'},
  {'sql': 'SELECT @redacted@ FROM "orders_orderproduct" WHERE
 "orders_orderproduct"."order_id" IN (114407, 84181, 84183, 25369)',
   'time': '0.006'}]

 Look only 4 ids in last query instead of 10 :)


 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35317#comment:2>
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 on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018e585b5395-c70674fd-83c5-45f8-bead-dff08f43ca69-000000%40eu-central-1.amazonses.com.

Reply via email to