Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 0C308632EED for ; Thu, 22 Apr 2010 09:37:59 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 63193-04 for ; Thu, 22 Apr 2010 12:37:40 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.186]) by mail.postgresql.org (Postfix) with ESMTP id 910AA63332E for ; Thu, 22 Apr 2010 09:37:40 -0300 (ADT) Received: from [192.168.0.105] (pool-96-244-14-10.bltmmd.fios.verizon.net [96.244.14.10]) by mrelayeu.kundenserver.de (node=mrbap1) with ESMTP (Nemesis) id 0MFR0G-1OAU6z3ZNk-00EiOm; Thu, 22 Apr 2010 14:37:34 +0200 Message-ID: <4BD0430C.1050308@2ndquadrant.com> Date: Thu, 22 Apr 2010 08:37:32 -0400 From: Greg Smith User-Agent: Thunderbird 2.0.0.24 (X11/20100317) MIME-Version: 1.0 To: Vlad Arkhipov CC: pgsql-performance@postgresql.org Subject: Re: Optimization idea References: <4BD0161A.4010908@dc.baikal.ru> In-Reply-To: <4BD0161A.4010908@dc.baikal.ru> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V01U2FsdGVkX1+hUK/q/OWpB3zLCTVUJC7Ynz8KevmZLF0ISJl Do1dfULFjsplH21llx6RHjgM5TMa1M5HgnLOSRpQ9lfItX7x0U lvSWdIyffZbIMIhBIXVCy2J7aFx+vmT X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.026 tagged_above=-10 required=5 tests=AWL=-1.025, BAYES_40=-0.001 X-Spam-Level: X-Archive-Number: 201004/287 X-Sequence-Number: 38537 Vlad Arkhipov wrote: > Please do this small optimization if it is possible. It seem that the > optimizer have the all information to create a fast plan but it does > not do that. This isn't strictly an optimization problem; it's an issue with statistics the optimizer has to work with, the ones ANALYZE computes. You noticed this yourself: > HERE IS THE PROBLEM. IF THE ESTIMATED COUNT = 1 OPTIMIZER BUILDS THE > CORRECT FAST PLAN, BUT IF THE ESTIMATION IS GREATER THAN 1 WE HAVE A > PROBLEM See http://www.postgresql.org/docs/current/static/planner-stats.html for an intro to this area. You didn't mention your PostgreSQL version. If you're running 8.3 or earlier, an increase to default_statistics_target might be in order to get more data about the distribution of data in the table, to reduce the odds of what you're seeing happening. I can't replicate your problem on the current development 9.0; all three plans come back with results quickly when I just tried it: Nested Loop (cost=0.00..50.76 rows=204 width=32) (actual time=0.049..0.959 rows=200 loops=1) -> Seq Scan on t1 (cost=0.00..1.06 rows=1 width=16) (actual time=0.013..0.016 rows=1 loops=1) Filter: (t = 2) -> Index Scan using t_idx on t2 (cost=0.00..47.66 rows=204 width=16) (actual time=0.029..0.352 rows=200 loops=1) Index Cond: (t2.t = 2) Total runtime: 1.295 ms Nested Loop (cost=0.00..1042.77 rows=20020 width=32) (actual time=0.042..0.437 rows=100 loops=1) -> Seq Scan on t1 (cost=0.00..1.06 rows=1 width=16) (actual time=0.013..0.015 rows=1 loops=1) Filter: (id = 3) -> Index Scan using t_idx on t2 (cost=0.00..791.45 rows=20020 width=16) (actual time=0.022..0.164 rows=100 loops=1) Index Cond: (t2.t = t1.t) Total runtime: 0.608 ms Bitmap Heap Scan on t2 (cost=16.11..558.73 rows=433 width=16) (actual time=0.095..0.674 rows=400 loops=1) Recheck Cond: (t = ANY ('{2,3,4}'::bigint[])) -> Bitmap Index Scan on t_idx (cost=0.00..16.00 rows=433 width=0) (actual time=0.075..0.075 rows=400 loops=1) Index Cond: (t = ANY ('{2,3,4}'::bigint[])) Total runtime: 1.213 ms -- Greg Smith 2ndQuadrant US Baltimore, MD PostgreSQL Training, Services and Support greg@2ndQuadrant.com www.2ndQuadrant.us