sql server 单表死锁问题

发布于 2024-08-28 04:08:18 字数 224 浏览 5 评论 0原文

我在 sql server 2000 中有一张表,比方说“xxx”。一个 .exe 正在通过 sql 作业将数据插入到该表“xxx”中。但是,一旦插入数据,一个存储过程就会从该“xxx 表”中读取数据,然后插入/更新到其他两个表中,并将状态更新回同一个“xxx”表中。现在,客户说该“xxx”表上发生了多个死锁。请任何人告诉我解决此死锁问题所需采取的解决步骤以及如何逐步识别它......

提前感谢...... XXX

I have one table let's say "xxx "in sql server 2000 . One .exe is inserting data into that table "xxx" through sql job. But once data is inserted , one stored procedure is reading the data from that "xxx table "and inserting/updating into other two tables and updating back the status into the same "xxx" table. Now, client says that multiple deadlocks are occuring on that "xxx" table . please kindly anyone sdvice me the resolution steps to be taken to resolve this deadlock issue and how to identify it step by step.............

Thanks in advance.....
XXX

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往昔成烟 2024-09-04 04:08:18

即使我可以访问您的系统和所有代码,这也是一个非常难以解决的问题。也就是说,您在问题中没有提供很多细节。

因此,应用减少 SQL Server 死锁的一般规则:

  • 确保数据库设计已适当标准化。
  • 让应用程序每次都以相同的顺序访问服务器对象。
  • 在交易期间,不允许用户输入任何内容。在交易开始之前收集它。
  • 避免光标。
  • 保持事务尽可能短。帮助实现此目的的一种方法是通过使用存储过程或将事务保留在单个批处理中来减少应用程序和 SQL Server 之间的往返次数。减少事务完成时间的另一种方法是确保您不会一遍又一遍地执行相同的读取。如果您的应用程序确实需要多次读取相同的数据,请通过将其存储在变量或数组中来缓存它,然后从那里而不是从 SQL Server 重新读取它。
  • 减少锁定时间。尝试开发您的应用程序,以便它在尽可能晚的时间获取锁,然后尽早释放它们。
  • 如果合适,请使用 ROWLOCK 或 PAGLOCK 减少锁升级。
  • 如果被锁定的数据不经常修改,请考虑使用 NOLOCK 提示来防止锁定。
  • 如果合适,请为运行事务的用户连接使用尽可能低的隔离级别。
  • 考虑使用绑定连接。

Even if I had access to your system and all of your code, this is a very difficult issue to resolve. That said, you don't provide many details in your question.

So apply the general rules of Reducing SQL Server Deadlocks:

  • Ensure the database design is properly normalized.
  • Have the application access server objects in the same order each time.
  • During transactions, don't allow any user input. Collect it before the transaction begins.
  • Avoid cursors.
  • Keep transactions as short as possible. One way to help accomplish this is to reduce the number of round trips between your application and SQL Server by using stored procedures or keeping transactions with a single batch. Another way of reducing the time a transaction takes to complete is to make sure you are not performing the same reads over and over again. If your application does need to read the same data more than once, cache it by storing it in a variable or an array, and then re-reading it from there, not from SQL Server.
  • Reduce lock time. Try to develop your application so that it grabs locks at the latest possible time, and then releases them at the very earliest time.
  • If appropriate, reduce lock escalation by using the ROWLOCK or PAGLOCK.
  • Consider using the NOLOCK hint to prevent locking if the data being locked is not modified often.
  • If appropriate, use as low of an isolation level as possible for the user connection running the transaction.
  • Consider using bound connections.
ぽ尐不点ル 2024-09-04 04:08:18

您可以尝试对读取行的查询应用提示:with(updlock,holdlock)。如果您没有聚集索引,请尝试创建一个在选择阶段使用的聚集索引。如果可能的话,尝试减少事务中处理的行数。

是否可以先更新表中的行,然后填充其他表?如果出现错误,您可以回滚事务。

我不建议使用 NOLOCK 提示,尤其是当您更新相同的数据时。

You might try to apply hint to the query that reads the rows: with(updlock, holdlock). Try to create a clustered index that would be used during the select phase if you don't have one. Try to narrow the amount of rows processed in transactions, if possible.

Is it possible to first update rows in the table and then populate other tables? If there is an error you can roll back the transaction.

I don't recommend using NOLOCK hint especially when you are updating the same data.

涫野音 2024-09-04 04:08:18

根据我使用 Sql Server 2000 的经验,避免死锁的最佳方法是限制查询内的并行性。
这可以通过添加两个查询(插入和 SP)或什至只是在查询末尾插入提示选项(Maxdop 1)来完成。

这会限制性能,但您可以安全执行。
如果只有一个线程正在运行,则没有其他线程可能发生死锁。

From my experience with Sql Server 2000 best way to avid deadlocks is to limit parallelism inside the query.
This can be done by adding on both queries (insert and SP) or even just the insert the hint Option (Maxdop 1) at the end of the query

This will limit the performance but you have a safe execution.
If only one thread is running there is no other thread that can deadlock.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文