【问题标题】:Why does SQL Server explicit predicate locking disallow INSERT statements outside of the predicate lock为什么 SQL Server 显式谓词锁定不允许在谓词锁之外插入语句
【发布时间】:2017-03-03 13:31:09
【问题描述】:

假设我们有以下数据库表:

create table department (
    id bigint not null, 
    budget bigint not null, 
    name varchar(255), 
    primary key (id)
)

create table employee (
    id bigint not null, 
    name varchar(255), 
    salary bigint not null, 
    department_id bigint, 
    primary key (id)
)

alter table employee 
add constraint FKbejtwvg9bxus2mffsm3swj3u9
foreign key (department_id) references department

我们有 3 个department 行:

insert into department (name, budget, id) 
values ('Department 1', 100000, 1)

insert into department (name, budget, id) 
values ('Department 2', 75000, 2)

insert into department (name, budget, id) 
values ('Department 3', 90000, 3)

我们还有 3 个employee 行:

insert into employee (department_id, name, salary, id) 
values (1, 'CEO', 30000, 1)

insert into employee (department_id, name, salary, id) 
values (1, 'CTO', 30000, 2)

insert into employee (department_id, name, salary, id) 
values (2, 'CEO', 30000, 3)

假设我们有两个并发用户:Alice 和 Bob。

首先,Alice 锁定属于第一个department 的所有员工:

SELECT * 
FROM employee 
WITH (HOLDLOCK) 
WHERE department_id = 1

现在,与此同时,预计 Bob 不能使用相同的 department_id 插入新的 employee

INSERT INTO employee WITH(NOWAIT) (department_id, name, salary, id) 
VALUES (1, 'Carol', 9000, 6)

上面的插入语句以Lock request time out period exceeded 结尾,这很好,因为 Alice 锁定了该特定范围。

但是,为什么下面的插入也被阻止了:

INSERT INTO employee WITH(NOWAIT) (department_id, name, salary, id) 
VALUES (3, 'Dave', 9000, 7)

这个插入语句使用了一个超出 Alice 谓词锁范围的department_id 值。但是,这个插入语句也以Lock request time out period exceeded 异常结束。

为什么 SQL Server HOLDLOCK 谓词锁超出其范围?

更新

通过向 FK 添加索引:

create index IDX_DEPARTMENT_ID on employee (department_id)

将第一个和第二个department 中的employee 条目数增加到1000 个,我设法看到谓词锁的行为符合预期。

【问题讨论】:

    标签: sql-server concurrency transactions locking predicate


    【解决方案1】:

    满足SELECT 查询的唯一方法是执行表扫描。没有自然资源可以仅基于department_id 进行定位和锁定,因此它最终会锁定所有行并阻止任何插入。

    在这个玩具示例中,仅在department_id 上添加索引将无济于事,因为优化器仍会选择执行表扫描。但是,在更大更现实的表上,我相信添加这样的索引将允许查询以更有针对性的方式应用锁。

    【讨论】:

      【解决方案2】:

      Damien 说的是正确的..当您在部门 id(谓词列)上没有索引时,范围会增加并且 HoldLock 意味着

      HOLDLOCK 表示 SERALIZABLE,因此允许 SELECTS,但会阻止 T1 选择的行的 UPDATE 和 DELETES,以及 T1 选择的范围内的任何 INSERT。

      所以在这种情况下,下面表格的索引会有所帮助,我的测试也证实了这一点

      下面是我做的一个示例测试

      在会话 1 中:

      create  index nci_department_id on dbo.employee(department_id)
      include
      (id,name,salary)
      go
      
      begin tran
      
      SELECT * 
      FROM employee 
      WITH (HOLDLOCK) 
      WHERE department_id = 1
      

      在会话 2 中:

      INSERT INTO employee WITH(NOWAIT) (department_id, name, salary, id) 
      VALUES (3, 'Dave', 9000, 7)
      

      现在上面的插入成功了

      参考资料:
      https://stackoverflow.com/a/7845331/2975396

      【讨论】:

        猜你喜欢
        • 2017-07-08
        • 2016-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-11
        • 2017-01-29
        • 1970-01-01
        相关资源
        最近更新 更多