Received: from localhost (uranus.hub.org [200.46.204.60]) by postgresql.org (Postfix) with ESMTP id 88A7A9FB2C8 for ; Thu, 23 Nov 2006 11:20:44 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.60]) (amavisd-new, port 10024) with ESMTP id 41735-01-3 for ; Thu, 23 Nov 2006 11:20:41 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail01.enterprisedb.com (mail01.enterprisedb.com [63.246.7.168]) by postgresql.org (Postfix) with ESMTP id BCED19FB2A2 for ; Thu, 23 Nov 2006 11:20:41 -0400 (AST) thread-index: AccPEvE4xY1K/sVIRBetfNC7YAS8Cg== Received: from [192.168.0.20] ([62.232.55.118]) by mail01.enterprisedb.com with Microsoft SMTPSVC(6.0.3790.1830); Thu, 23 Nov 2006 10:20:43 -0500 Subject: Re: PostgreSQL underestimates sorting From: "Simon Riggs" Content-Class: urn:content-classes:message Importance: normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.2757 To: "Markus Schaber" Cc: "PostgreSQL Performance List" In-Reply-To: <456423B3.7010505@logix-tt.com> References: <456423B3.7010505@logix-tt.com> Content-Type: text/plain; charset="iso-8859-1" Date: Thu, 23 Nov 2006 15:20:35 +0000 Message-ID: <1164295236.3841.629.camel@silverbirch.site> MIME-Version: 1.0 X-Mailer: Evolution 2.6.0 Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 23 Nov 2006 15:20:43.0187 (UTC) FILETIME=[F1152030:01C70F12] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/200 X-Sequence-Number: 21970 On Wed, 2006-11-22 at 11:17 +0100, Markus Schaber wrote: > PostgreSQL 8.1 (and, back then, 7.4) have the tendency to underestimate > the costs of sort operations, compared to index scans. > > The Backend allocates gigs of memory (we've set sort_mem to 1 gig), and > then starts spilling out more Gigs of temporary data to the disk. So the > execution gets - in the end - much slower compared to an index scan, and > wastes lots of disk space. > > We did not manage to tune the config values appropriately, at least not > without causing other query plans to suffer badly. 8.2 has substantial changes to sort code, so you may want to give the beta version a try to check for how much better it works. That's another way of saying that sort in 8.1 and before has some performance problems when you are sorting more than 6 * 2 * work_mem (on randomly sorted data) and the cost model doesn't get this right, as you observe. Try enabling trace_sort (available in both 8.1 and 8.2) and post the results here please, which would be very useful to have results on such a large real-world sort. -- Simon Riggs EnterpriseDB http://www.enterprisedb.com