<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.27 | +-----------+ 1 row in set (0.00 sec)
mysql> show variables like '%isola%'; +-----------------------+-----------------+ | Variable_name | Value | +-----------------------+-----------------+ | transaction_isolation | REPEATABLE-READ | +-----------------------+-----------------+ 1 row in set (0.02 sec)
mysql> show create table test; +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` char(32) NOT NULL COMMENT '使用者姓名', `num` int DEFAULT NULL, `phone` char(11) DEFAULT '' COMMENT '手機號', PRIMARY KEY (`id`), KEY `idx_name_phone` (`name`,`phone`) ) ENGINE=InnoDB AUTO_INCREMENT=108 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='test表' | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec
mysql> select * from test; +-----+---------------+---------+-------+ | id | name | num | phone | +-----+---------------+---------+-------+ | 1 | 執行業 | 1234567 | | | 2 | 執行業務1 | NULL | | | 3 | a | NULL | | | 4 | a | NULL | | | 5 | a | NULL | | | 6 | b | 1 | | | 7 | wdf | NULL | | | 10 | dd | 1 | | | 11 | hello | NULL | | | 15 | df | NULL | | | 16 | e | NULL | | | 20 | e | NULL | | | 21 | 好的 | NULL | | | 25 | g | 1 | | | 106 | hello | NULL | | | 107 | a | NULL | | +-----+---------------+---------+-------+ 16 rows in set (0.00 sec)
select * from test where id in (7,15) for update;
。update test set name='d' where id=10;
和insert into test(id,name) values(8,'hello');
。select * from test where id in (7,8,10,15);
。 第二步是否阻塞。第三步是否能讀到事務B執行的更新。mysql> begin;select * from test where id in (7,15) for update; Query OK, 0 rows affected (0.00 sec) +----+------+------+-------+ | id | name | num | phone | +----+------+------+-------+ | 7 | wdf | NULL | | | 15 | df | NULL | | +----+------+------+-------+ 2 rows in set (0.01 sec)
持有鎖情況:
mysql> 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 | 4974808984:1063:4890706744 | 46666 | 50 | 123 | my_test | test | NULL | NULL | NULL | 4890706744 | TABLE | IX | GRANTED | NULL | | INNODB | 4974808984:2:4:7:4915866136 | 46666 | 50 | 123 | my_test | test | NULL | NULL | PRIMARY | 4915866136 | RECORD | X,REC_NOT_GAP | GRANTED | 15 | | INNODB | 4974808984:2:4:9:4915866136 | 46666 | 50 | 123 | my_test | test | NULL | NULL | PRIMARY | 4915866136 | RECORD | X,REC_NOT_GAP | GRANTED | 7 | +--------+-----------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+---------------+-------------+-----------+ 3 rows in set (0.00 sec)
發現7,15持有了行鎖。
mysql> update test set name = 'sds' where id=10;insert into test(id,name) values(8,'hello'); Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 Query OK, 1 row affected (0.00 sec)
mysql> select * from test where id in (7,8,10,15); +----+-------+------+-------+ | id | name | num | phone | +----+-------+------+-------+ | 7 | wdf | NULL | | | 8 | hello | NULL | | | 10 | sds | 1 | | | 15 | df | NULL | | +----+-------+------+-------+ 4 rows in set (0.01 sec)
步驟二執行了,事務A讀到了事務B提交的資料。下面我們來看看正常的select;
還原資料:
mysql> update test set name = 'dd' where id=10;delete from test where id=8; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 Query OK, 1 row affected (0.00 sec)
select * from test where id in (7,15);
。update test set name='d' where id=10;
和insert into test(id,name) values(8,'hello');
。select * from test where id in (7,8,10,15);
。 第二步是否阻塞。第三步是否能讀到事務B執行的更新。mysql> begin;select * from test where id in (7,15); Query OK, 0 rows affected (0.00 sec) +----+------+------+-------+ | id | name | num | phone | +----+------+------+-------+ | 7 | wdf | NULL | | | 15 | df | NULL | | +----+------+------+-------+ 2 rows in set (0.00 sec)
持有鎖情況:
mysql> select * from performance_schema.data_locks; Empty set (0.00 sec)
mysql> update test set name = 'sds' where id=10;insert into test(id,name) values(8,'hello'); Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 Query OK, 1 row affected (0.00 sec)
mysql> select * from test where id in (7,8,10,15); +----+------+------+-------+ | id | name | num | phone | +----+------+------+-------+ | 7 | wdf | NULL | | | 10 | dd | 1 | | | 15 | df | NULL | | +----+------+------+-------+ 3 rows in set (0.00 sec)
步驟二執行了,事務A沒讀到了事務B提交的資料。筆者猜測for update加鎖之後會清除readview或者沒開啟readview,所以後面會讀到事務B的。
所以我們來看看到底是清除還是沒開啟。
mysql> select * from test where id in (7,15) for update; +----+------+------+-------+ | id | name | num | phone | +----+------+------+-------+ | 7 | wdf | NULL | | | 15 | df | NULL | | +----+------+------+-------+ 2 rows in set (0.00 sec) mysql> select * from test where id in (7,8,10,15); +----+------+------+-------+ | id | name | num | phone | +----+------+------+-------+ | 7 | wdf | NULL | | | 10 | dd | 1 | | | 15 | df | NULL | | +----+------+------+-------+ 3 rows in set (0.00 sec)
可以發現重新執行了場景一的步驟後結果沒變。
所以應該是沒開啟,應該是當前讀不會開啟readview。
筆者找了下資料沒找到,找到的筆者可以留言。
不過我們可以使用繼續實驗驗證下。
update test set name = 'dgf' where id in (7,15);
。update test set name='d' where id=10;
和insert into test(id,name) values(8,'hello');
。select * from test where id in (7,8,10,15);
。 第二步是否阻塞。第三步是否能讀到事務B執行的更新。這個場景就不搞實驗步驟了,結果是和筆者的猜想一樣的 ”當前讀不會開啟readview,第一個快照讀才會開啟“
select * from test where id in (7,15);
。insert into test(id,name) values(8,'hello');
。select * from test where id in (7,8,15);
。update test set name ='cv' where id =8;
。select * from test where id in (7,8,15);
。mysql> begin;select * from test where id in (7,15); Query OK, 0 rows affected (0.00 sec) +----+------+------+-------+ | id | name | num | phone | +----+------+------+-------+ | 7 | wdf | NULL | | | 15 | df | NULL | | +----+------+------+-------+ 2 rows in set (0.00 sec)
開啟了事務,淺讀一下。
insert into test(id,name) values(8,'hello');
mysql> select * from test where id in (7,8,15); +----+------+------+-------+ | id | name | num | phone | +----+------+------+-------+ | 7 | wdf | NULL | | | 15 | df | NULL | | +----+------+------+-------+ 2 rows in set (0.00 sec)
檢驗一下是否讀的到,發現讀不到。
mysql> update test set name ='cv' where id =8; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
對插入的進行更新。
mysql> select * from test where id in (7,8,15); +----+------+------+-------+ | id | name | num | phone | +----+------+------+-------+ | 7 | wdf | NULL | | | 8 | cv | NULL | | | 15 | df | NULL | | +----+------+------+-------+ 3 rows in set (0.00 sec)
發現可以讀到了。
能讀到的原因是因為本事務對版本鏈內容進行了修改,所以就讀到了。
這個場景可能會出現在實際開發中,會比較懵,當然“事務A第三步”是筆者隨便模擬的,實際生產中直接拿大不到剛剛插入的id,所以應該是模糊(沒有確定行)update。所以在生產中還是要確定行去進行修改,避免出現這種比較難理解的場景。
雖然也可以使用lock in share mode
或者for update
讀當前藉助next-key
去實現不幻讀(第二次讀到第一次沒有讀到的行),還是需要根據具體業務選擇。
根據以上的場景,我們可以知道:
以上就是Mysql中mvcc各場景理解的詳細內容,更多關於Mysql mvcc場景的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45