Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 647762E005B for ; Thu, 21 Feb 2008 04:34:15 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 85071-04 for ; Thu, 21 Feb 2008 04:34:12 -0400 (AST) Received: from pih-relay06.plus.net (pih-relay06.plus.net [212.159.14.133]) by postgresql.org (Postfix) with ESMTP id A028D2E0032 for ; Thu, 21 Feb 2008 04:34:12 -0400 (AST) Received: from [84.51.143.99] (helo=server3.office.archonet.com) by pih-relay06.plus.net with esmtp (Exim) id 1JS6sc-0005Xl-HZ; Thu, 21 Feb 2008 08:34:10 +0000 Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id 22AC4274060; Thu, 21 Feb 2008 08:34:10 +0000 (GMT) Message-ID: <47BD3781.6000503@archonet.com> Date: Thu, 21 Feb 2008 08:34:09 +0000 From: Richard Huxton User-Agent: Thunderbird 2.0.0.9 (X11/20071031) MIME-Version: 1.0 To: Markus Bertheau CC: pgsql-performance@postgresql.org Subject: Re: planner favors seq scan too early References: <684362e10802202304o2603105fl7272312e25cc7a19@mail.gmail.com> In-Reply-To: <684362e10802202304o2603105fl7272312e25cc7a19@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Plusnet-Relay: c3158bb85b5e36e2e75e54287c7ad4b0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/264 X-Sequence-Number: 28806 Markus Bertheau wrote: > > I'm getting a plan that uses a sequential scan on ext_feeder_item instead of > several index scans, which slows down the query significantly: > > # explain analyze select fi.pub_date from ext_feeder_item fi where fi.feed_id > in (select id from ext_feeder_feed ff where ff.is_system) order by pub_date > desc; > Sort (cost=298545.70..299196.46 rows=260303 width=8) (actual > time=89299.623..89302.146 rows=807 loops=1) > Using LIMIT in the subquery I can see that starting with 50 values for the in > the planner starts to prefer the seq scan. Plan for 49: > Sort (cost=277689.24..277918.39 rows=91660 width=8) (actual > time=477.769..478.193 rows=137 loops=1) > Note that the rows estimate for the index scan is way off. Increasing > statistics target for ext_feeder_item.feed_id to 100 lets the planner favor the > index scan up to LIMIT 150 for the subquery. > > Using enable_seqscan=false, I see that the index scan plan continues to > outperform the seqscan plan even with limit 1500 in the subquery (1196 values > actually returned from it): > Sort (cost=100925142.27..100925986.74 rows=337787 width=8) (actual > time=102.111..104.627 rows=807 loops=1) > Why does the planner choose that way and what can I do to make it choose the > better plan, preferably without specifying limit and a maybe unreasonably high > statistics target for ext_feeder_item.feed_id? Although the index scans are fast enough, the cost estimate is much more. This suggests you need to tweak your planner cost settings: http://www.postgresql.org/docs/8.3/static/runtime-config-query.html#RUNTIME-CONFIG-QUERY-CONSTANTS I'd probably start with reducing random_page_cost if you have a reasonable disk system and making sure effective_cache_size is accurately set. -- Richard Huxton Archonet Ltd