Re: intelligence in writing a query ... - Mailing list pgsql-hackers

From Tom Lane
Subject Re: intelligence in writing a query ...
Date
Msg-id 4891.991258533@sss.pgh.pa.us
Whole thread Raw
In response to intelligence in writing a query ...  (The Hermit Hacker <scrappy@hub.org>)
Responses Re: intelligence in writing a query ...  (Don Baccus <dhogaza@pacifier.com>)
List pgsql-hackers
The Hermit Hacker <scrappy@hub.org> writes:
> 2EXPLAIN SELECT distinct s.gid, s.created, count(i.title) AS images
>               FROM status s LEFT JOIN images i ON (s.gid = i.gid AND i.active), relationship_wanted rw
>              WHERE s.active AND s.status != 0
>                AND EXISTS ( SELECT gid
>                                 FROM relationship_wanted
>                                WHERE gender = 1 )
>                AND EXISTS ( SELECT gid
>                                 FROM personal_data
>                                WHERE gender = 0
>                                  AND ( ( age('now', dob) > '26 years' ) AND ( age('now', dob) < '46 years' ) )
>                                  AND country IN ( 'US' ) )
>           GROUP BY s.gid,s.created
>           ORDER BY  images desc;

I don't understand what you're trying to do here.  The inner SELECTs
aren't dependent on anything in the outer query, so what are they for?

> ... status and
> personal_data have a 1-to-1 relationship,

Then why have two tables?  Merge them into one table and save yourself a
join.

Also, since status.gid is (I assume) unique, what's the use of the
DISTINCT clause at the top level?  Seems like that's costing you
a useless sort & unique pass ...
        regards, tom lane


pgsql-hackers by date:

Previous
From: Paulo Angelo
Date:
Subject: Sync Data
Next
From: Ian Lance Taylor
Date:
Subject: Re: Support for %TYPE in CREATE FUNCTION