Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 9A8DBB5DBC8 for ; Tue, 27 Sep 2011 06:29:10 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 48222-07 for ; Tue, 27 Sep 2011 09:29:04 +0000 (UTC) X-Greylist: delayed 00:06:31.995911 by SQLgrey-1.8.0-rc2 Received: from mail.catalyst.net.nz (bertrand.catalyst.net.nz [202.78.240.40]) by mail.postgresql.org (Postfix) with ESMTP id 15E84B5DBBD for ; Tue, 27 Sep 2011 06:29:04 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.catalyst.net.nz (Postfix) with ESMTP id 136EF3201A for ; Tue, 27 Sep 2011 22:22:29 +1300 (NZDT) X-Virus-Scanned: Debian amavisd-new at catalyst.net.nz Received: from mail.catalyst.net.nz ([127.0.0.1]) by localhost (bertrand.catalyst.net.nz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PF4cZQ2tvSlM for ; Tue, 27 Sep 2011 22:22:28 +1300 (NZDT) Received: from [192.168.1.11] (122-60-64-230.jetstream.xtra.co.nz [122.60.64.230]) (Authenticated sender: mark.kirkwood) by mail.catalyst.net.nz (Postfix) with ESMTPSA id 0E833325AF for ; Tue, 27 Sep 2011 22:22:27 +1300 (NZDT) Message-ID: <4E8195D3.8010809@catalyst.net.nz> Date: Tue, 27 Sep 2011 22:22:27 +1300 From: Mark Kirkwood User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.21) Gecko/20110831 Thunderbird/3.1.13 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: overzealous sorting? References: <201109261628.15363.anthony.shipman@symstream.com> <201109271245.00979.anthony.shipman@symstream.com> <20110927105435.26d4e9c6@marco-dalibo> <201109271905.09573.anthony.shipman@symstream.com> In-Reply-To: <201109271905.09573.anthony.shipman@symstream.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/387 X-Sequence-Number: 45163 On 27/09/11 22:05, anthony.shipman@symstream.com wrote: > > What I really want is to just read a sequence of records in timestamp order > between two timestamps. The number of records to be read may be in the > millions totalling more than 1GB of data so I'm trying to read them a slice > at a time but I can't get PG to do just this. > > If I use offset and limit to grab a slice of the records from a large > timestamp range then PG will grab all of the records in the range, sort them > on disk and return just the slice I want. This is absurdly slow. > > The query that I've shown is one of a sequence of queries with the timestamp > range progressing in steps of 1 hour through the timestamp range. All I want > PG to do is find the range in the index, find the matching records in the > table and return them. All of the planner's cleverness just seems to get in > the way. > It is not immediately clear that the planner is making the wrong choices here. Index scans are not always the best choice, it depends heavily on the correlation of the column concerned to the physical order of the table's heap file. I suspect the reason for the planner choosing the bitmap scan is that said correlation is low (consult pg_stats to see). Now if you think that the table's heap data is cached anyway, then this is not such an issue - but you have to tell the planner that by reducing random_page_cost (as advised previously). Give it a try and report back! regards Mark