Hi all, I'd like to fix an inefficiency in our ORM's negative lookups.
A long, long time ago, we had an "ne" lookup for QuerySet.filter(), which would let us do "not equals" lookups, like this: MyModel.objects.filter(slug__ne='ignoreme') Unfortunately, we removed this lookup type in http://code.djangoproject.com/changeset/2422 -- four and a half years ago. (Wow, have we really been around that long!?) The excuse at the time was that we added QuerySet.exclude() and that made "ne" lookups redundant. Problem is, that's technically not true, because "ne" generated SQL that was more efficient than what exclude() currently generates. Specifically, the problem is that the exclude() SQL doesn't take advantage of indexes on mycolumn as efficiently as the old-style lookup. Here's the difference: -- Old-style ne lookup SELECT * FROM mytable WHERE mycolumn != 'foo'; -- exclude() SELECT * FROM mytable WHERE NOT (mycolumn = 'foo'); Cal Henderson talked about this in his DjangoCon presentation. See slide 30 here: http://www.slideshare.net/iamcal/why-i-hate-django-part-22-presentation It's also at 41:46 in the video: http://www.youtube.com/watch?v=i6Fr65PFqfk I'd like for Django to go back to the more efficient query here, instead of the lame "NOT" query. Two solutions come to mind: we can restore the "ne" lookup type, or we can change the exclude() implementation to generate "!=" in the underlying SQL if possible. The advantage of the former is that it's much simpler to implement, but the advantage of the latter is that any code currently using exclude() would get the benefit of faster queries. (Note that I imagine only people using exclude() with a single "exact" lookup parameter would get the benefit; we could get trickier beyond that, but...diminishing returns.) We could also do both. I'm inclined to say we do the former -- restore the "ne" lookup type -- because it's a quick fix, and ask somebody to write up a patch for the latter. Does anybody have strong opinions against this? If not, I can restore the "ne" lookup type. Adrian -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
