Repository navigation
Fix audit retention queries that exceed the command timeout on a large SQL Server table - #6202
ramonsmits wants to merge 3 commits into
Conversation
The DISTINCT query over the expired rows reads all of them. On a large table it exceeds the command timeout, and the retention pass fails before it deletes a row. The new query seeks the next expired hour on the clustered key.
The delete that EF Core generates failed on a large table in two ways: - The compile waited for a synchronous update of the statistics of the table. - The plan read IX_AuditMessages_Id in Id order with a filter on the hour. The last batch of an hour has fewer rows than the batch size, so the scan read the complete index. Both exceeded the command timeout, and the retention loop then waited for its next hourly tick. The statement is now raw SQL with WITH (FORCESEEK) and OPTION (KEEPFIXED PLAN), because EF Core cannot add these hints to ExecuteDeleteAsync.
A failed sweep waited for the next hourly tick, so one command timeout stopped the purge for up to one hour. The loop now starts the sweep again after 10 seconds, and doubles the delay for each next failure up to the sweep interval. Jitter spreads each delay over the upper half of its range.
82a8ca7 to
8fa07f3
Compare
| { | ||
| try | ||
| var attempt = 0; | ||
| while (!await TrySweep(cancellationToken)) |
There was a problem hiding this comment.
We should also be reporting to the custom check when the sweep takes longer than the timer interval, because that will be causing an ever-growing database.
There was a problem hiding this comment.
We should also be reporting to the custom check when the sweep takes longer than the timer interval, because that will be causing an ever-growing database.
@rbev I thought there was already a custom check for that? If there was no cleanup happening for a specific duration or something. I'll check.
There was a problem hiding this comment.
@rbev I understand your comment, the custom check was based on failure count... I refactored that to be based on time in the following PR. Also, the timestamp is based on data from the database so it also works then the instance is restarted/crashes. Previous implementation count was non-durable:
Audit retention on SQL Server fails on a large table. The retention queries exceed the 30-second command timeout, and the retention loop then waits for its next hourly tick. No row is removed and the database grows.
DISTINCTquery reads all expired rows. The new query seeks the next expired hour on the clustered key, one seek for each hour.IX_AuditMessages_IdinIdorder and filter on the hour. The last batch of an hour has fewer rows than the batch size, so the scan reads the complete index.WITH (FORCESEEK)forces the seek on the clustered key.OPTION (KEEPFIXED PLAN)removes that wait.The delete is raw SQL with the statement shape that EF Core generates, because
ExecuteDeleteAsynccannot add the hints.Measured on a 1.1 TB database with 115 million rows: without the query changes no retention pass completes. With them, more than 1,700 delete batches ran without an error.
Notes for the review