X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 28E5F475B8A for ; Fri, 14 Feb 2003 13:37:29 -0500 (EST) Received: from aragorn.vpop.net (c5cust187.starstream.net [206.170.161.187]) by postgresql.org (Postfix) with ESMTP id C0249475B8F for ; Fri, 14 Feb 2003 13:37:27 -0500 (EST) Received: from bhilton by aragorn.vpop.net with local (Exim 3.36 #1 (Debian)) id 18jkjc-0003Pi-00 for ; Fri, 14 Feb 2003 10:38:52 -0800 Subject: Re: sub-query optimization From: Brad Hilton To: pgsql-sql@postgresql.org In-Reply-To: <1045246939.29966.22.camel@aragorn.vpop.net> References: <1045246939.29966.22.camel@aragorn.vpop.net> Content-Type: text/plain Content-Transfer-Encoding: 7bit Organization: Message-Id: <1045247931.29968.28.camel@aragorn.vpop.net> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 Date: 14 Feb 2003 10:38:51 -0800 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/342 X-Sequence-Number: 11740 On Fri, 2003-02-14 at 10:22, 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 > ) To add one more detail: in simplifying my query for the list, I should have said: article_categories.category_id = 0 instead of article_categories.category_id is NULL Then, with an index on article_categories (category_id) you get the following results for the two queries: Without adding the "articles" table to the inner query: 1329 msec With the "articles" table in the inner query: 0.28 msec! That highlights the difference a bit more dramatically. Any ideas? Thanks, -Brad