Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id A73ED9FA162 for ; Thu, 14 Dec 2006 13:48:45 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 48965-06 for ; Thu, 14 Dec 2006 13:48:36 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 8DCF09F9FDA for ; Thu, 14 Dec 2006 13:48:37 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id kBEHma0c022465; Thu, 14 Dec 2006 12:48:36 -0500 (EST) To: "Tim Jones" cc: pgsql-performance@postgresql.org Subject: Re: strange query behavior In-reply-to: <47668A1334CDBF46927C1A0DFEB223D39F7B01@mail.optiosoftware.com> References: <47668A1334CDBF46927C1A0DFEB223D39F79A5@mail.optiosoftware.com> <10648.1166037392@sss.pgh.pa.us> <47668A1334CDBF46927C1A0DFEB223D39F79FA@mail.optiosoftware.com> <12988.1166052295@sss.pgh.pa.us> <47668A1334CDBF46927C1A0DFEB223D39F7B01@mail.optiosoftware.com> Comments: In-reply-to "Tim Jones" message dated "Thu, 14 Dec 2006 09:02:08 -0500" Date: Thu, 14 Dec 2006 12:48:35 -0500 Message-ID: <22464.1166118515@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/381 X-Sequence-Number: 22437 "Tim Jones" writes: > 18,273,008 rows in observationresults > [ and n_distinct = 12942 ] OK, so the estimated rowcounts are coming from those two numbers. It's notoriously hard to get a decent n_distinct estimate from a small sample :-(, and I would imagine the number of batteryidentifiers is really a lot more than 12942? What you need to do is increase the statistics target for observationresults.batteryidentifier (see ALTER TABLE) and re-ANALYZE and see if you get a saner n_distinct in pg_stats. I'd try 100 and then 1000 as target. Or you could just increase the global default target (see postgresql.conf) but that might be overkill. It's still a bit odd that the case with two batteryidentifiers was estimated fairly accurately when the other wasn't; I'll go look into that. But in any case you need better stats if you want good plans. regards, tom lane