public inbox for [email protected]  
help / color / mirror / Atom feed
From: Merlin Moncure <[email protected]>
To: Jeremy Finzel <[email protected]>
Cc: Tom Lane <[email protected]>
Cc: Ariel <[email protected]>
Cc: postgres performance list <[email protected]>
Subject: Re: index of only not null, use function index?
Date: Thu, 8 Jun 2017 14:57:38 -0500
Message-ID: <CAHyXU0yzuNo-=XCL3U8xGC+Z=wxozB1GnaC3LjS-LJLmu28PvQ@mail.gmail.com> (raw)
In-Reply-To: <CAMa1XUgfpqmoAqefr9dq0tFL8etDbUW61Vb4agdjy-BayDUg+w@mail.gmail.com>
References: <[email protected]>
	<CAHyXU0xm8TOJL=F-hTMZ_gMgEjRrtYCZR-t-fuNGDx+C51Gxvg@mail.gmail.com>
	<[email protected]>
	<CAMa1XUgfpqmoAqefr9dq0tFL8etDbUW61Vb4agdjy-BayDUg+w@mail.gmail.com>
List-Unsubscribe:  <mailto:[email protected]?body=unsub%20pgsql-performance>

On Thu, Jun 8, 2017 at 11:05 AM, Jeremy Finzel <[email protected]> wrote:
> On Thu, Jun 8, 2017 at 9:58 AM, Tom Lane <[email protected]> wrote:
>> Merlin Moncure <[email protected]> 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


-- 
Sent via pgsql-performance mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance




reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: index of only not null, use function index?
  In-Reply-To: <CAHyXU0yzuNo-=XCL3U8xGC+Z=wxozB1GnaC3LjS-LJLmu28PvQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox