SELECT max(Bug.heat) FROM Bug, BugTask, DistroSeries
WHERE BugTask.bug = Bug.id
AND BugTask.distroseries = DistroSeries.id
AND DistroSeries.distribution=3;
I can't really explain why PG is choosing such an appalling query plan for the current query. I suspect it to do with ORDER BY Bug.heat DESC LIMIT 1 not quite being identical to MAX(Bug.heat) - the first will return NULL if there are any NULL Bug.heat due to the default ordering of NULL values. We are smart enough to know there is a NOT NULL constraint on that column, but PG 8.3 might not be.
This rewritten query runs in 60ms on production:
SELECT max(Bug.heat) FROM Bug, BugTask, DistroSeries distroseries = DistroSeries.id distribution= 3;
WHERE BugTask.bug = Bug.id
AND BugTask.
AND DistroSeries.
I can't really explain why PG is choosing such an appalling query plan for the current query. I suspect it to do with ORDER BY Bug.heat DESC LIMIT 1 not quite being identical to MAX(Bug.heat) - the first will return NULL if there are any NULL Bug.heat due to the default ordering of NULL values. We are smart enough to know there is a NOT NULL constraint on that column, but PG 8.3 might not be.