if queryset.filter(pk=obj.pk).exists():
if obj in queryset:
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/88c83b8d-658c-47cc-9162-fcfebebe9c4a%40googlegroups.com.
If you already fetched the queryset, `if obj in queryset` will make a new database query
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/570372E7-2949-426C-BAAE-DBEF1B17FBC0%40polytechnique.org.
If you already fetched the queryset, `if obj in queryset` will make a new database queryI don't see why we couldn't implement __contains__ to do a walk through _result_cache if it has been fetched?
>>> sys.version
'3.7.2rc1 (default, Dec 12 2018, 06:25:49) \n[GCC 8.2.0]'
>>> django.__version__
'3.0.6'
>>> import myapp.models as m
>>> x = m.GL.objects.all()[324]
>>> x in m.GL.objects.all()
True
>>> import timeit
>>> timeit.timeit('m.GL.objects.filter(pk=x.pk)', setup='import myapp.models as m;x = m.GL.objects.all()[324]', number=100)
0.05818330496549606
>>> timeit.timeit('x in qs', setup='import myapp.models as m;qs = m.GL.objects.all(); x=qs[324]', number=100)
1.5688817161135375
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/55b4d7e0-ba30-48b2-8005-d27adacc7fd0%40googlegroups.com.
if obj in queryset:
>>> timeit.timeit('m.GL.objects.filter(pk=x.pk)', setup='import myapp.models as m;x = m.GL.objects.all()[324]', number=100)
0.05818330496549606
is not doing anything, add a `.exists()` or `len(..)` or something to evaluate the queryset. That number is WAY too low.
On 2 Jun 2020, at 19:42, Tim Graham <timog...@gmail.com> wrote:And here's some past discussion:https://code.djangoproject.com/ticket/24141 - contains() method for QuerySets (closed as needsinfo due to no mailing list discussion to find a consensus)https://github.com/django/django/pull/3906 - Efficient QuerySet.__contains__ (closed as wontfix due to the behavior change)
ps @roger
>>> timeit.timeit('m.GL.objects.filter(pk=x.pk)', setup='import myapp.models as m;x = m.GL.objects.all()[324]', number=100)
0.05818330496549606is not doing anything, add a `.exists()` or `len(..)` or something to evaluate the queryset. That number is WAY too low.
>>> # Searching for first in queryset
>>> n=10_000_000 ;timeit.timeit('x in qs', setup='import myapp.models as m;qs = m.GL.objects.all(); qs2=qs;i=len(qs2);x=qs[0]', number=n)/(n/1_000_000)
0.4136358265765011
>>> n = 100_000 ; timeit.timeit('m.GL.objects.filter(pk=x.pk)', setup='import myapp.models as m;x = list(m.GL.objects.all())[0]', number=n)/(n/1_000_000)
124.99445254914463
>>> # Searching for last in queryset.
>>> n=1000; timeit.timeit('x in qs', setup='import myapp.models as m;qs = m.GL.objects.all(); qs2=qs;i=len(qs2);x=qs[i-1]', number=n)/(n/1_000_000)
50741.098549216986
>>> n = 100_000 ; timeit.timeit('m.GL.objects.filter(pk=x.pk)', setup='import myapp.models as m;x = list(m.GL.objects.all())[-1]', number=n)/(n/1_000_000)
118.99649207945913
--Roger Gammans <rgam...@gammascience.co.uk>
import timeit
def time_fn(title, stmt, number=10000, prefetch=False):
setup = [
'from tickets.models import Ticket',
'qs=Ticket.objects.all()',
'obj_first = qs.first()',
'obj_mid = qs[50000]',
'obj_last = qs.last()',
]
if prefetch:
setup.append('list(qs)')
result_time = timeit.timeit(stmt, setup='\n'.join(setup), number=number)
print(f'{title}: {result_time}')
time_fn('Database fetch', 'list(qs)')
time_fn('Prefetched obj_first in qs', 'obj_first in qs', prefetch=True)
time_fn('Prefetched obj_mid in qs', 'obj_mid in qs', prefetch=True)
time_fn('Prefetched obj_last in qs', 'obj_last in qs', prefetch=True)
time_fn('Database obj_first exists()', 'qs.filter(pk=obj_first.pk).exists()')
time_fn('Database obj_mid exists()', 'qs.filter(pk=obj_mid.pk).exists()')
time_fn('Database obj_last exists()', 'qs.filter(pk=obj_last.pk).exists()')
Database fetch: 25.667968138001015Prefetched obj_first in qs: 0.027538340998944477Prefetched obj_mid in qs: 1051.1691511649988Prefetched obj_last in qs: 2369.5217889660016Database obj_first exists(): 6.442390248001175Database obj_mid exists(): 6.213641551999899Database obj_last exists(): 5.831252460000542
--
You received this message because you are subscribed to a topic in the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-developers/NZaMq9BALrs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/3928039038bac9b52279294f7efcac318dc80388.camel%40gammascience.co.uk.
# Old explicit fetch methodqueryset = Stuff.objects.all()if obj in list(queryset):pass# My suggestionqueryset = Stuff.objects.all().evaluate()if obj in queryset:pass
import timeit
def time_fn(title, stmt, number=10_000):
setup = [
'from movies.models import Movie',
'qs=Movie.objects.all()',
'obj_first = qs.first()',
'obj_mid = qs[300]',
'obj_last = qs.last()',
'list(qs)',
]
result_time = timeit.timeit(stmt, setup='\n'.join(setup), number=number)
print(f'{title}: {result_time}')
time_fn('Database fetch', 'qs._result_cache=None\nlist(qs)')
time_fn('Prefetched obj_first in qs', 'obj_first in qs')
time_fn('Prefetched obj_mid in qs', 'obj_mid in qs')
time_fn('Prefetched obj_last in qs', 'obj_last in qs')
time_fn('Database obj_first exists()', 'qs.filter(pk=obj_first.pk).exists()')
time_fn('Database obj_mid exists()', 'qs.filter(pk=obj_mid.pk).exists()')
time_fn('Database obj_last exists()', 'qs.filter(pk=obj_last.pk).exists()')
# Results
Database fetch: 616.097227364
Prefetched obj_first in qs: 0.030961711003328674
Prefetched obj_mid in qs: 6.6988333979970776
Prefetched obj_last in qs: 24.189914419999695
Database obj_first exists(): 6.468764332996216
Database obj_mid exists(): 6.167532913001196
Database obj_last exists(): 6.0190791100030765
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/20200605090349.559f3872.shai%40platonix.com.
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/88c83b8d-658c-47cc-9162-fcfebebe9c4a%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-developers/NZaMq9BALrs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CABD5YeE7_ORUfJcnt6f4aB4J0j-%3DyDK_BowEt_fefcaFMGdB1g%40mail.gmail.com.
If people agree QuerySet.contains() should be added, how do we move forward?
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAD69rUBKOse%2Be2RedPT%2B-KWkcdC%3DiaG0zyt1Hakefp6gQ7vdLA%40mail.gmail.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/88c83b8d-658c-47cc-9162-fcfebebe9c4a%40googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-developers/NZaMq9BALrs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-developers+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CABD5YeE7_ORUfJcnt6f4aB4J0j-%3DyDK_BowEt_fefcaFMGdB1g%40mail.gmail.com.
--Vänligen, Johan SchiffRadkompaniet AB072-229 61 19--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsub...@googlegroups.com.