Question, how intelligent is optimizer with subplans? - Mailing list pgsql-general

From Gregory Stark
Subject Question, how intelligent is optimizer with subplans?
Date
Msg-id 87iswjvdks.fsf@stark.dyndns.tv
Whole thread Raw
Responses Re: Question, how intelligent is optimizer with subplans?
List pgsql-general
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

pgsql-general by date:

Previous
From: Justin Clift
Date:
Subject: Re: Writing apps for ORDBMS
Next
From: Greg Stark
Date:
Subject: Re: Question, how intelligent is optimizer with subplans?