Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 64C22133798E for ; Tue, 9 Nov 2010 19:55:20 -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 25385-02 for ; Tue, 9 Nov 2010 23:55:13 +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 00A581336B42 for ; Tue, 9 Nov 2010 19:55:12 -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 oA9NtCkN020766; Tue, 9 Nov 2010 18:55:12 -0500 (EST) To: bricklen cc: pgsql-performance@postgresql.org Subject: Re: Huge overestimation in rows expected results in bad plan In-reply-to: References: <20388.1289345380@sss.pgh.pa.us> Comments: In-reply-to bricklen message dated "Tue, 09 Nov 2010 15:39:24 -0800" Date: Tue, 09 Nov 2010 18:55:12 -0500 Message-ID: <20765.1289346912@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: 201011/127 X-Sequence-Number: 41151 bricklen writes: > On Tue, Nov 9, 2010 at 3:29 PM, Tom Lane wrote: >> The query doesn't seem to match the plan.  Where is that OR (c.id = >> 38441828354::bigint) condition coming from? > Ah sorry, I was testing it with and without that part. Here is the > corrected query, with that as part of the join condition: > explain analyze > select c.id, c.transactionid, c.clickgenerated, c.confirmed, > c.rejected, cr.rejectedreason > from conversion c > inner join conversionrejected cr on cr.idconversion = c.id or c.id = 38441828354 > where date = '2010-11-06' > and idaction = 12906 > and idaffiliate = 198338 > order by transactionid; Hm. Well, the trouble with that query is that if there is any conversion row with c.id = 38441828354, it will join to *every* row of conversionrejected. The planner not unreasonably assumes there will be at least one such row, so it comes up with a join size estimate that's >= size of conversionrejected; and it also tends to favor a seqscan since it thinks it's going to have to visit every row of conversionrejected anyway. If you have reason to think the c.id = 38441828354 test is usually dead code, you might see if you can get rid of it, or at least rearrange the query as a UNION of two independent joins. regards, tom lane