Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 1B4AB63333A for ; Tue, 13 Apr 2010 15:00:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 55530-10 for ; Tue, 13 Apr 2010 17:59:58 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mta01.eastlink.ca (mta01.eastlink.ca [24.224.136.8]) by mail.postgresql.org (Postfix) with ESMTP id 90061633324 for ; Tue, 13 Apr 2010 14:59:58 -0300 (ADT) MIME-version: 1.0 Content-transfer-encoding: 7BIT Content-type: text/plain; CHARSET=US-ASCII Received: from ip01.eastlink.ca ([unknown] [24.222.39.10]) by mta01.eastlink.ca (Sun Java(tm) System Messaging Server 7u3-12.01 64bit (built Oct 15 2009)) with ESMTP id <0L0T009P7U03XD41@mta01.eastlink.ca> for pgsql-performance@postgresql.org; Tue, 13 Apr 2010 15:00:03 -0300 (ADT) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArgFAIZNxEsY4NjU/2dsb2JhbACPaoxTvUaFDQQ X-IronPort-AV: E=Sophos;i="4.52,198,1270436400"; d="scan'208";a="188466875" Received: from blk-224-216-212.eastlink.ca (HELO mail.dreadnet.org) ([24.224.216.212]) by ip01.eastlink.ca with ESMTP; Tue, 13 Apr 2010 15:09:24 -0300 Received: from amavis.dreadnet.org (amavis.dreadnet.org [192.168.200.18]) by mail.dreadnet.org (Postfix) with ESMTP id 055356D73C34 for ; Tue, 13 Apr 2010 14:59:57 -0300 (ADT) X-Virus-Scanned: amavisd-new at dreadnet.org Received: from mail.dreadnet.org ([192.168.200.15]) by amavis.dreadnet.org (amavis.dreadnet.org [192.168.200.18]) (amavisd-new, port 10024) with ESMTP id uwdly7X0jJ+c for ; Tue, 13 Apr 2010 14:59:55 -0300 (ADT) Received: from [192.168.200.101] (efnisien.dreadnet.org [192.168.200.101]) by mail.dreadnet.org (Postfix) with ESMTPA id D0C166D73C31 for ; Tue, 13 Apr 2010 14:59:55 -0300 (ADT) Message-id: <4BC4B11B.6090106@accesswave.ca> Date: Tue, 13 Apr 2010 14:59:55 -0300 From: Chris Bowlby User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.5) Gecko/20091130 SUSE/3.0.0-16.1 Thunderbird/3.0 To: pgsql-performance@postgresql.org Subject: Re: significant slow down with various LIMIT References: <4BC2C7140200002500030705@gw.wicourts.gov> <4BC34A340200002500030759@gw.wicourts.gov> <4BC4627202000025000307E5@gw.wicourts.gov> In-reply-to: <4BC4627202000025000307E5@gw.wicourts.gov> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201004/166 X-Sequence-Number: 38416 I'm also wondering if a re-clustering of the table would work based on the index that's used. such that: CLUSTER core_object USING plugins_plugin_addr_oid_id; and see if that makes any change in the differences that your seeing. On 04/13/2010 02:24 PM, Kevin Grittner wrote: > norn wrote: > > >> I am wondering why there are so big gap between two limits and how >> to avoid this... >> > > I think we've already established that it is because of the > percentage of the table which must be scanned to get to the desired > number of rows. The problem is exacerbated by the fact that it's a > "backward" scan on the index, which is slower than a forward scan -- > mainly because disks spin in one direction, and the spacing of the > sectors is optimized for forward scans. > > There are a couple things to try which will give a more complete > picture of what might work to make the run time more predictable. > Please try these, and run EXPLAIN ANALYZE of your problem query each > way. > > (1) Try it without the ORDER BY clause and the LIMIT. > > (2) Temporarily take that top index out of consideration. (Don't > worry, it'll come back when you issue the ROLLBACK -- just don't > forget the BEGIN statement.) > > BEGIN; > DROP INDEX plugins_plugin_addr_oid_id; > explain analyze > ROLLBACK; > > (3) Try it like this (untested, so you may need to fix it up): > > explain analyze > SELECT core_object.id > from (SELECT id, city_id FROM "plugins_guide_address") > "plugins_guide_address" > JOIN "plugins_plugin_addr" > ON ("plugins_plugin_addr"."address_id" > = "plugins_guide_address"."id") > JOIN "core_object" > ON ("core_object"."id" = "plugins_plugin_addr"."oid_id") > WHERE "plugins_guide_address"."city_id" = 4535 > ORDER BY "core_object"."id" DESC > LIMIT 4 -- or whatever it normally takes to cause the problem > ; > > -Kevin > >