pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurent Martelli <laurent@aopsys.com>
To: pgsql-performance@postgresql.org
Subject: Speeding up select distinct
Date: Wed, 16 Mar 2005 18:58:35 +0100
Message-ID: <87k6o74gms.fsf@stan.aopsys> (raw)

Consider this query:

SELECT distinct owner from pictures; 

 Unique  (cost=361.18..382.53 rows=21 width=4) (actual time=14.197..17.639 rows=21 loops=1)
   ->  Sort  (cost=361.18..371.86 rows=4270 width=4) (actual time=14.188..15.450 rows=4270 loops=1)
         Sort Key: "owner"
         ->  Seq Scan on pictures  (cost=0.00..103.70 rows=4270 width=4) (actual time=0.012..5.795 rows=4270 loops=1)
 Total runtime: 19.147 ms

I thought that 19ms to return 20 rows out of a 4000 rows table so I
added an index:

CREATE INDEX pictures_owner ON pictures (owner);

It gives a slight improvement:

 Unique  (cost=0.00..243.95 rows=21 width=4) (actual time=0.024..10.293 rows=21 loops=1)
   ->  Index Scan using pictures_owner on pictures  (cost=0.00..233.27 rows=4270 width=4) (actual time=0.022..8.227 rows=4270 loops=1)
 Total runtime: 10.369 ms

But still, it's a lot for 20 rows. I looked at other type of indexes,
but they seem to either not give beter perfs or be irrelevant. 

Any ideas, apart from more or less manually maintaining a list of
distinct owners in another table ?

-- 
Laurent Martelli
laurent@aopsys.com                                Java Aspect Components
http://www.aopsys.com/                          http://jac.objectweb.org




view thread (10+ messages)  latest in thread

Message-ID: <87k6o74gms.fsf@stan.aopsys>
Permalink:  ../87k6o74gms.fsf@stan.aopsys/
Also on:    postgresql.org/message-id/87k6o74gms.fsf@stan.aopsys

 · 

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: pgsql-performance@postgresql.org
  Cc: laurent@aopsys.com
  Subject: Re: Speeding up select distinct
  In-Reply-To: <87k6o74gms.fsf@stan.aopsys>

* 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