Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 754681337B43 for ; Mon, 17 Jan 2011 19:55:58 -0400 (AST) 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 83425-02 for ; Mon, 17 Jan 2011 23:55:51 +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 95CEB13364DE for ; Mon, 17 Jan 2011 19:55:50 -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 p0HNtnu5024113; Mon, 17 Jan 2011 18:55:49 -0500 (EST) To: Tim Uckun cc: Robert Haas , pgsql-bugs@postgresql.org Subject: Re: Problems with adding a is not null to a query. In-reply-to: References: <28545.1295106303@sss.pgh.pa.us> <9026.1295198134@sss.pgh.pa.us> <20744.1295297840@sss.pgh.pa.us> Comments: In-reply-to Tim Uckun message dated "Tue, 18 Jan 2011 11:14:06 +1300" Date: Mon, 17 Jan 2011 18:55:49 -0500 Message-ID: <24112.1295308549@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201101/129 X-Sequence-Number: 29151 Tim Uckun writes: >> Am I right in guessing that pg_stats.n_distinct is much too low for >> the domain_id column? > the domain_id is in the topical urls. A select count of domains shows > that there are 700 domains, the pg_stats shows 170 which seems kind of > low but maybe is not out of bounds because most domains are probably > inactive. It sounds like you've got one of those long-tail distributions where there are lots and lots of the first couple hundred domains, and not many at all of the last few. The problem with the stats as you've got them is that the planner isn't aware of the long tail, so for this specific domain id that's not even there at all, you're getting an estimate of a couple of thousand matches --- which is why it goes for the indexscan-according-to-ORDER-BY plan. It's figuring it will hit one of those matches and be able to end the scan after reading much less than all of the table. As I've stated repeatedly, your next move needs to be to increase the stats target, at least for that column if not globally. You probably don't need to have it know about every last domain id, but you need to have it know about enough that it realizes that domains not included in the MCV list are going to appear less than a couple of thousand times. regards, tom lane