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

From Brad Hilton
Subject Re: sub-query optimization
Date
Msg-id 1045247931.29968.28.camel@aragorn.vpop.net
Whole thread Raw
In response to sub-query optimization  (Brad Hilton <bhilton@vpop.net>)
List 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


pgsql-sql by date:

Previous
From: Brad Hilton
Date:
Subject: sub-query optimization
Next
From: greg@turnstep.com
Date:
Subject: Re: Drop temporary table only if it exists