Re: will the planner ever use an index when the condition is <> ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Roxanne Reid-Bennett <rox(at)tara-lu(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: will the planner ever use an index when the condition is <> ?
Date: 2011-12-18 18:31:26
Message-ID: 3088.1324233086@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Roxanne Reid-Bennett <rox(at)tara-lu(dot)com> writes:
> On 12/17/2011 11:24 AM, Filip Rembiakowski wrote:
>> Normally there is no chance it could work,
>> because (a) the planner does not know all possible values of a column,
>> and (b) btree indexes cannot search on "not equal" operator.

> Is there an index type that can check "not equal"?

There is not. It's not so much that it's logically impossible as that
it doesn't seem worth the trouble to implement and maintain, because
most of the time a query like "where x <> constant" is going to fetch
most of the table, and so it would be better done as a seqscan anyway.

If you have a specific case where that's not true, you might consider
a partial index (CREATE INDEX ... WHERE x <> constant). But the details
of that would depend a lot on the queries you're concerned about.

regards, tom lane

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Marti Raudsepp 2011-12-18 20:11:28 Re: will the planner ever use an index when the condition is <> ?
Previous Message Roxanne Reid-Bennett 2011-12-18 14:52:14 Re: will the planner ever use an index when the condition is <> ?