Re: DELETE FROM takes forever

From: Samuel Gendler <sgendler(at)ideasculptor(dot)com>
To: Josh <slushie(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: DELETE FROM takes forever
Date: 2011-02-10 18:37:24
Message-ID: AANLkTikZ40i2vohVASy8ZiHVPVue+FLRSvMJAd+cu3w0@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, Feb 10, 2011 at 9:57 AM, Josh <slushie(at)gmail(dot)com> wrote:

> Hi
>
> I'm trying to do a DELETE FROM on my large table (about 800 million
> rows) based on the contents of another, moderately large table (about
> 110 million rows). The command I'm using is:
>
> DELETE FROM records WHERE id NOT IN (SELECT id FROM unique_records);
>
> This process ran for about two weeks before I decided to stop it -- it
> was dragging down the DB server. I can understand long-running
> processes, but two weeks seems a bit much even for a big table.
>
> Is this the best way to approach the problem? Is there a better way?
>

You need

delete from records r where not exists (select 1 from unique_records ur
where ur.id = r.id);

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2011-02-10 18:44:18 Re: DELETE FROM takes forever
Previous Message Piotr Czekalski 2011-02-10 18:36:51 Re: DELETE FROM takes forever