In general if I have a query like
SELECT a,
FROM (SELECT foo, (select count(*) from anothertable where a=t.a) AS n FROM x) AS t
WHERE foo = 0
Is the optimizer clever enough not to run the "select count(*)" for every
record in x but only the ones where foo=0?
I can play with explain, but I won't know if I'm asking it to do the
impossible or not.
In case it's not obvious, the actual query is much more complex and involves a
DISTINCT ON() and a sort and the "select count(*)" is part of the sort so I
don't want to push it out manually and duplicate the expression.
--
greg