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

From Tom Lane
Subject Re: sub-query optimization
Date
Msg-id 11481.1045260507@sss.pgh.pa.us
Whole thread Raw
In response to sub-query optimization  (Brad Hilton <bhilton@vpop.net>)
Responses Re: sub-query optimization  (Brad Hilton <bhilton@vpop.net>)
List pgsql-sql
Brad Hilton <bhilton@vpop.net> writes:
> 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
>        )

> ... If I modify the query slightly:

> --------
> select 1 from article_categories
>   -->
> select 1 from articles, article_categories
> ---------

> the query takes 98 msec.

Yeah, because then the sub-query is a constant (it doesn't depend on the
current outer row at all) and so it is only evaluated once, not once per
outer row.  Unfortunately, that approach probably gives the wrong
answers...
        regards, tom lane


pgsql-sql by date:

Previous
From: Robert Treat
Date:
Subject: Re: postmaster -i & establishes three connections why?
Next
From: Brad Hilton
Date:
Subject: Re: sub-query optimization