Skip site navigation (1) Skip section navigation (2)

Re: DELETE FROM t WHERE EXISTS

From: Dan Langille <dan(at)langille(dot)org>
To: Tomasz Myrta <jasiek(at)klaster(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: DELETE FROM t WHERE EXISTS
Date: 2003-02-28 21:56:56
Message-ID: 20030228165055.J29368@xeon.unixathome.org (view raw or flat)
Thread:
Lists: pgsql-sql
On Fri, 28 Feb 2003, Tomasz Myrta 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?

> delete from clp where commit_date < (select commit_date from clp order
> by commit_date limit 1 offset 100); Regards, Tomasz Myrta

Thank you.  That does it well.  And it also revealed an error in my
original SQL:  a mising "desc".  Here's the plan FYI:


 Seq Scan on clp  (cost=0.00..2.26 rows=34 width=6) (actual
time=8.55..11.92 rows=399 loops=1)
   Filter: (commit_date < $0)
   InitPlan
     ->  Limit  (cost=5.62..5.62 rows=1 width=8) (actual time=7.58..7.60
rows=1 loops=1)
           ->  Sort  (cost=5.37..5.62 rows=101 width=8) (actual
time=6.75..7.17 rows=102 loops=1)
                 Sort Key: commit_date
                 ->  Seq Scan on clp  (cost=0.00..2.01 rows=101 width=8)
(actual time=0.26..3.93 rows=500 loops=1)
 Total runtime: 15.86 msec


In response to

pgsql-sql by date

Next:From: Dan LangilleDate: 2003-02-28 21:58:34
Subject: Re: DELETE FROM t WHERE EXISTS
Previous:From: Robert TreatDate: 2003-02-28 20:36:04
Subject: Re: DELETE FROM t WHERE EXISTS

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group