9

異なるセッションで同時に実行されているストアド プロシージャによって同時に CRUD された SQL サーバーのテーブルがあります。

|----------------|---------|
| <some columns> | JobGUID |
|----------------|---------|

手順は次のとおりです。

  1. GUID を生成します。
  2. 上記の共有テーブルにいくつかのレコードを挿入し、ステップ 1 の GUID でマークします。
  3. ステップ 2 のすべてのレコードに対していくつかの更新を実行します。
  4. ステップ 3 のレコードを SP 出力として選択します。

ストアド プロシージャ内のすべての select / insert / update / delete ステートメントにはWHERE JobGUID = @jobGUID句があるため、プロシージャは手順 2 で挿入したレコードでのみ機能します。共有テーブル。SQL Server プロファイラーからのデッドロック グラフを次に示します。

SQL Server プロファイラーのデッドロック グラフ

ロックのエスカレーションは発生しません。すべての DML ステートメントにロック ヒントを追加し(UPDLOCK, ROWLOCK)たり、プロシージャの本体をトランザクションにラップしたり、さまざまな分離レベルを使用したりしようとしましたが、役に立ちませんでした。共有テーブルに対する同じ RID ロックが引き続き使用されます。

その後、共有テーブルに主キー/ID 列がないことがわかりました。そして、それを追加すると、デッドロックが消えたように見えます:

alter table <SharedTable> add ID int not null identity(1, 1) primary key clustered

主キー列を削除すると、デッドロックが再発します。追加し直すと、デッドロックを再現できなくなります。

問題は、主キー ID 列が本当にデッドロックを解決できるのか、それとも単なる偶然なのかということです。

更新: @Catcallが示唆するように、既存の列に (ID 列を追加せずに) 自然にクラスター化された主キーを作成しようとしましたが、それでも同じデッドロックが発生しました (もちろん、今回は RID ロックではなくキーロックでした) )。

4

2 に答える 2

4

The best resource (still) for deadlock resolution is here: http://blogs.msdn.com/b/bartd/archive/2006/09/09/deadlock-troubleshooting_2c00_-part-1.aspx.

Pt #4 says:

Run the queries involved in the deadlock through Database Tuning Advisor. Plop the query in a Management Studio query window, change db context to the correct database, right-click the query text and select “Analyze Query in DTA”. Don’t skip this step; more than half of the deadlock issues we see are resolved simply by adding an appropriate index so that one of the queries runs more quickly and with a smaller lock footprint. If DTA recommends indexes (it'll say “Estimated Improvement: %”), create them and monitor to see if the deadlock persists. You can select “Apply Recommendations” from the Action drop-down menu to create the index immediately, or save the CREATE INDEX commands as a script to create them during a maintenance window. Be sure to tune each of the queries separately.

I know this doesn't "answer" the question to why necessarily, but it does show that adding indexes can change the execution in ways to make either the lock footprint smaller or execution time faster which can significantly reduce the chances of a deadlock.

于 2012-05-24T13:51:27.287 に答える
1

最近、この投稿を見ました。上記の情報によると、この投稿がお役に立てば幸いです。

http://databaseusergroup.blogspot.com/2013/10/deadlocked-on-sql-server.html

于 2013-10-08T10:27:57.643 に答える