pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurent Martelli <laurent@aopsys.com>
To: Merlin Moncure <merlin.moncure@rcsonline.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Speeding up select distinct
Date: Wed, 16 Mar 2005 19:38:30 +0100
Message-ID: <877jk74es9.fsf@stan.aopsys> (raw)
In-Reply-To: <6EE64EF3AB31D5448D0007DD34EEB3412A7650@Herge.rcsinc.local>
References: <6EE64EF3AB31D5448D0007DD34EEB3412A7650@Herge.rcsinc.local>

>>>>> "Merlin" == Merlin Moncure <merlin.moncure@rcsonline.com> writes:

  >> Consider this query:
  >> 
  >> SELECT distinct owner from pictures;

  Merlin> [...]
  >> Any ideas, apart from more or less manually maintaining a list of
  >> distinct owners in another table ?

  Merlin> you answered your own question.  With a 20 row owners table,
  Merlin> you should be directing your efforts there group by is
  Merlin> faster than distinct, but both are very wasteful and
  Merlin> essentially require s full seqscan of the detail table.

  Merlin> With a little hacking, you can change 'manual maintenance'
  Merlin> to 'automatic maintenance'.

  Merlin> 1. create table owner as select distinct owner from
  Merlin> pictures; 2. alter table owner add constraint
  Merlin> owner_pkey(owner); 3. alter table pictures add constraint
  Merlin> ri_picture_owner(owner) references owner; 4. make a little
  Merlin> append_ownder function which adds an owner to the owner
  Merlin> table if there is not already one there. Inline this to your
  Merlin> insert statement on pictures.

I just wished there was a means to fully automate all this and render
it transparent to the user, just like an index.

  Merlin> Voila!  Merlin p.s. normalize your data always!

I have this:

pictures(
    PictureID serial PRIMARY KEY,
    Owner integer NOT NULL REFERENCES users,
    [...]);
CREATE TABLE users (
    UserID serial PRIMARY KEY,
    Name character varying(255),
    [...]);

Isn't it normalized ?

-- 
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: <877jk74es9.fsf@stan.aopsys>
Permalink:  ../877jk74es9.fsf@stan.aopsys/
Also on:    postgresql.org/message-id/877jk74es9.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, merlin.moncure@rcsonline.com
  Subject: Re: Speeding up select distinct
  In-Reply-To: <877jk74es9.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