Re: Non-unique index performance

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Sezai YILMAZ <sezai(dot)yilmaz(at)pro-g(dot)com(dot)tr>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Non-unique index performance
Date: 2005-06-24 09:29:02
Message-ID: 20050624092902.GA6779@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jun 24, 2005 at 11:44:50AM +0300, Sezai YILMAZ wrote:
> Hello!
>
> I have a table eith name person as described below. It has an unique
> index for id column (it is also primary key) and has an index for parent
> column.

<snip>

> Why the difference of both queries is so dramatical for unique and
> non-unique indexed columns? Why PostgreSQL does not use the non-unique
> indexes (it says that it does sequential scan)?

<snip>

It has nothing to do with the index and everything to do with how many
rows it expected to return. If you look at the explain output you'll
see that the first only expected 5 rows to be returned, whereas the
second expected 14427 rows. Looking up 14000 rows in a index is rather
expensive so PostgreSQL decided that a seq scan would be faster.

If these numbers arn't accurate, you need to show EXPLAIN ANALYZE
output as well as checking how often you run ANALYZE generally.

Hope this helps,

> test=> explain analyze select id,name from person where id in ('17201',
> '338191', '244319', '515209', '20415');
> QUERY PLAN
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> Index Scan using person_pkey, person_pkey, person_pkey, person_pkey,
> person_pkey on person (cost=0.00..30.12 rows=5 width=18) (actual
> time=56.817..225.760 rows=5 loops=1)
> Index Cond: ((id = 17201) OR (id = 338191) OR (id = 244319) OR (id =
> 515209)OR (id = 20415))
> Total runtime: 225.893 ms
> (3 rows)
>
>
>
>
> test=> explain analyze select * from person where parent in ('17201',
> '338191', '244319', '515209', '20415');
> QUERY PLAN
> -----------------------------------------------------------------------------------------------------------------
> Seq Scan on person (cost=0.00..35871.26 rows=14427 width=32) (actual
> time=0.063..11192.809 rows=5 loops=1)
> Filter: ((parent = 17201) OR (parent = 338191) OR (parent = 244319)
> OR (parent = 515209) OR (parent = 20415))
> Total runtime: 11192.913 ms
> (3 rows)
>

--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sezai YILMAZ 2005-06-24 10:13:03 Re: Non-unique index performance
Previous Message Richard Huxton 2005-06-24 09:26:17 Re: Postgres 8.0 windows processes, field testing, and