Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 346ED634132 for ; Fri, 28 May 2010 15:54:35 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 21629-08 for ; Fri, 28 May 2010 18:54:27 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 06A21633F96 for ; Fri, 28 May 2010 15:54:27 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o4SIsPXc004338; Fri, 28 May 2010 14:54:25 -0400 (EDT) To: Thom Brown cc: pgsql-performance Subject: Re: Wildly inaccurate query plan In-reply-to: References: Comments: In-reply-to Thom Brown message dated "Fri, 28 May 2010 19:27:08 +0100" Date: Fri, 28 May 2010 14:54:25 -0400 Message-ID: <4337.1275072865@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201005/256 X-Sequence-Number: 38840 Thom Brown writes: > I get this: > Limit (cost=0.00..316895.11 rows=400 width=211) (actual > time=3.880..1368.936 rows=400 loops=1) > -> GroupAggregate (cost=0.00..41843621.95 rows=52817 width=211) > (actual time=3.872..1367.048 rows=400 loops=1) > -> Index Scan using "binaryID_2576_idx" on parts_2576 > (cost=0.00..41683754.21 rows=10578624 width=211) (actual > time=0.284..130.756 rows=19954 loops=1) > Index Cond: (("binaryID")::text > > '1082fa89fe499741b8271f9c92136f44'::text) > Total runtime: 1370.140 ms > The first thing which strikes me is how the GroupAggregate step shows > it got the 400 rows which matches the limit, but it estimated 52,817 > rows. Shouldn't it have already known it would be 400? No. Rowcount estimates are always in terms of what the node would emit if allowed to run to completion. Likewise cost. In this case both the indexscan and the groupagg are terminated early once they satisfy the limit. The planner is expecting this which is why the estimated cost for the limit node is way less than those for its inputs. That looks like a perfectly reasonable plan from here, though it would probably not get chosen with a larger limit or no limit at all, since the ultimate costs are pretty large. Essentially this is a fast-start plan rather than a lowest-total-cost plan, and that looks like the best bet for a small limit value. regards, tom lane