Re: Improving a simple query?

From: Hannu Krosing <hannu(at)tm(dot)ee>
To: Steve Wampler <swampler(at)noao(dot)edu>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Improving a simple query?
Date: 2003-07-13 23:58:28
Message-ID: 1058140707.19113.0.camel@fuji.krosing.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Steve Wampler kirjutas P, 13.07.2003 kell 23:46:
> On Sun, Jul 13, 2003 at 08:09:17PM +0100, Richard Huxton wrote:
> > > I'm not an SQL or PostgreSQL expert.
> > >
> > > I'm getting abysmal performance on a nested query and
> > > need some help on finding ways to improve the performance:
> > [snip]
> > > select * from attributes_table where id in (select id from
> > > attributes where (name='obsid') and (value='oid00066'));
> >
> > This is the classic IN problem (much improved in 7.4 dev I believe). The
> > recommended approach is to rewrite the query as an EXISTS form if
> > possible. See the mailing list archives for plenty of examples.
> >
> > Could you not rewrite this as a simple join though?
>
> Hmmm, I don't see how. Then again, I'm pretty much the village
> idiot w.r.t. SQL...
>
> The inner select is locating a set of (2049) ids (actually from
> the same table, since 'attributes' is just a view into
> 'attributes_table'). The outer select is then locating all
> records (~30-40K) that have any of those ids. Is that really
> something a JOIN could be used for?

There may be some subtle differences, but most likely the 'join' form
wis like this:

select at.*
from attributes_table at,
attributes a
where at.id = a.id
and a.name='obsid'
and a.value='oid00066'

--------------
Hannu

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message nolan 2003-07-14 01:23:47 Re: Improving a simple query?
Previous Message Jim C. Nasby 2003-07-13 22:32:14 Re: Pgsql - Red Hat Linux - VS MySQL VS MSSQL