X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 6C550475A45 for ; Sat, 15 Feb 2003 03:53:26 -0500 (EST) Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by postgresql.org (Postfix) with ESMTP id B86804759AF for ; Sat, 15 Feb 2003 03:53:25 -0500 (EST) Received: by serwer.skawsoft.com.pl (Postfix, from userid 1000) id 5150B2B86F; Sat, 15 Feb 2003 09:45:00 +0100 (CET) Date: Sat, 15 Feb 2003 09:45:00 +0100 To: Brad Hilton Cc: Tomasz Myrta , jasiek@serwer.skawsoft.com.pl, pgsql-sql@postgresql.org Subject: Re: sub-query optimization Message-ID: <20030215084500.GA30253@serwer> References: <1045246939.29966.22.camel@aragorn.vpop.net> <20030214192113.GA26987@serwer> <1045251747.29974.45.camel@aragorn.vpop.net> <20030214205922.M13630@klaster.net> <1045262371.29955.124.camel@aragorn.vpop.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1045262371.29955.124.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/382 X-Sequence-Number: 11779 On Fri, Feb 14, 2003 at 02:39:31PM -0800, Brad Hilton wrote: > If I just utilize article_categories primary key, I could end up with > duplicate articles since articles can live in multiple categories. > In case I'm not understanding your suggestiong perfectly, I tried to > flesh it out a bit more. Does the following query match your > suggestion? It looks ok now. Probably it needs some cosmetics changes. > > select a.* > from > categories c cross join category_map m > join article_categories ac on (c.id = ac.category_id and m.child_id = > ac.category_id) > join articles a on (a.id = ac.article_id) > where > m.parent_id=1 and > not c.restrict_views and > m.child_id = c.id and > a.post_status='publish' > > Unfortunately, this query returns duplicate articles (see explanation > above), and is fairly slow. Maybe I didn't follow your initial query > properly. What about adding "group by a.field1,a.field2..."? It will eliminate duplicates. Can you send explain analyze this query? Maybe table joins should be reordered or they need other indexes they have? Tomasz