简体   繁体   English

为什么SQL服务器会出现死锁?

[英]Why do deadlocks happen in SQL Server?

So as I understand it, SQL deadlocks happen when a SPID is busy processing another query and it can't be bothered to run another one because it's so busy right now.据我了解,SQL 死锁发生在 SPID 忙于处理另一个查询并且因为它现在很忙而无法运行另一个查询时。 The SQL Server "randomly" picks one of the queries to deadlock out of the resources asked for and fails it out, throwing an exception. SQL 服务器“随机”选择其中一个查询以从请求的资源中死锁并使其失败,从而引发异常。

I have an app running ~ 40 instances and a back-end Windows Service, all of which are hitting the same database.我有一个运行约 40 个实例的应用程序和一个后端 Windows 服务,所有这些都在访问同一个数据库。 I'm looking to reduce deadlocks so I can increase the number of threads I can runs simultaneously.我希望减少死锁,以便增加可以同时运行的线程数。

  1. Why can't SQL Server just enqueue the new query and run it when it has time and the resources are available?为什么 SQL 服务器不能将新查询排入队列并在有时间且资源可用时运行它? Most of what I'm doing can wait a few seconds on occasion.我正在做的大部分事情有时会等待几秒钟。
  2. Is there a way to set Transaction Isolation Level globally without having to specify it at the onset of each new connection/session?有没有一种方法可以全局设置事务隔离级别,而不必在每个新连接/会话开始时指定它?

Your understanding of deadlocks is not correct.您对死锁的理解是不正确的。 What you've described is blocking.你所描述的是阻塞。 It's a common mistake to equate the two.将两者等同起来是一个常见的错误。

A deadlock occurs when two separate transactions each want different resources and neither will release the one that they have so that the other can run.当两个单独的事务都需要不同的资源并且都不会释放它们拥有的一个以便另一个可以运行时,就会发生死锁。 It's probably easier to illustrate:可能更容易说明:

SPID #1 gets a lock on resource A SPID #2 gets a lock on resource B SPID #1 now needs a lock on resource B in order to complete SPID #2 now needs a lock on resource A in order to complete SPID #1 获得资源 A 的锁定 SPID #2 获得资源 B 的锁定 SPID #1 现在需要锁定资源 B 才能完成 SPID #2 现在需要锁定资源 A 才能完成

SPID #1 can't complete (and therefor release resource A) because SPID #2 has it SPID #2 can't complete (and therefor release resource B) because SPID #1 has it SPID #1 无法完成(因此释放资源 A),因为 SPID #2 拥有它 SPID #2 无法完成(因此释放资源 B),因为 SPID #1 拥有它

Since neither SPID can complete one has to give up (ie be chosen by the server as the deadlock victim) and will fail.由于两个 SPID 都无法完成,因此必须放弃(即被服务器选择为死锁牺牲品)并且会失败。

The best way to avoid them is to keep your transactions small (in number of resources needed) and quick.避免它们的最佳方法是使您的事务保持小(所需资源的数量)和快速。

Deadlock is where two threads of processing are both being held up by the other ( it can be more, but two is sufficiently complex ).死锁是两个处理线程都被另一个阻塞的地方(它可以更多,但两个足够复杂)。 So one thread locks a table, then requests a lock on another table.因此,一个线程锁定了一张表,然后请求锁定另一张表。 the other table is locked by the second thread, which cannot progress because it is waiting for a lock on the first table.另一个表被第二个线程锁定,由于它正在等待第一个表上的锁定,因此无法继续进行。

The reason that one of these has to be thrown out is that in a deadlock, they will never end - neither thread can progress at all.其中一个必须被抛出的原因是在死锁中,它们永远不会结束——两个线程都无法前进。 The only answer is for one to be stopped to allow the other to complete.唯一的答案是停止一个以允许另一个完成。

The solution to reducing deadlocks in the sort of situation you are talking about may be to redesign the solution.在您正在谈论的那种情况下减少死锁的解决方案可能是重新设计解决方案。 If you can make sure that less locking occurs, you will have less deadlocks.如果你能确保发生更少的锁定,你就会有更少的死锁。

Deadlocks occurs because, two concurrent transactions may overlap e lock different resources, both required by the other transaction to finish.死锁的发生是因为,两个并发事务可能会重叠并锁定不同的资源,这两个事务都需要另一个事务才能完成。

Let's imagine: 1 - Transaction A locks row1 2 - Transaction B locks row2 3 - Transaction A tries to lock row1, and, because of the previous lock, SQL server waits 4 - Transaction B tries to lock row2, and, because of the previous lock, SQL server waits让我们想象一下: 1 - 事务 A 锁定第 1 行 2 - 事务 B 锁定第 2 行 3 - 事务 A 尝试锁定第 1 行,并且由于先前的锁定,SQL 服务器等待 4 - 事务 B 尝试锁定第 2 行,并且由于先前的锁定锁定,SQL 服务器等待

So, SQL server must choose on transaction, kill it, and allow the other to continue.因此,SQL 服务器必须选择事务,将其杀死,并允许其他服务器继续。

This image ilustrates this situation very well: http://www.eupodiatamatando.com/wp-content/uploads/2008/01/deadlocknajkcomafarialibh3.jpg这张图片很好地说明了这种情况: http://www.eupodiatamatando.com/wp-content/uploads/2008/01/deadlocknajkcomafarialibh3.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM