Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
More efficient negative lookups
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Adrian Holovaty  
View profile  
 More options Oct 27 2010, 5:32 pm
From: Adrian Holovaty <adr...@holovaty.com>
Date: Wed, 27 Oct 2010 16:32:08 -0500
Local: Wed, Oct 27 2010 5:32 pm
Subject: More efficient negative lookups
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Kaplan-Moss  
View profile  
 More options Oct 27 2010, 5:55 pm
From: Jacob Kaplan-Moss <ja...@jacobian.org>
Date: Wed, 27 Oct 2010 16:55:21 -0500
Local: Wed, Oct 27 2010 5:55 pm
Subject: Re: More efficient negative lookups

On Wed, Oct 27, 2010 at 4:32 PM, Adrian Holovaty <adr...@holovaty.com> wrote:
> 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.

Sounds like a good plan to me (especially making simple excludes faster).

However, just for the record I think the reason we decided to remove
__ne is the first place was that its existence introduces a weird
inconsistency with regard to other lookup types. That is, if there's a
"ne" why isn't there a "nstartswith" or "nrange" or ... ? I think down
that path lies madness so I'm +0 on bringing back "ne" with the
proviso that we agree it's not the first step down a slippery slope
towards "nistartswith" and friends.

Jacob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Gaynor  
View profile  
 More options Oct 27 2010, 6:18 pm
From: Alex Gaynor <alex.gay...@gmail.com>
Date: Wed, 27 Oct 2010 18:18:43 -0400
Local: Wed, Oct 27 2010 6:18 pm
Subject: Re: More efficient negative lookups

I'd be -0.5 (let's see how pusillanimous I can be!) on introducing
__ne, but +1 on fixing the internals to generate proper not equal
comparisons.

Alex

--
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adrian Holovaty  
View profile  
 More options Oct 27 2010, 6:42 pm
From: Adrian Holovaty <adr...@holovaty.com>
Date: Wed, 27 Oct 2010 17:42:18 -0500
Local: Wed, Oct 27 2010 6:42 pm
Subject: Re: More efficient negative lookups

On Wed, Oct 27, 2010 at 4:55 PM, Jacob Kaplan-Moss <ja...@jacobian.org> wrote:
> However, just for the record I think the reason we decided to remove
> __ne is the first place was that its existence introduces a weird
> inconsistency with regard to other lookup types. That is, if there's a
> "ne" why isn't there a "nstartswith" or "nrange" or ... ? I think down
> that path lies madness so I'm +0 on bringing back "ne" with the
> proviso that we agree it's not the first step down a slippery slope
> towards "nistartswith" and friends.

Totally agree we should set clear expectations that "ne" does not mean
we'll be getting nistartswith or any other negative lookup -- good
call in bringing that up. This should be treated as a special case for
a common case.

Adrian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
George Vilches  
View profile  
 More options Oct 27 2010, 6:59 pm
From: George Vilches <g...@thataddress.com>
Date: Wed, 27 Oct 2010 15:59:52 -0700 (PDT)
Local: Wed, Oct 27 2010 6:59 pm
Subject: Re: More efficient negative lookups

On Oct 27, 5:55 pm, Jacob Kaplan-Moss <ja...@jacobian.org> wrote:

> On Wed, Oct 27, 2010 at 4:32 PM, Adrian Holovaty <adr...@holovaty.com> wrote:
> > 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.

> Sounds like a good plan to me (especially making simple excludes faster).

> However, just for the record I think the reason we decided to remove
> __ne is the first place was that its existence introduces a weird
> inconsistency with regard to other lookup types. That is, if there's a
> "ne" why isn't there a "nstartswith" or "nrange" or ... ? I think down
> that path lies madness so I'm +0 on bringing back "ne" with the
> proviso that we agree it's not the first step down a slippery slope
> towards "nistartswith" and friends.

I know it's been a little while since I've made any major ORM
contributions, but I'd say -0 on __ne, and +1 on making exclude
generate better code.  Django's worked far too hard on making things
consistent as possible to let like this slip by just because we don't
want to muddy our hands with a little harder work in the exclude()
code.  So many other tickets have been stuck in DDN/Accepted forever
because the area of code is harder to review, it's not like it's an
unknown state in the project. :)

I'd even be willing to throw my hat in the ring to contribute towards
an .exclude()-based solution if someone else doesn't step forward, but
I know I won't be touching it until a few days pass.

George


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
George Vilches  
View profile  
 More options Oct 27 2010, 6:11 pm
From: George Vilches <g...@thataddress.com>
Date: Wed, 27 Oct 2010 18:11:50 -0400
Local: Wed, Oct 27 2010 6:11 pm
Subject: Re: More efficient negative lookups
On Oct 27, 2010, at 5:55 PM, Jacob Kaplan-Moss wrote:

> On Wed, Oct 27, 2010 at 4:32 PM, Adrian Holovaty <adr...@holovaty.com> wrote:
>> 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.

> Sounds like a good plan to me (especially making simple excludes faster).

> However, just for the record I think the reason we decided to remove
> __ne is the first place was that its existence introduces a weird
> inconsistency with regard to other lookup types. That is, if there's a
> "ne" why isn't there a "nstartswith" or "nrange" or ... ? I think down
> that path lies madness so I'm +0 on bringing back "ne" with the
> proviso that we agree it's not the first step down a slippery slope
> towards "nistartswith" and friends.

I know it's been a little while since I've made any major ORM contributions, but I'd say -0 on __ne, and +1 on making exclude generate better code.  Django's worked far too hard on making things consistent as possible to let like this slip by just because we don't want to muddy our hands with a little harder work in the exclude() code.  So many other tickets have been stuck in DDN/Accepted forever because the area of code is harder to review, it's not like it's an unknown state in the project. :)

I'd even be willing to throw my hat in the ring to contribute towards an .exclude()-based solution if someone else doesn't step forward, but I know I won't be touching it until a few days pass.

George


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Russell Keith-Magee  
View profile  
 More options Oct 28 2010, 10:47 pm
From: Russell Keith-Magee <russ...@keith-magee.com>
Date: Fri, 29 Oct 2010 10:47:38 +0800
Local: Thurs, Oct 28 2010 10:47 pm
Subject: Re: More efficient negative lookups

I agree. We should certainly fix the behavior of exclude(), but
reintroducing __ne feels like the start of a slippery slope to me.
Conceptually, I'm -1 on reintroducing __ne.

In the interests of practicality, I'd be willing to reconsider if it
turns out that fixing exclude() is prohibitively complex. However, it
seems premature to reintroduce __ne as a stop-gap measure before
anyone has taken a serious look at what is involved with fixing
exclude(). We still have almost 3 months until the freeze for 1.3;
that should be plenty of time to investigate exactly how much work
would be involved in fixing this bug properly.

Yours,
Russ Magee %-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
andybak  
View profile  
 More options Oct 29 2010, 6:17 am
From: andybak <andy...@gmail.com>
Date: Fri, 29 Oct 2010 03:17:54 -0700 (PDT)
Local: Fri, Oct 29 2010 6:17 am
Subject: Re: More efficient negative lookups
One small related point.

The admin could benefit from a way of doing negative lookups within
the lookup syntax itself.

Currently there is no way to construct an exclude filter change list
views in the URL.

i.e. If I am writing a custom filterspec or anything else that results
in a URL for a changelist page then I can do:

/admin/app/model/?model__id__exact=4

but I can't reverse the logic.

There might be better ways to allow this than just adding a negative
version of every lookup but I thought I'd throw another use case into
the ring.

Andy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »