Re: [PERFORM] index of only not null, use function index? - Mailing list pgsql-performance

From Merlin Moncure
Subject Re: [PERFORM] index of only not null, use function index?
Date
Msg-id CAHyXU0yzuNo-=XCL3U8xGC+Z=wxozB1GnaC3LjS-LJLmu28PvQ@mail.gmail.com
Whole thread Raw
In response to Re: [PERFORM] index of only not null, use function index?  (Jeremy Finzel <finzelj@gmail.com>)
List pgsql-performance
On Thu, Jun 8, 2017 at 11:05 AM, Jeremy Finzel <finzelj@gmail.com> wrote:
> On Thu, Jun 8, 2017 at 9:58 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Merlin Moncure <mmoncure@gmail.com> writes:
>> > Having said that, what I typically do in such
>> > cases (this comes a lot in database driven work queues) something like
>> > this:
>> > CREATE INDEX ON table (OrderCol) WHERE col IS NOT NULL;
>>
>> Right, you can frequently get a lot of mileage out of indexing something
>> that's unrelated to the predicate condition, but is also needed by the
>> query you want to optimize.

> Normally, I find that in these situations, it makes sense to index the
> primary key of the table WHERE col is not null, because it will usually
> cover the largest number of cases, and is much better than a two-value
> boolean index, for example.

[meta note: please try to avoid top-posting]

Yeah, if you index the primary key and query it like this:

CREATE INDEX ON table (pkey) WHERE col IS NOT NULL;

SELECT pkey FROM table WHERE col IS NOT NULL
ORDER BY pkey LIMIT n;

This can give the best possible results since this can qualify for an
index only scan :-).

merlin


pgsql-performance by date:

Previous
From: Jeremy Finzel
Date:
Subject: Re: [PERFORM] index of only not null, use function index?
Next
From: Frits Jalvingh
Date:
Subject: [PERFORM] Improving PostgreSQL insert performance