Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 7BF7D2E0D34 for ; Thu, 24 Jan 2008 23:21:39 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 28761-04-6 for ; Thu, 24 Jan 2008 23:21:34 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 93ED92E565C for ; Thu, 24 Jan 2008 22:27:04 -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 m0P2R1iY021625; Thu, 24 Jan 2008 21:27:01 -0500 (EST) To: "Dmitry Potapov" cc: "Guillaume Smet" , pgsql-performance@postgresql.org Subject: Re: planner chooses unoptimal plan on joins with complex key In-reply-to: <878c83960801240803i3d94efc5n34160559bbd91aad@mail.gmail.com> References: <878c83960801230548i6bee9353m19bcee7335220c9b@mail.gmail.com> <1d4e0c10801230558i1e56a9dcgaa9e6c2fac6d0448@mail.gmail.com> <22362.1201108676@sss.pgh.pa.us> <878c83960801240803i3d94efc5n34160559bbd91aad@mail.gmail.com> Comments: In-reply-to "Dmitry Potapov" message dated "Thu, 24 Jan 2008 19:03:56 +0300" Date: Thu, 24 Jan 2008 21:27:01 -0500 Message-ID: <21624.1201228021@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/226 X-Sequence-Number: 28465 "Dmitry Potapov" writes: > Sorry, it was just EXPLAIN. I can't run EXPLAIN ANALYZE on that > (production) server, so I uploaded 3days old backup to a spare box and > here's what I've got: > -> Merge Join (cost=0.00..4955790.28 rows=1 width=59) > (actual time=0.048..4575782.472 rows=30805113 loops=1) > Merge Cond: ((t1.m1 = t2.m1) AND (t1.m2 = t2.m2) AND > (t1.m3 = t2.m3)) Well, there's our problem: an estimate of 1 row for a join that's actually 30805113 rows is uncool :-(. It's hard to tell whether the planner is just being overoptimistic about the results of ANDing the three join conditions, or if one or more of the basic condition selectivities were misestimated. Could you try explain analyze select 1 from t1, t2 where t1.m1 = t2.m1; explain analyze select 1 from t1, t2 where t1.m2 = t2.m2; explain analyze select 1 from t1, t2 where t1.m3 = t2.m3; and show the results? This will probably be slow too, but we don't care --- we just need to see the estimated and actual rowcounts. regards, tom lane