Re: need some help understanding sloq query

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Esger Abbink <pgsql(at)bumblebeast(dot)com>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: need some help understanding sloq query
Date: 2001-12-06 16:25:11
Message-ID: 20011206081928.G25449-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 6 Dec 2001, Esger Abbink wrote:

> > Esger,
> >
> > > select some_other_fields from ents e, qtys q where e.set_id =
> > > q.set_id and
> > > e.e_id = q.e_id and e.set_id in (select set_id from datasets
> > > where update_id in (select cur_update_id from current)) and
> > > q.other_field = some_const ;
> > >
> > > this query takes ages :(
> >
> > I'm not surprised. You're doing a nested subselect with the IN
> > operator; frankly, you couldn't make a relatively simple query any
> > slower than the above. It looks like the query parser is doing its best
> > to optimize, but you've forced it to compare every row in ents JOIN qtys
> > against the whole datasets table. What's wrong with:
> >
> > SELECT some_fields
> > FROM ents JOIN qtys USING (set_id)
> > JOIN datasets USING (set_id)
> > JOIN current ON datasets.update_id = current.cur_update_id
> > WHERE other_field = some_constant;
> >
>
> i figured i wasnt doing things "right" ;)
>
> unfortunately the query above takes down my postgres (7.0.3 tried on 2
> servers). 1 join no problem, 2 joins results in:

If you want to use JOIN, you'll need to upgrade. IIRC, 7.0.x had bugs that
caused this kind of failure that were fixed for 7.1.

I think the above is equivalent to:
select some_fields from
ents, qtys, datasets, current
where ents.set_id=qtys.set_id and
ents.set_id=datasets.set_id and
datasets.update_id = current.cur_update_id and
other_field = some_constant;

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2001-12-06 16:31:09 Re: how to chane the type
Previous Message Roberto Mello 2001-12-06 15:48:57 Re: Inline IF condition