Re: sub-query optimization

From: "Tomasz Myrta" <jasiek(at)klaster(dot)net>
To: Brad Hilton <bhilton(at)vpop(dot)net>, jasiek(at)serwer(dot)skawsoft(dot)com(dot)pl
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: sub-query optimization
Date: 2003-02-14 12:59:22
Message-ID: 20030214205922.M13630@klaster.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Brad Hilton wrote:
<cut>
> 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'

According to your table definition I can say, that you don't need subselect
and exists, because 1 row from article and 1 row from categories have only 1
hit row in articles_categories (primary key), so you can rewrite your query
as simple joins:
(Query is only a hint, it probably won't work)

select a.*
from
categories_c cross join category_map m
join articles a on (child_id=category_id)
join articles_categories ac using (article_id,category_id)
where
m.parent_id=1 and not c.restrict_views;
and a.post_status='publish'

You can change join order depending on your table stats.

Regards,
Tomasz Myrta

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Achilleus Mantzios 2003-02-14 13:17:31 Re: Passing arrays
Previous Message Bruce Momjian 2003-02-14 12:38:48 Re: Extending Datatype