Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 5ED372E0035 for ; Wed, 19 Dec 2007 05:47:34 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 65880-07 for ; Wed, 19 Dec 2007 05:47:30 -0400 (AST) Received: from oxford.xeocode.com (unknown [87.127.95.196]) by postgresql.org (Postfix) with ESMTP id 362A12E0032 for ; Wed, 19 Dec 2007 05:47:30 -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 1J4vWO-0004VE-Us; Wed, 19 Dec 2007 09:47:25 +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:47:24 +0000 Message-ID: <87wsrbhv83.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/275 X-Sequence-Number: 28091 "Richard Huxton" writes: > Paul Lambert wrote: > >> " -> Sort (cost=30197.98..30714.85 rows=206748 width=16) (actual >> time=5949.691..7018.931 rows=206748 loops=1)" >> " Sort Key: dealer_id, year_id, subledger_id, account_id" >> " Sort Method: external merge Disk: 8880kB" > Before that though, try issuing a "SET work_mem = '9MB'" before running your > query. If that doesn't change the plan step up gradually. You should be able to > get the sort stage to happen in RAM rather than on disk (see "Sort Method" > above). FWIW you'll probably need more than that. Try something more like 20MB. Also, note you can change this with SET for just this connection and even just this query and then reset it to the normal value (or use SET LOCAL). You don't have to change it in the config file and restart the whole server. 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. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com Get trained by Bruce Momjian - ask me about EnterpriseDB's PostgreSQL training!