X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 1E00B9F9998 for ; Mon, 19 Jun 2006 14:13:43 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 68367-03 for ; Mon, 19 Jun 2006 14:13:36 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from relais.videotron.ca (relais.videotron.ca [24.201.245.36]) by postgresql.org (Postfix) with ESMTP id 79F509FA5E1 for ; Mon, 19 Jun 2006 14:13:31 -0300 (ADT) Received: from lythanhphu ([66.130.229.163]) by VL-MH-MR002.ip.videotron.ca (Sun Java System Messaging Server 6.2-2.05 (built Apr 28 2005)) with ESMTP id <0J1400MJEAIHSME0@VL-MH-MR002.ip.videotron.ca> for pgsql-sql@postgresql.org; Mon, 19 Jun 2006 13:13:30 -0400 (EDT) Date: Mon, 19 Jun 2006 13:13:27 -0400 From: Daniel CAUNE Subject: Re: keeping last 30 entries of a log table In-reply-to: To: 'Jeff Frost' Cc: pgsql-sql@postgresql.org Message-id: <0J1400MJFAIHSME0@VL-MH-MR002.ip.videotron.ca> MIME-version: 1.0 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 X-Mailer: Microsoft Office Outlook, Build 11.0.6353 Content-type: text/plain; charset=utf-8 Content-transfer-encoding: 7BIT Thread-index: AcaTu/Lm++7LJ9CQRQ25vUOQ0a7RYwABzs8A X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/228 X-Sequence-Number: 25464 > >> insert into log (account_id, message) values (1, 'this is a test); > >> delete from log where account_id = 1 and id not in ( select id from log > >> where account_id = 1 order by timestamp desc limit 30); > >> > >> I'm wondering if there is a more performance oriented method of doing > the > >> delete that I'm not thinking of. > >> > > > > Depending on whether id is a kind of auto-incremented column that never > cycles, I would suggest something like: > > > > DELETE FROM log > > WHERE account_id = 1 > > AND id < ( > > SELECT MIN(id) > > FROM log > > WHERE account_id = 1 > > ORDER BY timestamp DESC > > LIMIT 30); > > > > I think there will be a performance difference with your method when the > number of records to be deleted is huge. > > Thanks Daniel, I'll try and benchmark them both and see if < turns out to > be > faster than NOT IN. I guess there's no way to get around the subselect > though. > Column id should be indexed indeed. Anyway, I'm not sure about any performance improvement using that last method, as the most consuming operation might be the DELETE operation, not really the SELECT operation, when dealing with a huge volume of data. -- Daniel