X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9A1C4475957 for ; Fri, 14 Feb 2003 14:29:33 -0500 (EST) Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by postgresql.org (Postfix) with ESMTP id 0B3E54758E6 for ; Fri, 14 Feb 2003 14:29:33 -0500 (EST) Received: by serwer.skawsoft.com.pl (Postfix, from userid 1000) id 31B612B84B; Fri, 14 Feb 2003 20:21:13 +0100 (CET) Date: Fri, 14 Feb 2003 20:21:13 +0100 To: Brad Hilton Cc: pgsql-sql@postgresql.org Subject: Re: sub-query optimization Message-ID: <20030214192113.GA26987@serwer> References: <1045246939.29966.22.camel@aragorn.vpop.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1045246939.29966.22.camel@aragorn.vpop.net> User-Agent: Mutt/1.3.28i From: jasiek@serwer.skawsoft.com.pl X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/374 X-Sequence-Number: 11772 On Fri, Feb 14, 2003 at 10:22:19AM -0800, Brad Hilton wrote: > Hello, > > I am hoping someone can help explain why modifying the following query > can effect such a huge change in speed. The query is: > > select * from articles > where exists > ( select 1 from article_categories > where > article_categories.article_id = articles.id and > article_categories.category_id is null > ) Can you test these two queries? select * from (select article_id from article_categories where category_id is null group by article_id) X join articles using (article_id); select from article_categories join articles using (article_id) where category_id is null group by Above queries will need index on article_id I'm not sure if it helps, but they are the only solutions in my mind ;-) Can you say anything about data statistics in your tables? How many rows are with category_id=null? I looked into query definition once again. Your query doesn't make sense - article_categories have not null category_id... What do you really want to do? Regards, Tomasz Myrta