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

From Bruno Wolff III
Subject Re: bad estimates / non-scanning aggregates
Date
Msg-id 20030829030156.GF4373@wolff.to
Whole thread Raw
In response to Re: bad estimates / non-scanning aggregates  (Ken Geis <kgeis@speakeasy.org>)
Responses Re: bad estimates / non-scanning aggregates  (Ken Geis <kgeis@speakeasy.org>)
List pgsql-performance
On Thu, Aug 28, 2003 at 19:50:38 -0700,
  Ken Geis <kgeis@speakeasy.org> wrote:
> Bruno Wolff III wrote:
> >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;
>
> Not according to the optimizer!  Plus, this is not guaranteed to return
> the correct results.

For it to be fast you need an index on (stock_id, price_date) so that
you can use an index scan.

The answers are guarenteed to be correct. See:
http://developer.postgresql.org/docs/postgres/sql-select.html#SQL-DISTINCT

pgsql-performance by date:

Previous
From: Ken Geis
Date:
Subject: Re: bad estimates / non-scanning aggregates
Next
From: Ken Geis
Date:
Subject: Re: bad estimates / non-scanning aggregates