From: Joel Burton <joel@joelburton.com>
To: David F. Skoll <dfs@roaringpenguin.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: What kind of index to use for many rows with few unique values?
Date: Mon, 2 Dec 2002 18:04:38 -0500
Message-ID: <20021202230438.GA16554@temp.joelburton.com> (raw)
In-Reply-To: <Pine.LNX.4.50.0212021707290.6178-100000@shishi.roaringpenguin.com>
References: <Pine.LNX.4.50.0212021707290.6178-100000@shishi.roaringpenguin.com>
On Mon, Dec 02, 2002 at 05:10:00PM -0500, David F. Skoll wrote:
> Hi,
>
> I have a table with a column called "state". Each row can be in one
> of four states, let's call them 'new', 'pending', 'ok', and 'bad'.
> On average, about 95% of the rows will be 'bad', with the remaining
> 5% being in one of the other three states. If the table has 50K rows
> and I just want to pull out the 'ok' rows, I don't want to do a sequential
> scan. To pull out the 'bad' rows, obviously, sequential scan is fine.
>
> I've heard that a btree index performs badly in this situation. Is
> a hash index appropriate? I've heard bad things about hash indexes in
> PostgreSQL.
create table states (id serial primary key, state varchar(10), t text );
create function fill_states(varchar, int) returns bool as '
begin for i in 1 .. $2
loop
insert into states (state, t) values ($1, ''random'');
end loop;
return true;
end;
' language plpgsql;
select fill_states('ok',45000);
select fill_states('bad',5000);
select fill_states('warning',5000);
analyze states;
joel@joel=# explain select * from states where state='warning';
QUERY PLAN
-------------------------------------------------------------------------------
Index Scan using state_idx on states (cost=0.00..1013.00 rows=5533 width=20)
Index Cond: (state = 'warning'::character varying)
(2 rows)
joel@joel=# explain select * from states where state='ok';
QUERY PLAN
--------------------------------------------------------------
Seq Scan on states (cost=0.00..1171.51 rows=44627 width=20)
Filter: (state = 'ok'::character varying)
(2 rows)
Looks right to me: index scan for the less-common option, seqscan for
the most common. Why don't you think this, as a btree, will work for
you?
--
Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Independent Knowledge Management Consultant
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: joel@joelburton.com, dfs@roaringpenguin.com
Subject: Re: What kind of index to use for many rows with few unique values?
In-Reply-To: <20021202230438.GA16554@temp.joelburton.com>
References: <Pine.LNX.4.50.0212021707290.6178-100000@shishi.roaringpenguin.com> <20021202230438.GA16554@temp.joelburton.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