Re: Finding uniques across a big join

From: "John D(dot) Burger" <john(at)mitre(dot)org>
To: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Finding uniques across a big join
Date: 2005-12-01 20:15:01
Message-ID: 1a3334be40bb73af9cfc7a25e6e9b49a@mitre.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Greg Stark wrote:

> select max(personid) as personid, eyecolor, haircolor, skincolor
> from persons
> group by eyecolor, haircolor, skincolor
> having count(*) = 1

Aha, I understand Bruno's suggestion now! I was actually trying to
think of some way of using an aggregate on personid, but couldn't
figure it out. Of course max does just what I want in this case, since
max on one value gives that value - min and some other aggregate
functions would work too. Very clever!

On my actual problem, where "persons" is actually three joined tables,
my original query took eight hours. The new query, modeled after the
above, runs in almost exactly a tenth of the time.

Thanks for all the suggestions!

- John D. Burger
MITRE

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephen Frost 2005-12-01 20:17:43 Re: Switchover : WAL archiving and shutdown...
Previous Message Tony Caduto 2005-12-01 19:59:55 Re: Function returning SETOF