T-SQL唯一约束锁定了SQL服务器

发布于 2024-09-05 07:57:51 字数 654 浏览 1 评论 0原文

你好 ! 这是我的表:

CREATE TABLE [ORG].[MyTable](   
    ..
    [my_column2] UNIQUEIDENTIFIER NOT NULL  CONSTRAINT FK_C1 REFERENCES ORG.MyTable2 (my_column2),  
    [my_column3] INT NOT NULL CONSTRAINT FK_C2 REFERENCES ORG.MyTable3 (my_column3)
    ..
    )

我编写了此约束以确保 my_column2 和 my_column3 的组合始终是唯一的。

ALTER TABLE [ORG].[MyTable] ADD
 CONSTRAINT UQ_MyConstraint UNIQUE NONCLUSTERED
 (
    my_column2,
    my_column3
 )

但突然间..数据库停止响应..有锁或其他东西.. 你知道为什么吗? 约束有什么不好?

我已经检查过的是,当我从 master.dbo.syslockinfo (与 sysprocesses 联接)中选择 * 时,我有一些锁定的对象。 10 分钟不活动后...此列表再次为空,我可以访问数据库中的所有对象。诡异的..

HI !
This is my table:

CREATE TABLE [ORG].[MyTable](   
    ..
    [my_column2] UNIQUEIDENTIFIER NOT NULL  CONSTRAINT FK_C1 REFERENCES ORG.MyTable2 (my_column2),  
    [my_column3] INT NOT NULL CONSTRAINT FK_C2 REFERENCES ORG.MyTable3 (my_column3)
    ..
    )

I've written this constraint to assure that combination my_column2 and my_column3 is always unique.

ALTER TABLE [ORG].[MyTable] ADD
 CONSTRAINT UQ_MyConstraint UNIQUE NONCLUSTERED
 (
    my_column2,
    my_column3
 )

But then suddenly.. The DB stopped responding.. there is a lock or something..
Do you have any idea why?
What is bad with the constraint?

What I've already checked is that I have some locked objects when I select * from master.dbo.syslockinfo (joined with sysprocesses). After 10 minutes of inactivity.. this list is empty again and I can access all the objects in database. Weird..

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

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

发布评论

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

评论(1

骷髅 2024-09-12 07:57:52

它必须在检查数据时锁定表以查看是否违反约束,否则在执行此操作时可能会插入一些坏数据一些

操作,例如重建索引(当然不使用企业版中的ONLINE)执行此操作时将使表无法访问

It has to lock the table while checking the data to see if it violates the constraint or not, otherwise some bad data might get inserted while it was doing this

Some operations like rebuilding an index (Of course not using the ONLINE in Enterprise Edition) also will make the table inaccessible while it does this

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