Re: Optimizer internals - Mailing list pgsql-performance

From Mark Lewis
Subject Re: Optimizer internals
Date
Msg-id 1150398064.31200.76.camel@archimedes
Whole thread Raw
In response to Re: Optimizer internals  ("John Vincent" <pgsql-performance@lusis.org>)
Responses Re: Optimizer internals  ("John Vincent" <pgsql-performance@lusis.org>)
List pgsql-performance
On Thu, 2006-06-15 at 14:46 -0400, John Vincent wrote:

> One question that we came up with is how does this affect other
> aggregate functions like MAX,MIN,SUM and whatnot? Being that this is
> our data warehouse, we use these all the time. As I've said
> previously, I didn't know a human could generate some of the queries
> we've passed through this system.

Previously, MIN and MAX would also run slowly, for the same reason as
COUNT(*).  But there really isn't a need for that, since you can still
get a big speedup by scanning the index in order, looking up each row
and stopping as soon as you find a visible one.

This has been fixed so newer versions of PG will run quickly and use the
index for MIN and MAX.  I don't remember which version had that change;
it might not be until 8.2.  You can dig the archives to find out for
sure.

For older versions of PG before the fix, you can make MIN and MAX run
quickly by rewriting them in the following form:

SELECT column FROM table ORDER BY column LIMIT 1;

Unfortunately SUM is in the same boat as COUNT; in order for it to
return a meaningful result it must inspect visibility information for
all of the rows.

-- Mark

pgsql-performance by date:

Previous
From: "John Vincent"
Date:
Subject: Re: Optimizer internals
Next
From: "John Vincent"
Date:
Subject: Re: Optimizer internals