Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 7E2BB632FDF for ; Mon, 23 Feb 2009 14:01:54 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 74789-02 for ; Mon, 23 Feb 2009 14:01:51 -0400 (AST) 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 75210632214 for ; Mon, 23 Feb 2009 14:01:51 -0400 (AST) 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 n1NI1omK018726; Mon, 23 Feb 2009 13:01:50 -0500 (EST) To: kouber@saparev.com cc: pgsql-performance@postgresql.org Subject: Re: LIMIT confuses the planner In-reply-to: <49A2DFFA.8000001@saparev.com> References: <49A295F0.5010405@saparev.com> <49A2A450.20002@archonet.com> <49A2DFFA.8000001@saparev.com> Comments: In-reply-to Kouber Saparev message dated "Mon, 23 Feb 2009 19:42:18 +0200" Date: Mon, 23 Feb 2009 13:01:50 -0500 Message-ID: <18725.1235412110@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200902/267 X-Sequence-Number: 32848 Kouber Saparev writes: > Now the planner believes there're 910 rows, which is a bit closer to the > real data: > swing=# select avg(length) from (select username, count(*) as length > from login_attempt group by username) as freq; > avg > ---------------------- > 491.6087310427555479 > (1 row) Hmph, that's still not real good. Ideally it should be estimating *less* than the average frequency, because the estimate is made after excluding all the most-common-values, which evidently 'kouber' is not one of. I suppose there's quite a large number of infrequently-seen usernames and the ndistinct estimate is much less than reality? (Look at the pg_stats row for this column.) It might be worth going all the way to stats target 1000 for this column. regards, tom lane