Re: DELETE FROM t WHERE EXISTS

From: Dmitry Tkach <dmitry(at)openratings(dot)com>
To: Dan Langille <dan(at)langille(dot)org>
Subject: Re: DELETE FROM t WHERE EXISTS
Date: 2003-02-28 22:47:04
Message-ID: 3E5FE6E8.9020200@openratings.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

What about

select * into temp rows_to_keep from clp order by commit_date limit 100;
truncate clp;
insert into clp select * from rows_to_keep;

Dima.

Dan Langille wrote:
> Hi folks,
>
> I wanted to delete "old" rows from a table. These are the rows I
> want to keep:
>
> SELECT *
> FROM clp
> ORDER BY commit_date
> LIMIT 100
>
> So I tried this:
>
> DELETE FROM clp
> WHERE NOT EXISTS (
> SELECT *
> FROM clp
> ORDER BY commit_date
> LIMIT 100);
>
> Uhh uhh, nothing deleted. I don't understand why.
>
> OK, I can do this instead:
>
> DELETE from clp
> where commit_log_id NOT in (
> SELECT commit_log_id
> FROM clp
> ORDER BY commit_date
> LIMIT 100);
>
> Can you think of a better way?

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Dan Langille 2003-03-01 01:12:48 Re: Optimizing
Previous Message Dmitry Tkach 2003-02-28 22:42:00 Re: disable constraints