Re: index of only not null, use function index?

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Jeremy Finzel <finzelj(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Ariel <aspostgresql(at)dsgml(dot)com>, postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: index of only not null, use function index?
Date: 2017-06-08 19:57:38
Message-ID: CAHyXU0yzuNo-=XCL3U8xGC+Z=wxozB1GnaC3LjS-LJLmu28PvQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Thu, Jun 8, 2017 at 11:05 AM, Jeremy Finzel <finzelj(at)gmail(dot)com> wrote:
> On Thu, Jun 8, 2017 at 9:58 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Merlin Moncure <mmoncure(at)gmail(dot)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

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Frits Jalvingh 2017-06-09 13:04:29 Improving PostgreSQL insert performance
Previous Message Jeremy Finzel 2017-06-08 16:05:40 Re: index of only not null, use function index?