X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id E99199FA5BE for ; Thu, 15 Jun 2006 16:01:15 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 39075-01 for ; Thu, 15 Jun 2006 16:01:06 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mir3-fs.mir3.com (mail.mir3.com [65.208.188.100]) by postgresql.org (Postfix) with ESMTP id 3F3969FA13E for ; Thu, 15 Jun 2006 16:01:05 -0300 (ADT) Received: mir3-fs.mir3.com 172.16.1.11 from 172.16.2.68 172.16.2.68 via HTTP with MS-WebStorage 6.0.6249 Received: from archimedes.mirlogic.com by mir3-fs.mir3.com; 15 Jun 2006 12:01:04 -0700 Subject: Re: Optimizer internals From: Mark Lewis To: pgsql-performance@lusis.org Cc: PGSQL Performance In-Reply-To: References: <1150396425.31200.66.camel@archimedes> Content-Type: text/plain Content-Transfer-Encoding: 7bit Organization: MIR3, Inc. Date: Thu, 15 Jun 2006 12:01:03 -0700 Message-Id: <1150398064.31200.76.camel@archimedes> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-27) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/323 X-Sequence-Number: 19680 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