Re: sub-query optimization

From: "Tomasz Myrta" <jasiek(at)klaster(dot)net>
To: Brad Hilton <bhilton(at)vpop(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: sub-query optimization
Date: 2003-02-14 12:24:45
Message-ID: 20030214202445.M57741@klaster.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> 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
> )
>

Can you test these two queries?
select * from
(select article_id from article_categories where category_id is null
group by article_id) X
join articles using (article_id);

select <fields> from
article_categories
join articles using (article_id)
where category_id is null
group by <fields>

Above queries will need index on article_id
I'm not sure if it helps, but they are the only solutions in my
mind ;-)

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?

Regards,
Tomasz Myrta

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bruce Momjian 2003-02-14 12:38:48 Re: Extending Datatype
Previous Message Achilleus Mantzios 2003-02-14 12:10:09 Re: [SQL] Passing arrays