optimisations to aggregates

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: optimisations to aggregates
Date: 2010-01-05 15:26:48
Message-ID: 20100105152648.GA5407@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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/

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2010-01-05 15:27:45 Re: FM format modifier does not remove leading zero from year
Previous Message howardnews@selestial.com 2010-01-05 15:24:11 Using regex to update a table