Deadlocks: let’s do one, understand it, and fix it.

You keep getting warnings and emails about deadlocks, but let’s be honest: you’re not really sure how they happen or what to do about it.

I’ll show you how to build a deadlock in just a few lines of T-SQL so you can see how SQL Server reacts. You’ll be more comfortable with the decisions SQL Server takes, and when you can safely ignore ’em.

When you can’t ignore ’em, you’ll learn how to analyze them with free tools (sp_BlitzLock and SentryOne Plan Explorer), and how to fix them with index and code changes.

https://www.slideshare.net/BrentOzar/deadlocks-lets-do-one-understand-it-and-fix-it

Demo Scripts

To follow along, you can use any version of the Stack Overflow database (even the small one is fine.)

NOLOCK Demo Script – Left Side

NOLOCK Demo Script – Right Side

Deadlock Demo Script – Left Side

Deadlock Demo Script – Right Side

More Complex Stored Procedure Example

With this example, I use a single stored procedure that we’ll call from different sessions, and we’ll produce deadlocks with it, and then fix ’em.