Re: intelligence in writing a query ...

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: The Hermit Hacker <scrappy(at)hub(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org, tech-fyi(at)hub(dot)org
Subject: Re: intelligence in writing a query ...
Date: 2001-05-30 21:35:33
Message-ID: 4891.991258533@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox
Thread:
Lists: pgsql-hackers

The Hermit Hacker <scrappy(at)hub(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jan Wieck 2001-05-30 22:00:05 Re: Support for %TYPE in CREATE FUNCTION
Previous Message Ian Lance Taylor 2001-05-30 21:22:38 Re: Support for %TYPE in CREATE FUNCTION