SQL事务自动回滚
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL事务自动回滚相关的知识,希望对你有一定的参考价值。
我正在使用Storedproc,DML语句发生在Transaction中。在交易中,我们正在更新多个列,如下所示:
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
BEGIN TRAN
UPDATE TBLMarket SET [YR] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [YR] = ' ';
UPDATE TBLMarket SET [TYBEG] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [TYBEG] = ' ';
UPDATE TBLMarket SET [TYEND] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [TYEND] = ' ';
UPDATE TBLMarket SET [PSEQ] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [PSEQ] = ' ';
UPDATE TBLMarket SET [CTI] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [CTI] = ' ';
UPDATE TBLMarket SET [GTI] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [GTI] = ' ';
UPDATE TBLMarket SET [PTI] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [PTI] = ' ';
UPDATE TBLMarket SET [TIPR] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [TIPR] = ' ';
UPDATE TBLMarket SET [RAR] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [RAR] = ' ';
UPDATE TBLMarket SET [TMOD_E] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [TMOD_E] = ' ';
UPDATE TBLMarket SET [ATI] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [ATI] = ' ';
UPDATE TBLMarket SET [PERC] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [PERC] = ' ';
UPDATE TBLMarket SET [PPCT] = NULL WHERE ImportTrnId = @pi_ImportTrnId AND [PPCT] = ' ';
.
.
.
.
.
-----120 update statements
.
.
COMMIT TRAN
但是,当我并行运行多个事务时(我们尝试使用50个事务),我们遇到了这个错误。
“事务(进程ID 102)在锁资源上与另一个进程陷入僵局,并被选为死锁牺牲品。重新运行该事务。”
注意:每个事务都有不同的@pi_ImportTrnId,这意味着并行运行的事务永远不会尝试更新同一组行。
任何帮助/建议将不胜感激。谢谢!
答案
There are no indexes
- 这就是问题所在。在ImportTrnId
列上放置索引,以单语句方式重写查询,删除READ UNCOMMITTER
,它将运行得很好。
UPDATE t SET
[YR] = ISNULL(NULLIF(LTRIM(t.[YR]), ''), t.[YR]),
[TYBEG] = ISNULL(NULLIF(LTRIM(t.[TYBEG]), ''), t.[TYBEG]),
...
FROM TBLMarket t
WHERE ImportTrnId = @pi_ImportTrnId
以上是关于SQL事务自动回滚的主要内容,如果未能解决你的问题,请参考以下文章