Re: sub-query optimization

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Brad Hilton <bhilton(at)vpop(dot)net>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: sub-query optimization
Date: 2003-02-14 20:04:45
Message-ID: 20030214115945.V64558-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On 14 Feb 2003, Brad Hilton wrote:

> 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
> )
>
> The original query was much more complex, but I have trimmed it down to
> highlight the problem. The query above also manifests the problem. OK,
> the above query (with 100,000 records in the articles table) takes 1292
> msec (see output below). If I modify the query slightly:
>
> --------
> select 1 from article_categories
> -->
> select 1 from articles, article_categories
> ---------

After putting the latter in the subselect do you actually have the same
query? In one case articles is an outer reference for the particular
row. In the other it's a reference to the copy of articles in the
subselect. Wouldn't that give the wrong results when you have any matches
(since there'd exist a row from the subselect even if it wasn't the one
matching the outer query)?

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Eduardo 2003-02-14 20:57:44 Re: PL/PGSQL EDITOR
Previous Message Brad Hilton 2003-02-14 19:42:27 Re: sub-query optimization