当前位置: 首页 > MySQL数据库

MySQL mvcc奇怪的现象分析

时间:2026-01-28 14:10:41

奇怪的现象1:

session1:

root@localhost : test 08:43:09> select * from test1;(1)
+---------+------+
| orderid | ID   |
+---------+------+
|       2 | 123  |
+---------+------+
1 row in set (0.00 sec)

root@localhost : test 08:43:14> start transaction;(2)
Query OK, 0 rows affected (0.00 sec)

root@localhost : test 08:43:23> select * from test1;(3)
+---------+------+
| orderid | ID   |
+---------+------+
|       2 | 123  |
+---------+------+
1 row in set (0.00 sec)

root@localhost : test 08:43:27> select * from test1;(5)
+---------+------+
| orderid | ID   |
+---------+------+
|       2 | 123  |
+---------+------+
1 row in set (0.00 sec)

root@localhost : test 08:44:03> update test1 set id=234 where orderid=1;(6)
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

root@localhost : test 08:44:27> select * from test1;(7)
+---------+------+
| orderid | ID   |
+---------+------+
|       1 | 234  |
|       2 | 123  |
+---------+------+
2 rows in set (0.00 sec)

session2:

root@localhost : test 08:43:48> INSERT into test1 values(1,123);(4)
Query OK, 1 row affected (0.01 sec)

奇怪的现象2:

session1:

root@localhost : test 08:46:57> select * from test1;(1)
+---------+------+
| orderid | ID   |
+---------+------+
|       1 | 123  |
|       2 | 123  |
+---------+------+
2 rows in set (0.00 sec)

root@localhost : test 08:47:01> start transaction;(2)
Query OK, 0 rows affected (0.00 sec)

root@localhost : test 08:47:15> select * from test1;(4)
+---------+------+
| orderid | ID   |
+---------+------+
|       1 | 123  |
|       2 | 123  |
|       3 | 123  |
+---------+------+
3 rows in set (0.00 sec)

session2:

insert into test1  values(3,'123');(3)

现象3:

root@localhost : test 08:49:26> start transaction;(1)
Query OK, 0 rows affected (0.00 sec)

root@localhost : test 08:49:28> select * from test1;(2)
+---------+------+
| orderid | ID   |
+---------+------+
|       1 | 123  |
|       2 | 123  |
|       3 | 123  |
+---------+------+
3 rows in set (0.00 sec)

root@localhost : test 08:49:30> select * from test1;(4)
+---------+------+
| orderid | ID   |
+---------+------+
|       1 | 123  |
|       2 | 123  |
|       3 | 123  |
+---------+------+
3 rows in set (0.00 sec)

session2:
root@localhost : test 08:47:43> insert into test1  values(4,'123');(3)
Query OK, 1 row affected (0.01 sec)

现象2和现象3中可以用mvcc中的read-view完美解释:

1、 看不到read view创建时刻以后启动的事务
2、 看不到read view创建时活跃的事务

可能我们熟知的在repeatable-read的隔离级别下,是当一个会话发起dml语句后,在当前会话中如果没有提交当前会话是看不到dml语句操作的结果的!read-view的应用可能很少注意到,像我就是这样。

但是现象1怎么解释呢?

会话1中的insert  trx_id=aa 会话2 中的 trx_id=ac,session2中update之后trx_id=ab ,下面可以看得更清楚

也就是在会话一中update 会话2上的不可见的更新时 会将会话2中的trx_id变为会话1相同的trx_id 所以会话1这个时候能看到的会话2 中不可见数据的更新之后的状态。


上一篇:怎么解决MySQL中的ERROR 1201 (HY000)错误问题
下一篇:怎么使用innodb行锁
mysql
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素