pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Eckermann <jeff_eckermann@yahoo.com>
To: Dan Langille <dan@langille.org>
To: pgsql-sql@postgresql.org
Subject: Re: DELETE FROM  t WHERE EXISTS
Date: Fri, 28 Feb 2003 11:30:56 -0800 (PST)
Message-ID: <20030228193056.21775.qmail@web20805.mail.yahoo.com> (raw)
In-Reply-To: <3E5F530B.1649.424D34C1@localhost>

--- Dan Langille <dan@langille.org> 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.
> 

Your WHERE clause will never evaluate to true in this
case, because something will always be returned by the
subselect.

> 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?
> -- 

AFAIK joins cannot be used with DELETEs (but see
below), so you are stuck with a subselect.  If you
don't like the performance of the IN, you could do an
EXISTS using a correlated subselect from a subselect,
but that is ugly.  Or you could do it in two steps:

SELECT INTO TEMP sometable *
FROM clp
ORDER BY commit_date
LIMIT 100;

DELETE FROM clp
WHERE commit_date = sometable.commit_date;

That last must be converted into a join clause
somehow, but right now I am too lazy to turn on
logging to find out what :-)

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/



view thread (11+ messages)  latest in thread

Message-ID: <20030228193056.21775.qmail@web20805.mail.yahoo.com>
Permalink:  ../20030228193056.21775.qmail@web20805.mail.yahoo.com/
Also on:    postgresql.org/message-id/20030228193056.21775.qmail@web20805.mail.yahoo.com

 · 

reply

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: jeff_eckermann@yahoo.com, dan@langille.org
  Subject: Re: DELETE FROM  t WHERE EXISTS
  In-Reply-To: <20030228193056.21775.qmail@web20805.mail.yahoo.com>

* 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