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 8D1189FA5E1 for ; Mon, 19 Jun 2006 13:16:54 -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 61525-04 for ; Mon, 19 Jun 2006 13:16:45 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from glacier.frostconsultingllc.com (glacier.frostconsultingllc.com [64.127.99.90]) by postgresql.org (Postfix) with ESMTP id 75F769F9998 for ; Mon, 19 Jun 2006 13:16:45 -0300 (ADT) Received: from localhost ([127.0.0.1]:36946) by glacier.frostconsultingllc.com with esmtp (Exim 4.62) (envelope-from ) id 1FsMQd-0002cs-Hf; Mon, 19 Jun 2006 09:16:43 -0700 Date: Mon, 19 Jun 2006 09:16:43 -0700 (PDT) From: Jeff Frost To: Daniel CAUNE cc: pgsql-sql@postgresql.org Subject: Re: keeping last 30 entries of a log table In-Reply-To: <0J10001JS4N1G9G0@VL-MH-MR001.ip.videotron.ca> Message-ID: References: <0J10001JS4N1G9G0@VL-MH-MR001.ip.videotron.ca> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-SA-Exim-Connect-IP: 127.0.0.1 X-SA-Exim-Mail-From: jeff@frostconsultingllc.com X-SA-Exim-Scanned: No (on glacier.frostconsultingllc.com); SAEximRunCond expanded to false X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/227 X-Sequence-Number: 25463 On Sat, 17 Jun 2006, Daniel CAUNE wrote: >> 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. -- Jeff Frost, Owner Frost Consulting, LLC http://www.frostconsultingllc.com/ Phone: 650-780-7908 FAX: 650-649-1954