Re: [SQL] query with subquery abnormally slow?

From: "Moray McConnachie" <moray(dot)mcconnachie(at)computing-services(dot)oxford(dot)ac(dot)uk>
To: "Oskar Liljeblad" <osk(at)hem(dot)passagen(dot)se>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: [SQL] query with subquery abnormally slow?
Date: 1999-11-01 19:59:20
Message-ID: 00a001bf24a3$96a18460$01c8a8c0@malthouse.private.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> 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?)

Assuming you do need to do it the way you have done it ,

SELECT * FROM items WHERE NOT EXISTS
(SELECT package FROM items itemscopy WHERE ... blah ... AND
itemscopy.itemid=items.itemid GROUP BY package);

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.

Yours,
Moray McConnachie

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Oskar Liljeblad 1999-11-01 20:10:30 Re: [SQL] query with subquery abnormally slow?
Previous Message Oskar Liljeblad 1999-11-01 19:05:28 Re: [SQL] query with subquery abnormally slow?