Re: [SQL] query with subquery abnormally slow? - Mailing list pgsql-sql

From Moray McConnachie
Subject Re: [SQL] query with subquery abnormally slow?
Date
Msg-id 00a001bf24a3$96a18460$01c8a8c0@malthouse.private.net
Whole thread Raw
In response to query with subquery abnormally slow?  (Oskar Liljeblad <osk@hem.passagen.se>)
Responses Re: [SQL] query with subquery abnormally slow?  (Oskar Liljeblad <osk@hem.passagen.se>)
List 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



pgsql-sql by date:

Previous
From: Oskar Liljeblad
Date:
Subject: Re: [SQL] query with subquery abnormally slow?
Next
From: Oskar Liljeblad
Date:
Subject: Re: [SQL] query with subquery abnormally slow?