Received: from news.tht.net (news.hub.org [216.126.91.242]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f7DH2nP44868 for ; Mon, 13 Aug 2001 13:02:49 -0400 (EDT) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id f7DGZjX11012 for pgsql-sql@postgresql.org; Mon, 13 Aug 2001 12:35:45 -0400 (EDT) (envelope-from news) From: Laurent Martelli X-Newsgroups: comp.databases.postgresql.sql Subject: Re: optimizing select ... not in (select ...) Date: 13 Aug 2001 18:35:43 +0200 Organization: Hub.Org Networking Services (http://www.hub.org) Lines: 27 Message-ID: <87zo93apeo.fsf@lolo.aopsys> References: <80pua0ypph.fsf@junk.nocrew.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: scrappy@hub.org User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 To: pgsql-sql@postgresql.org X-Archive-Number: 200108/170 X-Sequence-Number: 4392 >>>>> "Tomas" == Tomas Berndtsson writes: Tomas> Laurent Martelli writes: >> I have this query : >> >> select distinct on (Pictures.PictureID) * from Pictures where >> Pictures.PictureID not in (select distinct PictureID from >> Keywords); >> >> and I find it a bit slow. Does anybody have suggestions to run >> this faster ? (I have indexes on PictureID on both Pictures and >> Keywords) Tomas> Try this instead: Tomas> select distinct on (Pictures.PictureID) * from Pictures where Tomas> not exists (select distinct PictureID from Keywords where Tomas> Pictures.PictureID=Keywords.PictureID); Tomas> I've found that it's usually faster, probably because it can Tomas> use indices better. It is indeed much faster. Thanks to all of you who answered so fast. -- Laurent Martelli laurent@aopsys.com http://www.bearteam.org/~laurent/