optimisations to aggregates

Поиск
Список
Период
Сортировка
От Sam Mason
Тема optimisations to aggregates
Дата
Msg-id 20100105152648.GA5407@samason.me.uk
обсуждение исходный текст
Список 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/

В списке pgsql-general по дате отправления:

Предыдущее
От: Steve White
Дата:
Сообщение: Re: access computed field of RECORD variable
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: FM format modifier does not remove leading zero from year