Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id C21199FA9BD for ; Sun, 22 Apr 2007 23:14:27 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 97330-10 for ; Sun, 22 Apr 2007 23:14:18 -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 D01E29FA6AC for ; Sun, 22 Apr 2007 23:14:24 -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 l3N2ELXZ012852; Sun, 22 Apr 2007 22:14:21 -0400 (EDT) To: "henk de wit" cc: pgsql-performance@postgresql.org Subject: Re: Redundant sub query triggers slow nested loop left join In-reply-to: References: Comments: In-reply-to "henk de wit" message dated "Mon, 23 Apr 2007 00:39:04 +0200" Date: Sun, 22 Apr 2007 22:14:21 -0400 Message-ID: <12851.1177294461@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/375 X-Sequence-Number: 24252 "henk de wit" writes: >> --- the thing is evidently assuming >> that only about 1 in 200 rows have status = '0', which might be accurate >> as a global average but not for this particular merchant. What exactly >> is the relationship between status and merchant_id, anyway? > The meaning is that a "banners_link" belongs to a merchant with the id > merchant_id. A "banners_link" can be disabled (status 1) or enabled (status > 0). Globally about 1/3 of the banners_links have status 0 and 2/3 have > status 1. The 1 in 200 estimate is indeed way off. Well, that's darn odd. It should not be getting that so far wrong. What's the datatype of the status column exactly (I'm guessing varchar but maybe not)? Would you show us the pg_stats row for the status column? > One interesting other thing to note; if I remove the banners_links.status = > 0 condition from the query altogether the execution times improve > dramatically again. Right, because it was dead on about how many merchant_id = 5631 rows there are. The estimation error is creeping in where it guesses how selective the status filter is. It should be using the global fraction of status = 0 rows for that, but it seems to be using a default estimate instead (1/200 is actually the default eqsel estimate now that I think about it). I'm not sure why, but I think it must have something to do with the subquery structure of your query. Were you showing us the whole truth about your query, or were there details you left out? regards, tom lane