Re: Finding uniques across a big join - Mailing list pgsql-general

From John D. Burger
Subject Re: Finding uniques across a big join
Date
Msg-id 1a3334be40bb73af9cfc7a25e6e9b49a@mitre.org
Whole thread Raw
In response to Re: Finding uniques across a big join  (Greg Stark <gsstark@mit.edu>)
List 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


pgsql-general by date:

Previous
From: Tony Caduto
Date:
Subject: Re: Function returning SETOF
Next
From: Stephen Frost
Date:
Subject: Re: Switchover : WAL archiving and shutdown...