r/learnSQL • u/markinatlanta • 23d ago
A report ran about 10 times a month and caused deadlocks. Lessons from the fix.
We're working with a medium-large sized manufacturer and they had a report running about 10 times a month which was involved in deadlocks.
So interesting tuning takeaways I thought I would share.
Our team traced an increase in the deadlocks to a reporting query and this was the report that ran give or take 10 times a month.
That execution count is what makes this case worth discussing, though because if you prioritize tuning work by frequency alone, a query like this is easy to defer.
The changes were a new nonclustered index and a parameter type change from NVARCHAR to VARCHAR. Recorded logical reads per execution fell from 120,660 to 67,083.
The blocking and deadlocking stopped after the changes.
The lesson for us was how we prioritize these investigations. About 10 runs a month sounds easy to put off in fairness but once we traced the deadlocks to the reporting query, the execution count mattered less than what happened when it ran.
My takeaways:
- Execution frequency belongs alongside incident impact. A report running occasionally still deserves attention when its execution coincides with failures.
- Read reduction is useful evidence, but it does not explain the deadlock cycle. For a similar investigation, I would want the graph, the competing statements and their transaction boundaries before recommending a fix.
- Test with the competing workload. A report completing successfully on its own does not reproduce the conditions of a deadlock between transactions.
- Treat the parameter change as case-specific. Check the data, collation and supported characters before changing NVARCHAR to VARCHAR. Validate the returned results as well as performance.
- Agree on what counts as fixed. If the complaint is failed transactions, the review should include what happened to those failures when the affected operation ran again.
For teams handling intermittent deadlocks, what evidence do you require before closing an incident when the affected report runs infrequently?