Wednesday, November 2, 2011

Re: QuerySet: "if obj in queryset" can be very slow

That's because your queryset is being evaluated when you compare, maybe if you explain your problem, we can give you a better solution than the use of `in`.

Take a look at the docs about when querysets are evaluated:


[]'s

On Wed, Nov 2, 2011 at 9:42 AM, Thomas Guettler <hv@tbz-pariv.de> wrote:
Hi,

I just discovered, that "if obj in queryset" can be very slow:

queryset=MyModel.objects.all()
obj=MyModel.objects.get(id=319526)

#if obj in queryset: # This is very slow, if queryset is big: All lines from queryset get created to Python objects
#    print 'in'

if queryset.filter(id=obj.id): # Fast: Check is done inside DB.
   print 'in'

What is the best way to do "if obj in queryset"?

 Thomas

--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Flávia Missi

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment