pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: scott.marlowe <scott.marlowe@ihs.com>
To: Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>
Cc: PostgreSQL Administrators Mailing List <pgsql-admin@postgresql.org>
Subject: Re: Partial indexes (was: Re: Indexing a boolean)
Date: Fri, 22 Aug 2003 07:48:56 -0600 (MDT)
Message-ID: <Pine.LNX.4.33.0308220746520.16351-100000@css120.ihs.com> (raw)
In-Reply-To: <Pine.LNX.4.50.0308220018410.25250-100000@short.lancs.ac.uk>

On Fri, 22 Aug 2003, Sam Barnett-Cormack wrote:

> On Thu, 21 Aug 2003, scott.marlowe wrote:
> 
> > On Thu, 21 Aug 2003, Kris Kiger wrote:
> >
> > > I would appreciate it if I could get some thoughts on indexing a field
> > > with only two values?  For example, I have a table with a few million
> > > rows in it.  All items in this table are broken up into two categories
> > > using 'T' or 'F'.  It seems logical to me that an index on this field
> > > would create two logical 'buckets', so that one could say, "I want all
> > > 'T' values", or "I want all 'F' values" and merely have to look in the
> > > appropriate bucket, rather than have to perform a sequential scan
> > > through three million items every time a request is made based on 'T' or
> > > 'F'.  If I were to create an index on a char(1) field that contains only
> > > values of 'T' or 'F', would the query analyzer override the use of this
> > > index?  How does Postgres address this problem and what are all of your
> > > thoughts on this issue?  I appreciate the help!
> >
> > Often the best approach here is to make a partial index:
> >
> > create index table_dx on table (bool_field) where bool_field IS TRUE;
> >
> > This works well if you have a large portion of the boolean fields set to
> > FALSE, and want to find the few that are TRUE.  Reverse the TRUE and false
> > for other situations.
> 
> A more general question:
> 
> Can the planner tell which index to use if there is more than one that
> fits the bill? Like if there is a full index and one or more partial
> indexes on a field, can it determine which to use for a given query?

Basically, for partial / functional indexes, the create index where clause 
needs to exactly (or nearly so) match the select query's where clause:

create index table_dx on table (bool_field) where bool_field IS TRUE;
select * from table where bool_field = 't';  <- might not match (currently 
won't match)
select * from table where bool_field IS TRUE;  <- will match

And yes, the query planner will usually know to use the most selective 
index, i.e. the smaller, cheaper to use index.




view thread (4+ messages)  latest in thread

Message-ID: <Pine.LNX.4.33.0308220746520.16351-100000@css120.ihs.com>
Permalink:  ../Pine.LNX.4.33.0308220746520.16351-100000@css120.ihs.com/
Also on:    postgresql.org/message-id/Pine.LNX.4.33.0308220746520.16351-100000@css120.ihs.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: scott.marlowe@ihs.com, s.barnett-cormack@lancaster.ac.uk
  Subject: Re: Partial indexes (was: Re: Indexing a boolean)
  In-Reply-To: <Pine.LNX.4.33.0308220746520.16351-100000@css120.ihs.com>
  References: <Pine.LNX.4.50.0308220018410.25250-100000@short.lancs.ac.uk> <Pine.LNX.4.33.0308220746520.16351-100000@css120.ihs.com>

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

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