Re: sub-query optimization

From: Brad Hilton <bhilton(at)vpop(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: sub-query optimization
Date: 2003-02-14 18:38:51
Message-ID: 1045247931.29968.28.camel@aragorn.vpop.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, 2003-02-14 at 10:22, Brad Hilton wrote:
> 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
> )

To add one more detail: in simplifying my query for the list, I should
have said:

article_categories.category_id = 0

instead of

article_categories.category_id is NULL

Then, with an index on article_categories (category_id) you get the
following results for the two queries:

Without adding the "articles" table to the inner query: 1329 msec
With the "articles" table in the inner query: 0.28 msec!

That highlights the difference a bit more dramatically.

Any ideas?

Thanks,
-Brad

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message greg 2003-02-14 18:41:10 Re: Drop temporary table only if it exists
Previous Message Brad Hilton 2003-02-14 18:22:19 sub-query optimization