Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 9FF229FA4A5 for ; Tue, 5 Dec 2006 17:21:57 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 84900-06 for ; Tue, 5 Dec 2006 17:21:40 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from itspmx2.state.ms.us (itspmx2.state.ms.us [205.144.224.112]) by postgresql.org (Postfix) with ESMTP id 894C59FA173 for ; Tue, 5 Dec 2006 17:21:48 -0400 (AST) Received: from rray.drdc.mstc.ms.gov ([10.3.192.2]) by itspmx2.state.ms.us (8.13.4/8.13.4) with ESMTP id kB5LLfuT024055 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 5 Dec 2006 15:21:47 -0600 (CST) Received: from rray.drdc.mstc.ms.gov (localhost.localdomain [127.0.0.1]) by rray.drdc.mstc.ms.gov (8.13.6/8.13.6) with ESMTP id kB5LLfPL007061 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 5 Dec 2006 15:21:41 -0600 Received: from localhost (rray@localhost) by rray.drdc.mstc.ms.gov (8.13.6/8.13.6/Submit) with ESMTP id kB5LLf3i007058 for ; Tue, 5 Dec 2006 15:21:41 -0600 X-Authentication-Warning: rray.drdc.mstc.ms.gov: rray owned process doing -bs Date: Tue, 5 Dec 2006 15:21:41 -0600 (CST) From: Richard Ray X-X-Sender: rray@rray.drdc.mstc.ms.gov To: pgsql-sql@postgresql.org Subject: Re: Can someone explain the problem with this select In-Reply-To: <11220.1165353263@sss.pgh.pa.us> Message-ID: References: <11220.1165353263@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/61 X-Sequence-Number: 27017 I've been foolin with this for a couple of days Sometimes you just have to ask Thanks gentlemen On Tue, 5 Dec 2006, Tom Lane wrote: > Richard Ray writes: >> dcc=# EXPLAIN ANALYZE select * from documents left outer join comments on >> (documents.doc_num = comments.doc_num) where documents.doc_num in (select >> doc_num from documents limit 10); >> [ is slow ] > > This isn't your fault, it's an optimizer limitation: PG < 8.2 can't > reorder outer joins relative to regular joins, and the IN with a > sub-select is a kind of regular join. So it's forming the whole > outer-join result and then joining to the sub-select :-( > > This is fixed in 8.2, released today, so perhaps upgrading is the > thing for you to do. Alternatively, you can contort the query to > get the IN restriction inside the outer join: > > select * from > (select * from documents > where documents.doc_num in (select doc_num from documents limit 10)) ss > left outer join comments on (ss.doc_num = comments.doc_num); > > regards, tom lane >