X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C0E8B475957 for ; Fri, 14 Feb 2003 14:41:09 -0500 (EST) Received: from aragorn.vpop.net (c5cust187.starstream.net [206.170.161.187]) by postgresql.org (Postfix) with ESMTP id 3E8464758E6 for ; Fri, 14 Feb 2003 14:41:09 -0500 (EST) Received: from bhilton by aragorn.vpop.net with local (Exim 3.36 #1 (Debian)) id 18jlj9-0007Lz-00; Fri, 14 Feb 2003 11:42:27 -0800 Subject: Re: sub-query optimization From: Brad Hilton To: jasiek@serwer.skawsoft.com.pl Cc: pgsql-sql@postgresql.org In-Reply-To: <20030214192113.GA26987@serwer> References: <1045246939.29966.22.camel@aragorn.vpop.net> <20030214192113.GA26987@serwer> Content-Type: text/plain Content-Transfer-Encoding: 7bit Organization: Message-Id: <1045251747.29974.45.camel@aragorn.vpop.net> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 Date: 14 Feb 2003 11:42:27 -0800 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/345 X-Sequence-Number: 11743 On Fri, 2003-02-14 at 11:21, jasiek@serwer.skawsoft.com.pl wrote: > Can you test these two queries? Thanks, I'll test them shortly. I wanted to answer your other questions, first: > 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? Sorry to cause confusion. My original query and db format were fairly complex so I didn't want to distract from my problem. My actual query looks like: select * from articles where exists (select 1 from article_categories, categories, category_map where article_categories.article_id = articles.id and categories.restrict_views = FALSE and article_categories.category_id = categories.id and category_map.parent_id = 1 and category_map.child_id = categories.id and category_map.child_id = article_categories.category_id and articles.post_status = 'publish' ) and post_status = 'publish' ------------------ The problem is that sometimes there are no categories with "restrict_views = FALSE" and the query takes a *long* time: 23 seconds. However, if I simply add the 'articles' table to the inner query it takes 0.23 msec. *But*, sometimes there are many categories where "restrict_views = FALSE", and in such a case adding the 'articles' table to the inner query actually hurts performance quite a bit. Does that help at all? Thanks, -Brad