Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 6C4E89FBEA0 for ; Thu, 18 Oct 2007 06:29:39 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 79503-04 for ; Thu, 18 Oct 2007 06:29:33 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by postgresql.org (Postfix) with ESMTP id 657BD9FBE89 for ; Thu, 18 Oct 2007 06:29:32 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1IiRh0-0003C9-DN for pgsql-performance@postgresql.org; Thu, 18 Oct 2007 09:29:26 +0000 Received: from cpe.atm2-0-1261189.0x503fa7b2.bynxx8.customer.tele.dk ([80.63.167.178]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 18 Oct 2007 09:29:26 +0000 Received: from nis by cpe.atm2-0-1261189.0x503fa7b2.bynxx8.customer.tele.dk with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 18 Oct 2007 09:29:26 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-performance@postgresql.org From: =?ISO-8859-1?Q?Nis_J=F8rgensen?= Subject: Re: Incorrect estimates on columns Date: Thu, 18 Oct 2007 11:29:08 +0200 Lines: 32 Message-ID: References: <200710171334.24526.chris.kratz@vistashare.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: cpe.atm2-0-1261189.0x503fa7b2.bynxx8.customer.tele.dk User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) In-Reply-To: <200710171334.24526.chris.kratz@vistashare.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/218 X-Sequence-Number: 27240 Chris Kratz skrev: > Hello Everyone, > > I'm struggling to get postgres to run a particular query quickly. It > seems that very early on, the planner seems to mis-estimate the > number of rows returned by a join which causes it to assume that > there is only 1 row as it goes up the tree. It then picks a nested > loop join which seems to cause the whole query to be slow. Or at > least if I turn off nestloop, it runs in 216ms. > > explain analyze SELECT 1 FROM rpt_agencyquestioncache_171_0 par right > outer join namemaster dem on (par.nameid = dem.nameid and > dem.programid = 171) right join activity_parentid_view ses on > (par.activity = ses.activityid and ses.programid=171) left join ( > select ct0.inter_agency_id,ct0.nameid from > rpt_agencyquestioncache_171_0 ct0 join rpt_agencyquestioncache_171_2 > ct2 on ct2.participantid =ct0.participantid ) as par30232 on > (dem.nameid=par30232.nameid and par30232.inter_agency_id=30232) WHERE > ( ( (par.provider_lfm) ='Child Guidance Treatment Centers Inc.')) The first two join-conditions seem strange - I think those are the cause of the performance problems. The result of the first join, for instance, is the return of all rows from dem, and matching rows from par IFF dem.program_id =171 (NULLS otherwise). In fact, since you are using a condition on the par table, you could just as well use inner joins for the first two cases. Hope this helps, Nis