Re: bad estimates / non-scanning aggregates - Mailing list pgsql-performance

From Bruno Wolff III
Subject Re: bad estimates / non-scanning aggregates
Date
Msg-id 20030829025109.GD4373@wolff.to
Whole thread Raw
In response to bad estimates / non-scanning aggregates  (Ken Geis <kgeis@speakeasy.org>)
Responses Re: bad estimates / non-scanning aggregates
List pgsql-performance
On Thu, Aug 28, 2003 at 17:10:31 -0700,
  Ken Geis <kgeis@speakeasy.org> wrote:
> The query I want to run is
>
> select stock_id, min(price_date) from day_ends group by stock_id;

The fast way to do this is:

select distinct on (stock_id) stock_id, price_date
  order by stock_id, price_date;

> Also, to get the MIN for a given group, not all values of the index need
> to be seen.  Must pgsql do a full scan because it treats all aggregates
> in the same way?  Are MIN and MAX used often enough to justify special
> treatment, and could that be cleanly implemented?  Perhaps the aggregate
> function can request the data in a certain order, be told that it is
> being passed data in a certain order, and return before seeing the
> entire set of data.

Yes, max and min are not treated special so they don't benefit from
indexes. This has been discussed repeatedly in the archives.

pgsql-performance by date:

Previous
From: Bill Moran
Date:
Subject: Re: The results of my PostgreSQL/filesystem performance
Next
From: Ken Geis
Date:
Subject: Re: bad estimates / non-scanning aggregates