
Microsoft SQL Server uses “pessimistic” locking by default– your queries are naturally defensive, paranoid that they might read the wrong data. It’s been the same way for decades, and it’s one of the reasons that apps can seem slow on SQL Server.
There are 3 easy fixes:
- Have just the right amount of indexes – enough to make your queries go fast, but not so many that inserts/updates/deletes slow down. SQL Server has to keep all your indexes in sync in real time, which means the more indexes you have, the longer your locks are held, and the more likely that you’ll run into blocking & deadlocking issues. Learn how to do this in my Fundamentals of Index Tuning class, then moving on to my Mastering Index Tuning class.
- Tune your deadlock-involved queries to be as short & sweet as possible, work through tables in a predictable order. I cover how to do that in my Fundamentals of Query Tuning class, then moving on to my Mastering Query Tuning class.
- Use the right isolation level for your app’s needs – the default (read committed, aka pessimistic) is probably the wrong one for you. Read uncommitted, aka nolock, is usually a bad idea too. Instead, you either want read committed snapshot isolation (RCSI) or snapshot isolation. This one requires server-level tuning first to make sure your server can handle the additional workload, and I cover that in my Fundamentals of TempDB class, then moving on to my Mastering Server Tuning class.
You don’t have to do all 3!
Just pick the one you’re the most comfortable with – tuning indexes, tuning queries, or making server-level changes – and do that one first. In most cases, you can do just one of them, and the blocking & deadlocking will gradually start dropping away.
