r/SQLServer • u/ManufacturerSalty148 • Sep 02 '26
Question Archive data in SQL Server – best way to move old logs without killing performance
Hey all,
We've got a bunch of systems writing logs into SQL Server tables, and the main log table has crept up to ~16 million rows. Every few weeks someone runs a DELETE to trim it down, and the whole server stutters for like 20 minutes. Not fun.
The catch: we can't just delete the old rows. We need to move them to an archive table so we still have access to them if something blows up and we need to go back and dig.
I've been trying a basic INSERT INTO archive SELECT ... FROM logs WHERE date < X followed by a DELETE, but even that's slow at this scale and locks things up.
What I'm looking for:
Is there a better pattern for this? (batching, partitioning, something else I'm not thinking of?)
Any gotchas I should know about before I set this up as a recurring job?
Does anyone have a solid stored procedure or script they've used for this in production?
Happy to share schema / stats if that helps. Basically I just want to stop the "why is everything slow" Slack messages every two weeks.
Thanks in advance.
•
u/AutoModerator Sep 02 '26
After your question has been solved /u/ManufacturerSalty148, please reply to the helpful user's comment with the phrase "Solution verified".
This will not only award a point to the contributor for their assistance but also update the post's flair to "Solved".
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/PossiblePreparation Sep 02 '26
Putting it out there, have you tried not archiving? It’s simpler to keep the data where it landed, rather than write it multiple times and face the indexing hit over and over.
What problem are you actually aiming to solve? if you say performance in querying this table then you likely want to look at the design of your indexes instead.
6
u/dnabsuh1 Sep 02 '26
There are a few ways to do this, but the main idea is to break it into smaller chunks - maybe a few thousand rows at a time. If you have a primary key on the table it is even better - Note I need to split the @ and the variable names or reddit thinks I am referring to users. This is the basic pattern we followed purging millions of records daily on a high volume transactional system. Depending on your system, the tables,etc, you can play with the rowcount size, and stick this in a stored procedure to run on a regular basis. The key here is to keep the actual transaction managable.
set @ @ nocount
SET ROWCOUNT 1000
Declare @ purgedate datetime
select @ purgedate= getdate() - 7 #Or what ever your logic is for the date to purge
select * into #TempHoldingTable from Maintable where 1=0 # Build temp table
Select "Starting" # To display the process is starting, and trick the while
while (@@ rowcount >0)
begin
insert into #tempholdingtable select * from maintable where date <@ purgedate
insert into archive select * from #tempholding table
delete from maintable from maintable inner join #tempholdingtable on maintable.primarykey= #tempholdingtable.primarykey
delete from #tempholdingtable ## You can try a truncate here as well -sometimes we had issues with truncate and rowcount
end
3
u/Achsin 1 Sep 02 '26
The archive table is in the same database? Have you looked at partitioning?
1
u/ManufacturerSalty148 Sep 04 '26
Thanks for all the feedback, I really appreciate it! I'll definitely try them all and see which one fits me.
3
u/I_Am_Rook Sep 02 '26
An example would be something like—
Insert into #copyvalues ( id )
Select top (@batchsize) id from logtable where date < archivedate order by id
Insert into logtablearchive ( cols… )
Select cols… from logtable with (rowlock) inner join #copyvalues on id = id
5. Now you have the id of the rows you just copied. Make sure to add some check statement here to ensure the data was archived properly. Now, delete the data from the logtable using the rowlock hint again.
6. Clear/drop temp table. Loop and repeat until zero rows are selected.
2
u/MoistAbuelita Sep 02 '26
You’re on the right track. Archiving and deleting in batches during off hours or non-peak business hours.
2
u/Black_Magic100 Sep 02 '26
I'm not actually sure if it's faster or not but I would use DELETE OUTPUT INTO syntax and delete in tiny batches. If you write your batch properly and have good indexing, it should be able to run during peak hours without issue even in chatty environments. The only issue you need to be careful of are ghost record cleanups falling behind if you have RCSI on and or use availability groups.
2
u/cyberllama Sep 02 '26
Create a brand new partitioned table. Then, in a transaction, rename the current one to <table_name>_old and rename the new table to whatever the old one is called. Done this seamlessly on a few horribly structured tables in our prod databases. For the ones needing an archive, I'm ETLing the previous day's data into an archive on another server that's also been partitioned and compressed, with the prod table having a job that truncates any partition over 30 days old and creates the new partitions 10 days in advance (just in case that job started failing and nobody noticed, as can happen with the people who created that mess in the first place 😂). Your best solution will depend on your particular needs but that one works for us. We used to have 2 huge tables that, because reasons, also had a set of "new" versions set up to run in parallel 10 years earlier and never properly switched over. All 4 tables were heaps, couple of billion rows in each and taking up terabytes of space for no real purpose. They were also in a DB that was replicated to 4 other servers so that unnecessary waste of space was 5x times as bad 🤦🏼♀️
2
u/Complete-Fondant-202 Sep 02 '26
Several ways to address this..... the tiny aspect of your table makes this a lot easier.
16m is small beans. I'm dealing with 10s of billions in one table. The following possibilities exist.
Option 1
Create new table xx_Archive with same structure
DELETE TOP 1000 FROM original table WHERE .... OUTPUT ..... INTO xx_Archive.......
You'd probably want a scheduled job to run this going forward.
Option 2
Create new table xx_Archive, partitioned appropriately.
Partition existing table identically
SWITCH PARTITIONS as and when
Option 3.
Assumes you'll be putting all existing rows into archive.
Rename existing table _Archive
Create new table identical to _Archive structurally under original name, with appropriate permissions etc.
Option 4
Leave it - since 16m is tiny.
As it's a log table, and depends what the datatypes are.....
Enable PAGE or ROW compression on the table.,
If it has columns containing valid XML, set them to type XML COMPRESSED assuming it's a new enough version of MSSQL.
If it has columns containing stack traces, PAGE/ROW compression won't help them. Consider creating a varbinary column, and at time of in sert apply the SQL function Compress() in the insert statement
Assuming you have a date column for date of insert of row, partition it now while you have a chance based on either YEAR or YEAR MONTH. It's much easier on smaller tables in terms of effort.
Ultimately, only you know how quickly the growth occurs.
2
u/muaddba 1 Sep 02 '26
Everyone keeps mentioning how "16 million rows is small" but no one seems to acknowledge that the delete statement locks the server up for 20 minutes. To me that says either the server is way underpowered or the table isn't as small as some folks think. Maybe it has NVARCHAR(max) data in it, maybe the fillfactor is 5%, or maybe something else but a "small" table delete generally doesn't lock up a server for 20m. (Yes, there are reasons it can, but there's been some very large logical leaps made here)
Partitioning is a good solution here for many reasons:
The trick is getting the downtime to put your data into the partitioning scheme. There are a couple of methods that will let you do this slowly over time and then have a quick cutover during a maintenance window. You do need enough space for the table to be stored twice, though.
1
u/MerlinTrashMan Sep 02 '26
Does the log table have a primary key? If so, I would create a stored proc that runs via sql agent every 10 minutes. Select Top 1000 rows Where the date is meets the criteria into a temp table. Then insert into the archive table those rows (join on the temp table). Then delete the where primary key is in the temp table. The important part is to make sure it is running consistently.
1
u/Fergus653 Sep 02 '26
We have a single or minimal number of places that the actual table name is used, so we create a new table with an incremented number on the end, then switch references to it. Then we can do what we need to with the old table.
1
u/RuprectGern Sep 02 '26
You should learn how to partition tables and then use a sliding window for the most recent queries/ most recent data. You can query the table and your results will only come from the top of the table of which you set the partition size 1 year 2 years 3 months whatever.
If your heart is still set on archiving /deleting data you should create a batch routine rather than straight deletes and straight inserts that collect data in batches of 10,000 or 100,000 and performs the operations as it Loops through the data.
Query Google "batch delete database data in SQL Server" or "how to write a looping batch in SQL Server"
1
u/Lost_Term_8080 Sep 02 '26
Why are you archiving? 16 million rows is very little data.
I also suspect your SQL server is severely under-resourced.
But for a pattern to archive more efficiently, google "Brent Ozar Fast ordered Deletes"
You don't have to create a view, also works with CTEs as long as its ordered and supported by index
1
u/chandleya Sep 03 '26
Deletes in the 1000s should take a few seconds. You either have a ton of indexes, no index on the delete, or poor IO. This can be amplified by always on configurations.
You need to see an execution plan for the delete. I fear you’ve got a clustered index scan to pull off the delete.
1
u/CanProfessional766 Sep 03 '26
I’d avoid doing one big
INSERT+DELETE. Batch it in small chunks, ideally by an indexed date column, so locks and log growth stay under control. If this is something you’ll run regularly partitioning is worth looking at too then archiving old partitions can be much cleaner than deleting millions of rows.1
u/IrquiM Sep 08 '26
If you index it properly, it won't be an issue when copying.
But if you index it properly, you won't have to move the rows because 16 millions is not much data.
1
u/AdjustedTwit Sep 08 '26
0
u/jdanton14 Microsoft MVP Sep 02 '26
Do you have cloud options? Archive the logs into cloud storage (you can do this on-prem if you have S3 locally). Everyone else here is correct, but the right architecture is to never store logs in a database.
1
u/[deleted] Sep 05 '26
[removed] — view removed comment