Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The is Log table in production which receive entries from various applications. The Log table receives around 5000 rows per second. As per client requirement, they want to run a hourly job to copy all records to Archive Log table and delete all the copied records from Log table. This will happen while 5000 rows per second inserts are committing.

My approach:
SQL
DECLARE @startID AS integer
DECLARE @endID AS integer

SET @startID = (SELECT MIN(ID) FROM Log)
SET @endID   = (SELECT MAX(ID) FROM Log)
INSERT INTO ArchiveLog SELECT * FROM Log WHERE ID BETWEEN @startID AND @endID
Delete FROM LOG WHERE ID BETWEEN @startID AND @endID


What would be the best possible approach to do this in an fast and optimized way? Please suggest.

Thanks in advance.
Posted
Updated 16-Jan-15 20:19pm
v2

1 solution

Based on the information you provided your approach seems valid to me. Few things I would do

  • Ensure that the ID is unique and indexed
  • Typically I discourage the use of NOLOCK hint but in this case I believe it may be justified. See for example What should be considered when NOLOCK hint is used[^]
  • If the deletion takes too much time and the transaction starts to block others run the deletion in a loop and delete smaller portions of records. With each iteration commit the deletion. Depending on the logic you may have to add check that the row exists in the archivelog table
 
Share this answer
 
Comments
Wild-Programmer 17-Jan-15 2:30am    
Thank you so much :)
Wendelius 17-Jan-15 2:42am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900