Re: optimizing select ... not in (select ...)

From: Laurent Martelli <laurent(at)aopsys(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: optimizing select ... not in (select ...)
Date: 2001-08-13 16:35:43
Message-ID: 87zo93apeo.fsf@lolo.aopsys
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>>>>> "Tomas" == Tomas Berndtsson <tomas(at)nocrew(dot)org> writes:

Tomas> Laurent Martelli <laurent(at)aopsys(dot)com> 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(at)aopsys(dot)com http://www.bearteam.org/~laurent/

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh Berkus 2001-08-14 01:34:57 Eh?
Previous Message Bruce Momjian 2001-08-13 15:59:48 Re: optimizing select ... not in (select ...)