Hi,
I've just realised that I'm performing the same rewrite on lots of my
queries to get performance reasonable. They take the form of something
like:
SELECT a.x, b.y, COUNT(*) AS n
FROM foo a, bar b
WHERE a.z = b.z
GROUP BY a.x, b.y;
And I rewrite them to:
SELECT a.x, b.y, SUM(b.count) AS n
FROM foo a, (
SELECT y, z, COUNT(*)
FROM bar
GROUP BY y, z) b
WHERE a.z = b.z
GROUP BY a.x, b.y;
Obviously this is only a win when "bar" is large enough that doing the
aggregation reduces the number of rows significantly, hence we're also
predicated on there being a small number of distinct (y,z) values.
This seems like a somewhat easy rewrite that the planner could be doing
itself, but the general case seems harder. Extending the aggregate
abstraction as I suggested here:
http://archives.postgresql.org/pgsql-hackers/2009-11/msg00322.php
would make this transform possible in the general case. It still seems
a bit fiddly to detect though.
--
Sam http://samason.me.uk/