From: Oskar Liljeblad <osk@hem.passagen.se>
To: Moray McConnachie <moray.mcconnachie@computing-services.oxford.ac.uk>
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] query with subquery abnormally slow?
Date: Mon, 1 Nov 1999 21:36:53 +0100
Message-ID: <19991101213653.A8213@oskar> (raw)
In-Reply-To: <00a001bf24a3$96a18460$01c8a8c0@malthouse.private.net>
References: <19991101194707.A7034@oskar>
<00a001bf24a3$96a18460$01c8a8c0@malthouse.private.net>
On Mon, Nov 01, 1999 at 07:59:20PM -0000, Moray McConnachie wrote:
> > select *
> > from items
> > where package in
> > (select package
> > from items
> > where ...blah...
> > group by package)
>
> Can't see why you don't rewrite this as one query:
>
> select * from items where ... blah ... order by package;
> (is it aggregates in the where clause?)
The items table consists of some columns `performer', `title',
and `package'. Each row corresponds to a song (or similar),
and a `package' is the album (or similar) the song is on.
(This design could certainly be improved with multiple tables,
but it wasn't created that way.)
My query is supposed to list all songs on all albums (packages)
which has at least one song by a specific artist (performer).
("Can you show me some stats on the albums which artist X
has participated in?".)
> Assuming you do need to do it the way you have done it ,
...
> should do it. itemid should be replaced by whatever the primary key of the
> items table is. Note that in blah, fields must be referred to as
> itemcopy.field1,itemcopy.field2, etc.
This query works:
select *
from items i1
where exists
(select package
from items i2
where i2.performer ~ '...'
and i1.package = i2.package
group by i2.package)
However this was also very slow (>30 seconds). When I replaced
"and i1.package = i2.package" with "and i1.id = i2.id" only some
songs from the matching albums were returned. (The songs which
matched performer ~ '...'.)
Oskar Liljeblad (osk@hem.passagen.se)
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: osk@hem.passagen.se, moray.mcconnachie@computing-services.oxford.ac.uk
Subject: Re: [SQL] query with subquery abnormally slow?
In-Reply-To: <19991101213653.A8213@oskar>
References: <19991101194707.A7034@oskar> <00a001bf24a3$96a18460$01c8a8c0@malthouse.private.net> <19991101213653.A8213@oskar>
* 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