Re: sub-query optimization - Mailing list pgsql-sql

From Brad Hilton
Subject Re: sub-query optimization
Date
Msg-id 1045251747.29974.45.camel@aragorn.vpop.net
Whole thread Raw
In response to sub-query optimization  (Brad Hilton <bhilton@vpop.net>)
Responses Re: sub-query optimization  ("Tomasz Myrta" <jasiek@klaster.net>)
List pgsql-sql
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.idand  category_map.parent_id = 1 and  category_map.child_id = categories.id and  category_map.child_id =
article_categories.category_idand  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


pgsql-sql by date:

Previous
From: "Tomasz Myrta"
Date:
Subject: Re: sub-query optimization
Next
From: Stephan Szabo
Date:
Subject: Re: sub-query optimization