X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CEF2E5E46C8 for ; Sun, 15 Aug 2004 00:55:04 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 68017-05 for ; Sun, 15 Aug 2004 03:55:08 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 5BAB95E46CD for ; Sun, 15 Aug 2004 00:54:59 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i7F3t3LM015918; Sat, 14 Aug 2004 23:55:04 -0400 (EDT) To: Martin Foster Cc: pgsql-performance@postgresql.org Subject: Re: Faster with a sub-query then without In-reply-to: <4oATc.14571$S55.11652@clgrps12> References: <4oATc.14571$S55.11652@clgrps12> Comments: In-reply-to Martin Foster message dated "Sun, 15 Aug 2004 03:03:28 +0000" Date: Sat, 14 Aug 2004 23:55:03 -0400 Message-ID: <15917.1092542103@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/235 X-Sequence-Number: 7871 Martin Foster writes: > The one not using sub-queries under EXPLAIN ANALYZE proves itself to be > less efficient and have a far higher cost then those with the penalty of > a sub-query. Since this seems to be counter to what I have been told > in the past, I thought I would bring this forward and get some > enlightenment. The ones with the subqueries are not having to form the full join of W and G; they just pick a few rows out of G and look up the matching W rows. The "subquery penalty" is nonexistent in this case because the subqueries are not dependent on any variables from the outer query, and so they need be evaluated only once, rather than once per outer-query row which is what I suppose you were expecting. This is reflected in the EXPLAIN output: notice they are shown as InitPlans not SubPlans. The outputs of the InitPlans are essentially treated as constants (shown as $0 in the EXPLAIN output) and the outer plan is approximately what it would be if you'd written WHERE g.field = 'constant' instead of WHERE g.field = (select ...) regards, tom lane