pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Partial indexes (was: Re: Indexing a boolean)
4+ messages / 4 participants
[nested] [flat]

* Partial indexes (was: Re: Indexing a boolean)
@ 2003-08-21 23:20  Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>
  0 siblings, 1 reply; 4+ messages in thread

From: Sam Barnett-Cormack @ 2003-08-21 23:20 UTC (permalink / raw)
  To: pgsql-admin

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?

-- 

Sam Barnett-Cormack
Software Developer                           |  Student of Physics & Maths
UK Mirror Service (http://www.mirror.ac.uk)  |  Lancaster University



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Partial indexes (was: Re: Indexing a boolean)
@ 2003-08-22 13:48  scott.marlowe <scott.marlowe@ihs.com>
  parent: Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>
  0 siblings, 1 reply; 4+ messages in thread

From: scott.marlowe @ 2003-08-22 13:48 UTC (permalink / raw)
  To: Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>; +Cc: pgsql-admin

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.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Partial indexes (was: Re: Indexing a boolean)
@ 2003-08-22 14:34  Tom Lane <tgl@sss.pgh.pa.us>
  parent: scott.marlowe <scott.marlowe@ihs.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2003-08-22 14:34 UTC (permalink / raw)
  To: scott.marlowe <scott.marlowe@ihs.com>; +Cc: Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>; pgsql-admin

"scott.marlowe" <scott.marlowe@ihs.com> writes:
> Basically, for partial / functional indexes, the create index where clause 
> needs to exactly (or nearly so) match the select query's where clause:

Another consideration is that an index declaration like this:

	create index fooi on foo (flag) where flag;

is really rather redundant, since the entries will only be made for rows
where flag is true, and so storage of the column value in the index is
useless.  You may be able to get more mileage out of the index by making
the index on another column that you often test in conjunction with the
flag.  For example:

regression=# create table foo (flag bool, ts timestamp);
CREATE TABLE
regression=# create index fooi on foo(ts) where flag;
CREATE INDEX
regression=# explain select * from foo where ts > '2003-10-11' and flag;
                                   QUERY PLAN
--------------------------------------------------------------------------------
 Seq Scan on foo  (cost=0.00..22.50 rows=167 width=9)
   Filter: ((ts > '2003-10-11 00:00:00'::timestamp without time zone) AND flag)
(2 rows)

regression=# set enable_seqscan TO 0;
SET
regression=# explain select * from foo where ts > '2003-10-11' and flag;
                               QUERY PLAN
-------------------------------------------------------------------------
 Index Scan using fooi on foo  (cost=0.00..43.25 rows=167 width=9)
   Index Cond: (ts > '2003-10-11 00:00:00'::timestamp without time zone)
   Filter: flag
(3 rows)

(In a more realistic situation, the planner would probably have chosen
the indexscan without any prompting.)

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Partial indexes (was: Re: Indexing a boolean)
@ 2003-08-22 22:48  Nikolaus Dilger <nikolaus@dilger.cc>
  0 siblings, 0 replies; 4+ messages in thread

From: Nikolaus Dilger @ 2003-08-22 22:48 UTC (permalink / raw)
  To: tgl@sss.pgh.pa.us; +Cc: scott.marlowe@ihs.com; s.barnett-cormack@lancaster.ac.uk; pgsql-admin

Tom's example shows that the query actually takes
longer using the index than without.  And that's not
surprising.  A btree index doesn't buy you much when
you have very low cardinality.  That's ideal for bitmap
indexes that PostgreSQL currently doesn't offer.

In the expample of the original question no index on
the boolean column is probably also better/faster. 
Just try it.

What may help is a multicolum index.
So lets say you have the columns first_name, last_name,
gender.  Instead of creating one index for last_name
and one for gender.  Just create a multicolumn index on
last_name and gender.  Of course that assumes that you
have both columns in your WHERE clause combined with an
AND.

CREATE INDEX name_gender ON person (last_name, gender);

SELECT *
FROM person
WHERE last_name='Smith'
AND gender='male';

Regards,
Nikolaus

On Fri, 22 Aug 2003 10:34:39 -0400, Tom Lane wrote:

> 
> "scott.marlowe" <scott.marlowe@ihs.com> writes:
> > Basically, for partial / functional indexes, the
> create index where clause 
> > needs to exactly (or nearly so) match the select
> query's where clause:
> 
> Another consideration is that an index declaration
like
> this:
> 
> 	create index fooi on foo (flag) where flag;
> 
> is really rather redundant, since the entries will
only
> be made for rows
> where flag is true, and so storage of the column value
> in the index is
> useless.  You may be able to get more mileage out of
> the index by making
> the index on another column that you often test in
> conjunction with the
> flag.  For example:
> 
> regression=# create table foo (flag bool, ts
timestamp);
> CREATE TABLE
> regression=# create index fooi on foo(ts) where flag;
> CREATE INDEX
> regression=# explain select * from foo where ts >
> '2003-10-11' and flag;
>                                    QUERY PLAN
>
--------------------------------------------------------------------------------
>  Seq Scan on foo  (cost=0.00..22.50 rows=167 width=9)
>    Filter: ((ts > '2003-10-11 00:00:00'::timestamp
> without time zone) AND flag)
> (2 rows)
> 
> regression=# set enable_seqscan TO 0;
> SET
> regression=# explain select * from foo where ts >
> '2003-10-11' and flag;
>                                QUERY PLAN
>
-------------------------------------------------------------------------
>  Index Scan using fooi on foo  (cost=0.00..43.25
> rows=167 width=9)
>    Index Cond: (ts > '2003-10-11 00:00:00'::timestamp
> without time zone)
>    Filter: flag
> (3 rows)
> 
> (In a more realistic situation, the planner would
> probably have chosen
> the indexscan without any prompting.)
> 
> 			regards, tom lane
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2003-08-22 22:48 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-08-21 23:20 Partial indexes (was: Re: Indexing a boolean) Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>
2003-08-22 13:48 ` scott.marlowe <scott.marlowe@ihs.com>
2003-08-22 14:34   ` Tom Lane <tgl@sss.pgh.pa.us>
2003-08-22 22:48 Re: Partial indexes (was: Re: Indexing a boolean) Nikolaus Dilger <nikolaus@dilger.cc>

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