Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id CB9529FB3B6 for ; Thu, 12 Oct 2006 01:16:07 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 51458-09 for ; Thu, 12 Oct 2006 04:16:04 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 41F979FB3AF for ; Thu, 12 Oct 2006 01:16:04 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from mail.frii.com (phobos02.frii.net [216.17.128.162]) by svr4.postgresql.org (Postfix) with ESMTP id CB88C5AF8FA for ; Thu, 12 Oct 2006 04:15:59 +0000 (GMT) Received: from kyoto.home (smcgraw.ppp.frii.com [216.17.133.158]) by mail.frii.com (FRII) with ESMTP id B7A11A47AE for ; Wed, 11 Oct 2006 22:15:58 -0600 (MDT) Received: from maxwell (maxwell.home [10.1.65.135]) by kyoto.home (Postfix) with SMTP id 6F2AB18F959 for ; Wed, 11 Oct 2006 22:15:58 -0600 (MDT) From: "Stuart McGraw" To: Subject: Re: slow view Date: Wed, 11 Oct 2006 22:15:58 -0600 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) In-Reply-To: Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.204 tagged_above=0 required=5 tests=FORGED_RCVD_HELO, SPF_NEUTRAL X-Spam-Level: * X-Archive-Number: 200610/113 X-Sequence-Number: 26502 On 2006/10/11 Stuart wrote: > [..] Apologies for following up my own post, but after struggling with that query for over a day, I figured out the answer within thirty minutes of posting. (Sigh) My slow query was: > SELECT p.id AS pid, a.id AS aid, sub.bid AS bid > FROM p > JOIN a ON a.p=p.id > LEFT JOIN ( > SELECT a.id AS aid, b.id as bid > FROM a > LEFT JOIN b ON b.p=a.p > LEFT JOIN x ON x.a=a.id AND x.b=b.id > WHERE x.a IS NULL > ) AS sub ON sub.aid=a.id > WHERE p.id=1; Including the parent table p in the inner select was all it took. SELECT p.id AS pid, a.id AS aid, sub.bid AS bid FROM p JOIN a ON a.p=p.id LEFT JOIN ( SELECT p.id AS pid, a.id AS aid, b.id as bid FROM p JOIN a ON a.p=p.id LEFT JOIN b ON b.p=a.p LEFT JOIN x ON x.a=a.id AND x.b=b.id WHERE x.a IS NULL ) AS sub ON sub.aid=a.id AND sub.pid=p.id WHERE p.id=1;