Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 4C2BE9FB82C for ; Fri, 23 Mar 2007 19:13:59 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 85606-02 for ; Fri, 23 Mar 2007 19:13:55 -0300 (ADT) 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 8FDDC9FB62C for ; Fri, 23 Mar 2007 19:13:55 -0300 (ADT) 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 l2NMDqJu025686; Fri, 23 Mar 2007 18:13:52 -0400 (EDT) To: "Noah M. Daniels" cc: pgsql-performance@postgresql.org Subject: Re: Strange left outer join performance issue In-reply-to: <5335C295-4D3E-4A1B-A270-6B822377DD25@mac.com> References: <5335C295-4D3E-4A1B-A270-6B822377DD25@mac.com> Comments: In-reply-to "Noah M. Daniels" message dated "Fri, 23 Mar 2007 15:44:44 -0400" Date: Fri, 23 Mar 2007 18:13:52 -0400 Message-ID: <25685.1174688032@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/378 X-Sequence-Number: 23783 "Noah M. Daniels" writes: > I have two queries that are very similar, that run on the same table > with slightly different conditions. However, despite a similar number > of rows returned, the query planner is insisting on a different > ordering and different join algorithm, causing a huge performance > hit. I'm not sure why the planner is doing the merge join the way it > is in the slow case, rather than following a similar plan to the fast > case. It likes the merge join because it predicts (apparently correctly) that only about 1/14th of the table will need to be scanned. This'd be an artifact of the relative ranges of supplier ids in the two tables. What PG version is this? 8.2 understands about repeated indexscans being cheaper than standalone ones, but I get the impression from the explain estimates that you may be using something older that's overestimating the cost of the nestloop way. regards, tom lane