【发布时间】:2021-06-11 12:25:06
【问题描述】:
我需要获取行级锁定以进行更新,同时允许其他选择查询获取未锁定的预期行。
我观察到的是,如果我锁定 row1,则不允许其他正在搜索其他行的选择查询。
我有下表架构 -
CREATE TABLE lock_test(
id int NOT NULL DEFAULT unique_rowid(),
col_with_unique_index text NOT NULL,
other_col text NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
UNIQUE INDEX unique_idx (col_with_unique_index ASC),
FAMILY "primary"(id, col_with_unique_index, other_col));
在 2 行下方插入 -
insert into lock_test(col_with_unique_index, other_col) values('val1', 'some_val');
insert into lock_test(col_with_unique_index, other_col) values('val2', 'some_val');
开通2个航站楼-
第一个航站楼 -
begin;
select * from lock_test where col_with_unique_index = 'val1' for update;
第二终端 -
select * from lock_test where col_with_unique_index = 'val2';
期望第二个终端显示 val2 的结果,但它没有(进入等待状态),而是在我在第一个终端执行提交后,第二个终端显示了结果。
我尝试将我的 where 子句从 col_with_unique_index 更改为 id 这是这里的主键,这次第二个终端没有等待并显示了预期的结果。
我无法理解这里的行为。如果我的 where 子句中有主键,我只能使用行级锁吗?
【问题讨论】:
标签: database concurrency locking cockroachdb select-for-update