Matthew Hogoboom

Case study · SQL Server

Ending deadlocks and lock starvation on a reporting SQL Server

Fixing failing Power BI and Excel refreshes on SQL Server 2022 without changing a single report, and the follow-up problem the fix created.

  • ~100 → 0Deadlocks per day
  • 122Tables reloaded in 11 s
  • 0Reports rewritten

The setting

Our reporting runs on a SQL Server 2022 instance that holds a replicated copy of the ERP database plus the reporting, staging and vendor databases built around it. Analysts refresh Power BI and Excel against it all day, and a load job rebuilds about 120 reporting tables every 15 minutes. One constraint shaped everything: I couldn't change how the analysts wrote their queries, so every fix had to be on the server side.

Problem one: one Excel window stalls everyone

Reports would periodically hang for everyone at once. The blocking chain always started the same way: an Excel workbook pulling an entire reporting view over ODBC, streaming rows for over an hour, and holding a schema-stability lock the whole time. A scheduled job that refreshes view definitions needed a schema-modification lock on the same object, so it queued behind the workbook. And because SQL Server queues lock requests in order, every new query, even IntelliSense in someone's SSMS, queued up behind the waiting job.

Killing blockers one at a time didn't work: as each reader finished, another Power Query connection arrived and the job got a new blocker. One run took 82 minutes against a normal 3 to 5 seconds. The lesson that took longest to accept: read-committed snapshot isolation doesn't help here, because every reader takes a schema-stability lock regardless of isolation level.

The corrective plan was to bound the job's wait (a lock timeout with retries, so it fails fast instead of holding a queue open), narrow it to only the objects the nightly restore actually invalidates, move it outside business hours, alert on blocking before users notice, and move the worst workbook to a filtered Power Query load.

Problem two: 100 deadlocks a day

Separately, the analysts' Power BI refreshes were failing with deadlock errors: between 16 and 148 a day. I pulled the deadlock graphs from SQL Server's built-in system_health session and summarized them by participant. (That tooling is Get-SqlDeadlockReport in my infra-toolkit.)

The pattern was unambiguous. Every 15 minutes the load job dropped and recreated each table, changing the system catalog. Power Query reads that same catalog every time someone opens the navigator or refreshes. The two collided, and the load never lost: it had been involved in 470 deadlocks without once being the victim. Two analysts alone had more than 500 failed refreshes in three weeks.

I rewrote the load to reload each table in place inside its own transaction instead of dropping and recreating it, so the catalog stops changing on every run. Two safeguards came along: if a source view's columns change, only that table falls back to the old rebuild-and-swap path, and if a reload ever collides with a user's query, the load yields, keeps the previous data, and retries on the next run. With read-committed snapshot isolation turned on at the same time, readers keep seeing the previous rows until each table commits.

The first run reloaded all 122 tables in 11 seconds, and the deadlocks stopped that evening.

The part I'd normally leave out

The next morning the load started taking 7 to 12 minutes per run. A live sample showed it waiting on disk reads, not other users, and page life expectancy had collapsed to about 170 seconds. The tables were heaps, and the TABLOCK hint on the insert meant every reload wrote to fresh pages while leaving the old ones allocated. The pages were 2.7% full and three tables were growing by about 1.5 GB per run. I paused the schedule, removed the hint, verified the runs, and wrote it up.

It's the change I learned the most from: a fix can be correct for the problem it targets and still need watching the next day.

Also in this work

  • Weekly integrity checks across all 13 databases, plus SQL Agent and Database Mail alerting.
  • Six unused SQL Server services disabled to cut attack surface and memory pressure.
  • A four-database federation that keeps the replicated source, the reporting layer and an outside vendor's data separate, with a stored procedure that safely reasserts permissions after any restore.

Related