3 ms·
"As a general recommendation I advise database developers to avoid exactly this sort of "outsmart the planner"" Probably wise in general. This particular case
by jeffdavis 13y ago
"As a general recommendation I advise database developers to avoid exactly this sort of "outsmart the planner""
Probably wise in general. This particular case is peculiar because postgres doesn't optimize distinct aggregates at all (see my other comment: https://news.ycombinator.com/item?id=7115707 https://news.ycombinator.com/item?id=7115707 ), so it's possible that the rewritten query is actually more optimizable than the original.
The original query won't adapt to changing data sizes or distributions as well as the rewritten one.
This is not fundamental, nor universal. It's a specific limitation of postgres, so I still agree with your general advice. And postgres may fix this, so your advice may prove to be true even in this situation in the long run.