Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 37A6F2E0064 for ; Wed, 19 Dec 2007 05:50:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 62230-03 for ; Wed, 19 Dec 2007 05:50:22 -0400 (AST) Received: from oxford.xeocode.com (unknown [87.127.95.196]) by postgresql.org (Postfix) with ESMTP id 384812E025B for ; Wed, 19 Dec 2007 05:50:31 -0400 (AST) Received: from localhost.enta.net ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.67) (envelope-from ) id 1J4vZN-0004VP-FS; Wed, 19 Dec 2007 09:50:29 +0000 To: "Richard Huxton" Cc: "Paul Lambert" , Subject: Re: Optimising a query In-Reply-To: <4768DAC0.4010304@archonet.com> (Richard Huxton's message of "Wed\, 19 Dec 2007 08\:48\:00 +0000") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.performance" 3687) References: <4768B4D1.1090702@reynolds.com.au> <4768BE4B.9080308@reynolds.com.au> <4768DAC0.4010304@archonet.com> From: Gregory Stark Organization: EnterpriseDB Date: Wed, 19 Dec 2007 09:50:29 +0000 Message-ID: <87ve6vhv2y.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/276 X-Sequence-Number: 28092 > Also, try replacing the DISTINCT with GROUP BY. The code path for DISTINCT > unfortunately needs a bit of cleaning up and isn't exactly equivalent to GROUP > BY. In particular it doesn't support hash aggregates which, if your work_mem > is large enough, might work for you here. Sorry, strike that last suggestion. I was looking at the plan and forgot that the query had DISTINCT ON. It is possible to replace DISTINCT ON with GROUP BY but it's not going to be faster than the DISTINCT ON case since you'll need the sort anyways. Actually it's possible to do without the sort if you write some fancy aggregate functions but for this large a query that's going to be awfully complex. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com Get trained by Bruce Momjian - ask me about EnterpriseDB's PostgreSQL training!