为什么插入时索引记录独占锁只有在冲突的时候才存在?

2020-07-04 13:02:50 +08:00
 JasonLaw

执行以下代码

create table t(id int primary key);
insert into t values (5),(10);

-- session 1
start transaction;
insert into t values (8);

此时,select * from performance_schema.data_locks的输出是:

+--------+--------------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+-----------+-------------+-----------+
| ENGINE | ENGINE_LOCK_ID                       | ENGINE_TRANSACTION_ID | THREAD_ID | EVENT_ID | OBJECT_SCHEMA | OBJECT_NAME | PARTITION_NAME | SUBPARTITION_NAME | INDEX_NAME | OBJECT_INSTANCE_BEGIN | LOCK_TYPE | LOCK_MODE | LOCK_STATUS | LOCK_DATA |
+--------+--------------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+-----------+-------------+-----------+
| INNODB | 139956701343096:1063:139956609693392 |                  2070 |        48 |       18 | test          | t           | NULL           | NULL              | NULL       |       139956609693392 | TABLE     | IX        | GRANTED     | NULL      |
+--------+--------------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+-----------+-------------+-----------+

session 1 的那个事务只有一个表级别的独占意向锁。

但是执行以下代码之后,

-- session 2
start transaction;
select * from t where id = 8 for share;

select * from performance_schema.data_locks的输出是:

+--------+---------------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+---------------+-------------+-----------+
| ENGINE | ENGINE_LOCK_ID                        | ENGINE_TRANSACTION_ID | THREAD_ID | EVENT_ID | OBJECT_SCHEMA | OBJECT_NAME | PARTITION_NAME | SUBPARTITION_NAME | INDEX_NAME | OBJECT_INSTANCE_BEGIN | LOCK_TYPE | LOCK_MODE     | LOCK_STATUS | LOCK_DATA |
+--------+---------------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+---------------+-------------+-----------+
| INNODB | 139956701343096:1063:139956609693392  |                  2070 |        48 |       18 | test          | t           | NULL           | NULL              | NULL       |       139956609693392 | TABLE     | IX            | GRANTED     | NULL      |
| INNODB | 139956701343096:2:4:4:139956609690400 |                  2070 |        49 |       14 | test          | t           | NULL           | NULL              | PRIMARY    |       139956609690400 | RECORD    | X,REC_NOT_GAP | GRANTED     | 8         |
| INNODB | 139956701343944:1063:139956609699536  |       421431678054600 |        49 |       14 | test          | t           | NULL           | NULL              | NULL       |       139956609699536 | TABLE     | IS            | GRANTED     | NULL      |
| INNODB | 139956701343944:2:4:4:139956609696624 |       421431678054600 |        49 |       14 | test          | t           | NULL           | NULL              | PRIMARY    |       139956609696624 | RECORD    | S,REC_NOT_GAP | WAITING     | 8         |
+--------+---------------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+---------------+-------------+-----------+

此时,显示 session 1 的那个事务持有 id 为 8 的那个索引记录的独占锁。但是为什么在第一次输出时没有显示呢?

757 次点击
所在节点    数据库
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/687094

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX