Re: Postgres inconsistent use of Index vs. Seq Scan

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jim Carroll" <jim(at)carroll(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Postgres inconsistent use of Index vs. Seq Scan
Date: 2015-03-13 22:04:06
Message-ID: 25960.1426284246@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

"Jim Carroll" <jim(at)carroll(dot)com> writes:
> I'm having difficulty understanding what I perceive as an inconsistency in
> how the postgres parser chooses to use indices. We have a query based on NOT
> IN against an indexed column that the parser executes sequentially, but
> when we perform the same query as IN, it uses the index.

What you've got here is a query that asks for all rows with vid <> 1.
Not-equals is not an indexable operator according to Postgres, and there
would not be much point in making it one, since it generally implies
having to scan the majority of the table.

If, indeed, 99% of your table has vid = 1, then there would be some point
in trying to use an index to find the other 1%; but you'll have to
formulate the query differently (perhaps "vid > 1" would do?) or else
use a properly-designed partial index.

regards, tom lane

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Scott Marlowe 2015-03-13 22:25:50 Re: Performance issues
Previous Message Varadharajan Mukundan 2015-03-13 22:03:05 Re: Performance issues