久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL在刪除子表行時鎖定父表

SQL locks parent table while deleting child table row(SQL在刪除子表行時鎖定父表)
本文介紹了SQL在刪除子表行時鎖定父表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

TLDR: 在嘗試按主鍵刪除包含指向另一個父"表的外鍵的子"表上的行時,它會在子表的持續時間內鎖定父表交易.外鍵/子刪除可以做什么來防止發生鎖定?

TLDR: While trying to delete a row by primary key on a "child" table that contains a foreign key to another "parent" table, it locks the parent table for the duration of the child's transaction. What could be done with the foreign key / child delete to prevent that lock from happening?

設置:

IF ( SELECT OBJECT_ID('dbo.Child')
   ) IS NOT NULL
   DROP TABLE dbo.Child;
IF ( SELECT OBJECT_ID('dbo.Parent')
   ) IS NOT NULL
   DROP TABLE dbo.Parent;
GO
CREATE TABLE dbo.Parent
       (
         ID INT PRIMARY KEY
                IDENTITY(1, 1) ,
         Value TINYINT NOT NULL
       );
CREATE TABLE dbo.Child
       (
         ID INT PRIMARY KEY
                IDENTITY(1, 1) ,
         Parent_ID INT CONSTRAINT FK_Child_Parent_ID FOREIGN KEY REFERENCES Parent ( ID ) ,
         Value TINYINT NOT NULL
       );
GO
INSERT  INTO dbo.Parent
        ( Value )
VALUES  ( 1 ),
        ( 2 );
INSERT  INTO dbo.Child
        ( Parent_ID, Value )
VALUES  ( 1, 1 );
GO

連接 1:(先運行)

BEGIN TRANSACTION;
DELETE  dbo.Child
WHERE   Child.ID = 1;

連接 2:

DELETE  dbo.Parent
WHERE   Parent.ID = 2;

<小時>

在上面的場景中,連接 2 的刪除將被連接 1 阻塞,直到該連接完成打開的事務 - 即使在父級上刪除的行與被刪除的子級引用的行不同(并且實際上沒有任何子條目).


In the above scenario, the delete from connection 2 will be blocked by connection 1 until that connection finishes the open transaction - even though the row being deleted on the parent is not the same as the row referenced by the child being deleted (and in fact doesn't have any child entries).

有什么方法可以修改約束以允許這種情況起作用嗎?

Is there any way to modify the constraint to allow this scenario to work?

推薦答案

在這種情況下,您只需要在列 Parent_ID 上創建索引.它將強制查詢優化器使用索引查找操作

In this scenario you just need to create index on the column Parent_ID. It will force query optimizer to use Index Seek operation

CREATE INDEX x ON dbo.Child(Parent_ID)

否則 Connection2 將對被 Connection1 阻塞的表 Child 進行聚集索引掃描

Otherwise Connection2 will have Clustered Index Scan on the table Child which blocks by Connection1

這篇關于SQL在刪除子表行時鎖定父表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 成人免费视频一区二区 | 91福利在线导航 | 成人在线视频一区二区三区 | 日韩av在线中文字幕 | 亚洲风情在线观看 | 国产高清视频在线观看 | 麻豆一区二区三区 | 日韩在线观看一区二区三区 | 狠狠综合久久av一区二区老牛 | 欧洲视频一区 | 中文字幕国产第一页 | 亚洲精品免费看 | 国产在线精品一区二区三区 | 午夜激情一区 | 久久成人国产 | 欧美日韩久久久久 | www.av7788.com| 久久美女视频 | 一级毛片成人免费看a | 午夜免费观看体验区 | 久久久久久国产一区二区三区 | 日韩www视频 | 成人精品在线观看 | 国产一区二区在线免费播放 | 97国产爽爽爽久久久 | www.成人久久 | 欧美精品一区二区三区在线播放 | 在线视频 亚洲 | 欧美日韩综合 | 国产91在线精品 | 青青草av在线播放 | 亚洲一区在线日韩在线深爱 | 国内激情av片 | 在线观看视频你懂得 | 高清免费av| 热久久久久 | 国内精品伊人久久久久网站 | 亚洲视频一区二区三区 | 国产a一区二区 | 久久国产精品-国产精品 | 欧美成人精品一区二区男人看 |