Re: Optimizer internals

From: Mark Lewis <mark(dot)lewis(at)mir3(dot)com>
To: pgsql-performance(at)lusis(dot)org
Cc: PGSQL Performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Optimizer internals
Date: 2006-06-15 19:01:03
Message-ID: 1150398064.31200.76.camel@archimedes
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message John Vincent 2006-06-15 19:21:50 Re: Optimizer internals
Previous Message John Vincent 2006-06-15 18:46:11 Re: Optimizer internals