Re: eliminating records not in (select id ... so SLOW?

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Ivan Sergio Borgonovo" <mail(at)webthatworks(dot)it>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: eliminating records not in (select id ... so SLOW?
Date: 2008-08-01 11:23:41
Message-ID: df6405c9-c07e-4a72-ba4c-caa66b1756d1@mm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ivan Sergio Borgonovo wrote:

> But what if I *really* had to execute that query?
> Any other magic I could play to speed it up?

A trick that is sometimes spectacularly efficient is to rewrite the
query to use an outer join instead of NOT IN.

Try:

DELETE FROM table1 WHERE id IN
(SELECT table1.id FROM table1 LEFT JOIN table2 ON table1.id=table2.id
WHERE table2.id IS NULL)

Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage:
http://www.manitou-mail.org

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Marc Cuypers 2008-08-01 11:45:20 Re: Postgresql not using an index
Previous Message Ivan Sergio Borgonovo 2008-08-01 10:47:58 function definition and "entity"