久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产成人一区二区三区电影 | 在线观看中文字幕 | av色站| 成人毛片在线观看 | 91精品国产欧美一区二区成人 | 亚洲精品成人 | 黄色毛片在线看 | 日韩第一夜| 欧美福利精品 | 911精品美国片911久久久 | 国产一区二区三区www | 亚洲成人观看 | 天天干天天干 | 色在线免费视频 | 国产激情亚洲 | 国产免费播放视频 | 成人美女免费网站视频 | 精品国产一区一区二区三亚瑟 | 欧美一区二区三区在线播放 | 不卡av在线 | 黄色网址在线免费观看 | 精品欧美一区二区精品久久久 | 亚洲一区二区av | 最近中文字幕第一页 | 91精品国产综合久久久久蜜臀 | 女人夜夜春 | 999久久久久久久久6666 | 欧美日本在线观看 | 欧美精品一二区 | 久久er精品 | 欧美日本韩国一区二区 | 91美女在线观看 | 日韩欧美专区 | 一久久久| 日韩网站在线观看 | 西西裸体做爰视频 | 国产精品久久久久久福利一牛影视 | 国产一区二区三区在线看 | 精品福利在线视频 | 99热在线播放 | 久久精品|