Repository navigation
Fix audit retention queries that exceed the command timeout on a large SQL Server table - #6202
Open
ramonsmits wants to merge 3 commits into
Open
ramonsmits wants to merge 3 commits into
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.
ramonsmits
force-pushed
the
audit-retention-delete-plan
branch
from
November 27, 2026 08:26
82a8ca7 to
8fa07f3
Compare
ramonsmits
marked this pull request as ready for review
November 27, 2026 08:27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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