tags:

views:

302

answers:

3

How to delete N oldest entries. I'm limited Sybase. I need to write a stored procedure which would accept a number X and then leave only X newest entries in the table.

For example: Say ID is auto incremented. The smaller it is, the older this entry is.

ID  Text
=========
1   ASD
2   DSA
3   HJK
4   OIU

I need a procedure which would be executed like this.

execute CleanUp 2

and the result will be

ID  Text
=========
3   HJK
4   OIU
+3  A: 

Note: SQL Server syntax, but should work

Delete from TableName where ID in 
    (select top N ID from TableName order by ID )

If you want N to be a parameter you will have to construct the statement string and execute it

declare @query varchar(4000)
set @query = 'Delete from TableName where ID in '
set @query = @query + '(select top ' + @N + ' ID from TableName order by ID )'
exec sp_executesql @query
Eduardo Molteni
Might want that to be "where ID NOT in" - as it is you would be deleting the top N records rather than deleting all but the top n records.
Eric Petroelje
I think Sergej says "How to delete N oldest entries". Am I getting it wrong?
Eduardo Molteni
Sadly it's quite slow, but this probably is the only thing to do.
Sergej Andrejev
For better performance I recommend you to retrieve to highest ID between the records to delete and then do "Delete from TableName where ID <= @highestID"
Eduardo Molteni
@Eduardo - Looks like the title says "n oldest entries" but then the description says "leave only the X newest entries". So I guess we are both right :)
Eric Petroelje
What's wrong with title. Delete N oldest and leave X newest. X+N=Total number of entries.After fighting with performance issue I ended up using Eduardo suggestion. I understand it's not always precise enough, but the performance is much better.
Sergej Andrejev
+1  A: 

I Like Eduardo's option best as it's the simplest solution, but since Sergej mentions it is quite slow, here's an alternative solution:

Create a stored procedure that does the following:

  1. Create a temp table with the same structure as the original table.
  2. Insert the top N rows into the temp table.
  3. Truncate the original table.
  4. Copy the rows from the temp table back to the original table.

Generally this will be much faster, especially if you have lots of rows in the table.

Eric Petroelje
A: 

If you have a clustered index on id, it's safe to execute a delete top query.

delete top 2 from TableName;
brianegge
No, it really isn't. A clustered index provides no ordering guarantees for any statement. The fact that, *by luck*, it appears to work in simple cases does not mean it should be relied upon.
Damien_The_Unbeliever
From the previously linked document: When you use top n with delete, update, or in a view, you cannot specify ordering. If there is an implied order on the table from a clustered index, that order applies, otherwise, the results are unpredictable and they can be in any order.I wouldn't say this is by luck if the documentation specifies it.
brianegge