pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: jasiek@serwer.skawsoft.com.pl
To: Brad Hilton <bhilton@vpop.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: sub-query optimization
Date: Fri, 14 Feb 2003 20:21:13 +0100
Message-ID: <20030214192113.GA26987@serwer> (raw)
In-Reply-To: <1045246939.29966.22.camel@aragorn.vpop.net>
References: <1045246939.29966.22.camel@aragorn.vpop.net>

On Fri, Feb 14, 2003 at 10:22:19AM -0800, 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
>        )

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



view thread (14+ messages)  latest in thread

Message-ID: <20030214192113.GA26987@serwer>
Permalink:  ../20030214192113.GA26987@serwer/
Also on:    postgresql.org/message-id/20030214192113.GA26987@serwer

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jasiek@serwer.skawsoft.com.pl, bhilton@vpop.net
  Subject: Re: sub-query optimization
  In-Reply-To: <20030214192113.GA26987@serwer>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox