From: Tomas Berndtsson <tomas@nocrew.org>
To: Laurent Martelli <laurent@aopsys.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: optimizing select ... not in (select ...)
Date: 13 Aug 2001 16:55:22 +0200
Message-ID: <80pua0ypph.fsf@junk.nocrew.org> (raw)
In-Reply-To: <87bslkayl8.fsf@lolo.aopsys>
References: <87bslkayl8.fsf@lolo.aopsys>
Laurent Martelli <laurent@aopsys.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)
Try this instead:
select distinct on (Pictures.PictureID) *
from Pictures
where not exists (select distinct PictureID from Keywords where Pictures.PictureID=Keywords.PictureID);
I've found that it's usually faster, probably because it can use
indices better.
Tomas
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tomas@nocrew.org, laurent@aopsys.com
Subject: Re: optimizing select ... not in (select ...)
In-Reply-To: <80pua0ypph.fsf@junk.nocrew.org>
* 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