Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id E2AC02E2DF5 for ; Wed, 9 Jan 2008 14:21:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 15160-10 for ; Wed, 9 Jan 2008 14:21:14 -0400 (AST) Received: from starfish.geekisp.com (mail.geekisp.com [216.168.135.169]) by postgresql.org (Postfix) with ESMTP id 6E39C2E3239 for ; Wed, 9 Jan 2008 14:21:15 -0400 (AST) Received: (qmail 25476 invoked by uid 1003); 9 Jan 2008 18:21:21 -0000 Received: from mobile-032-161-217-138.mycingular.net (HELO dino-1.misuse.org) (public@misuse.org@32.161.217.138) by mail.geekisp.com with (DHE-RSA-AES256-SHA encrypted) SMTP; 9 Jan 2008 18:21:21 -0000 X-Mailer: QUALCOMM Windows Eudora Version 7.1.0.9 Date: Wed, 09 Jan 2008 10:20:21 -0800 To: pgsql-sql-owner@postgresql.org,pgsql-sql@postgresql.org From: Steve Midgley Subject: Re: How to keep at-most N rows per group? periodic DELETEs or constraints or..? In-Reply-To: <20080109152431.79AFF2E02F6@postgresql.org> References: <20080109152431.79AFF2E02F6@postgresql.org> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed Message-Id: <20080109182115.6E39C2E3239@postgresql.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/56 X-Sequence-Number: 29909 At 07:20 AM 1/9/2008, pgsql-sql-owner@postgresql.org wrote: >Date: Tue, 8 Jan 2008 17:41:18 +0000 >From: "Jamie Tufnell" >To: pgsql-sql@postgresql.org >Subject: Re: How to keep at-most N rows per group? periodic DELETEs or >constraints or..? >Message-ID: > > >On 1/8/08, codeWarrior wrote: > > Jamie: > > > > I think you are probably having slowdown issues in your "DELETE > FROM WHERE > > NOT IN SELECT ORDER BY DESCENDING" construct -- that seems a bit > convoluted > > to me.... > >Hmm so rather than NOT IN ( .. LIMIT 50) would you suggest IN ( ... >OFFSET 50) like in Erik's example? Or something else entirely? > > > ALSO: It looks to me like you have a column named "timestamp' ??? > This is > > bad practice since "timestamp" is a reserved word... You really > ought NOT to > > use reserved words for column names... different debate. > >I do realize it would be better to use something else and thanks for >the tip This is an established database and "timestamp" has been >used in other tables which is why I stuck to it here.. one day when >time permits maybe I'll rename them all! > > > Why bother deleting records anyway ? Why not alter your query that > tracks > > the 50 records to LIMIT 50 ??? > >The read query does LIMIT 50 and the reason for deleting the rest of >the records is because they're not needed by the application and >there's loads of them being created all the time (currently several >million unnecessary rows) -- I imagine eventually this will slow >things down? > >Do you think a regular batch process to delete rows might be more >appropriate than a trigger in this scenario? > >Thanks, >Jamie This is kludgy but you would have some kind of random number test at the start of the trigger - if it evals true once per every ten calls to the trigger (say), you'd cut your delete statements execs by about 10x and still periodically truncate every set of user rows fairly often. On average you'd have ~55 rows per user, never less than 50 and a few outliers with 60 or 70 rows before they get trimmed back down to 50.. Seems more reliable than a cron job, and solves your problem of an ever growing table? You could adjust the random number test easily if you change your mind of the balance of size of table vs. # of delete statements down the road. Steve