我需要获取行级锁定以进行更新,同时允许其他选择查询获取未锁定的预期行。
我观察到的是,如果我锁定 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;
2号航站楼——
select * from lock_test where col_with_unique_index = 'val2';
预计第二个终端会显示val2的结果,但它没有(进入等待状态),而是在我在第一个终端执行提交后,第二个终端显示了结果。
我尝试将 where 子句从col_with_unique_index更改为这里的主键id,这次第二个终端没有等待并显示了预期的结果。
我无法理解这里的行为。如果我的 where 子句中有主键,我只能使用行级锁吗?