假设我们有这些表:
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX),
) PRIMARY KEY (SingerId);
CREATE TABLE Albums (
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING(MAX),
) PRIMARY KEY (SingerId, AlbumId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE;
我想对专辑表执行写入操作,歌手表也被锁定还是只是专辑表被锁定?
我搜索了
interleaved table lock
,但一无所获。
不过我确实找到了这篇文章,其中有这样的摘录:
Unlike other approaches that lock entire tables or rows, the granularity of transactional locks in Spanner is a cell, or the intersection of a row and a column. This means that two transactions can read and modify different columns of the same row at the same time. To maximize the number of transactions that have access to a particular data cell at a given time, Cloud Spanner uses different lock modes
这是否意味着在更新交错子表时父表未锁定?
Cloud Spanner 不采用表锁,因此从这个意义上说,您问题的直接答案是“否”。相反,Cloud Spanner 在单元级别进行锁定,如您引用的文章所述。
具体来说,在您给出的示例中,在Albums
表中插入一行需要
Singer
记录存在。这需要锁定特定
Singer
记录的主键值。这再次意味着,当插入
Singer
记录的事务仍在运行时,您无法删除
Album
记录。但是,您可以更新同一
Singer
记录的非主键值(假设没有其他语句对任何这些单元格采取任何锁定)。如果您在插入
Singer
记录的事务仍处于活动状态时尝试删除
Album
记录,则其中一项事务将被 Cloud Spanner 中止。