V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  JasonLaw  ›  全部回复第 35 页 / 共 36 页
回复总数  714
1 ... 27  28  29  30  31  32  33  34  35  36  
2020-07-05 23:28:27 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 是这样实现可重复读的
@louettagfh

1. 你说“一个事务可以有多条语句,s1 创建事务 t1 用 record lock 锁住了这条 record, 但它执行 t1 后面的语句时,这把 record lock 已经被放开了. 其他事务的是可以修改的 id 为 5 的 record.”,可以用实例展现一下吗?
2. 你说“MySQL 如何实现可重复读? 利用 MVCC”,其实我讲的主要是 locking read 的可重复读,而不是 consistent nonlocking read 的可重复读,可以标题有点误导吧,我会修改一下。
3. 你说创建事务时就创建 read view,这个应该不对吧?在 https://dev.mysql.com/doc/refman/8.0/en/innodb-consistent-read.html 中,它说“If the transaction isolation level is REPEATABLE READ (the default level), all consistent reads within the same transaction read the snapshot established by the first such read in that transaction.”,“With READ COMMITTED isolation level, each consistent read within a transaction sets and reads its own fresh snapshot.”,不管是哪个级别,都不是创建事务时就去建立 snapshot 。
2020-07-05 18:59:57 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 是这样实现可重复读的
@lhx2008 也许标题应该改一下😅,因为文章没有讲到 consistent nonlocking read,只是讲到了 locking read,虽然知道 MVCC 相关理论,但是不太了解 InnoDB 具体是怎么实现的。有空了解了之后再补充。
2020-07-05 13:54:58 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
上面那篇文章是不正确的,我准备取消那篇文章的发布。不过我在这里贴出两个问题,[mysql - Is insert intention lock truly a gap lock? - Stack Overflow]( https://stackoverflow.com/questions/62712571/is-insert-intention-lock-truly-a-gap-lock)和[mysql - Does transaction release insert intention lock after insertion? - Stack Overflow]( https://stackoverflow.com/questions/62737123/does-transaction-release-insert-intention-lock-after-insertion)。欢迎大家解答这两个问题,谢谢。
2020-07-05 13:23:17 +08:00
回复了 JasonLaw 创建的主题 数据库 InnoDB LOCK_MODE X,GAP,INSERT_INTENTION 到底是什么?
@limuyan44 我不太明白你是怎么从 https://dev.mysql.com/doc/refman/5.7/en/innodb-locking.html 得知 X,GAP,INSERT_INTENTION 是什么类型的锁的,难道是从`trx id 8731 lock_mode X locks gap before rec insert intention waiting`得知?
2020-07-04 23:27:19 +08:00
回复了 JasonLaw 创建的主题 数据库 InnoDB LOCK_MODE X,GAP,INSERT_INTENTION 到底是什么?
@limuyan44 你说官方文档有,可以发一下相关的链接吗?我实在是找不到有官方文档说这个的。
具体原因我也不太清楚,我猜测数据是 float 类型之类的,因为自己尝试过,float 类型的时候,`lock data`就会是这种格式的数据,但是如果数据是 int 类型的话,`lock data`会是人类可读的(比如说 8 )。
2020-07-03 18:15:46 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@luckyrayyy @showhand @taoprogramer @TouchQQ @makese @salamanderMH @qsfun

上一条有点问题,再打扰一次🤣

插入意向锁真的是间隙锁吗? - 简书 https://www.jianshu.com/p/a449eda843b9
2020-07-03 18:13:35 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@luckyrayyy @showhand @taoprogramer @TouchQQ @makese @salamanderMH @qsfun 大家好,查阅了很多资料之后,我整理[插入意向锁真的是间隙锁吗? - 简书]( https://www.jianshu.com/p/a449eda843b9)这篇文章,欢迎查看,如果有错误的话,欢迎指出。
2020-07-02 17:42:58 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@TouchQQ 你说插入意向锁是用来解决并发问题的,它解决的是什么并发问题呢?还有“首先插入意向锁是一种类型的间隙锁,锁模式是 IX, 而一般说的间隙锁( Gap Locks )锁模式是 X”这句话,我不太理解,可以具体解释一下吗?
2020-07-02 15:26:04 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@salamanderMH 是的,我没有想到这点,因为一直被 gap lock 影响,让我忘记了它是一个 intention lock 。如果是这样子的话,为什么说插入意向锁时间隙锁呢?不是应该是表级别的 intention exclusive lock 吗?
2020-07-02 15:21:04 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@makese 我有点明白了,插入意向锁说到底还是一个意向锁,而意向锁是表级别的,所以在获取到“插入行的独占锁”之前,会先获取插入意向锁。如果一个事务获取到了插入意向锁,那么其他事务就不能够获取到表级别的 write 锁(不太明白为什么可以获取到表级别的 read 锁,可能是为了更好的并发吧)。说到底还是跟多粒度锁有关。

我尝试了以下 schedule,的确跟我说的一样。`session 1: start transaction; create table t (k int, key ix_t_k (k)); insert into t values(5); session 2: start transaction; lock tables t write;`,session 2 执行`lock tables t write`时会阻塞。

插入意向锁是否只有这么一个作用?还有其他的作用吗?

如果是这样子的话,为什么说插入意向锁时间隙锁呢?不是应该是表级别的 intention exclusive lock 吗?
2020-07-02 14:13:37 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@makese “你这个问题是因为 s 是行锁”,你说的这个问题是 https://www.v2ex.com/t/686462#r_9191604 吗?我不明白你所说的跟我的问题有什么关系?可以详细解释一下吗?
2020-07-02 12:36:02 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@taoprogramer @TouchQQ 插入意向锁用来解决“phantom read”?如果是的话,你可以执行以下代码(按顺序执行)。`session 1: start transaction; create table t (k int, key ix_t_k (k)); insert into t values(5); insert into t values(10); insert into t values(8); session 2: start transaction; insert into t values(7);`。在 session 1 执行`insert into t values(8)`时,会获取到插入意向锁(5, 10),如果它是用来解决“phantom read”的话,session 2 就不能成功执行`insert into t values(7)`了。间隙锁才是用来阻止其他事务插入,而不是插入意向锁。
2020-07-02 11:19:23 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@showhand 但是 insert intention lock 存在的意义是什么呢?如果“插入时不需要在获取独占锁前获取插入意向锁,而是直接获取独占锁”,会怎样呢?获取独占锁时,事务难道不会因为其他事务拥有的 gap lock 冲突而等待吗?
2020-07-02 11:05:23 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@luckyrayyy 我的疑惑更多是关于“插入意向锁存在的意义是什么?它的作用是什么呢?能够防止什么操作并发执行?”,如果可以用实例解释就最好了。
2020-07-02 11:03:03 +08:00
回复了 JasonLaw 创建的主题 数据库 MySQL 插入意向锁的作用是什么?
@luckyrayyy 在[https://dev.mysql.com/doc/refman/8.0/en/innodb-locking.html]( https://dev.mysql.com/doc/refman/8.0/en/innodb-locking.html)中,介绍了意向锁和插入意向锁。它说`Intention locks are table-level locks that indicate which type of lock (shared or exclusive) a transaction requires later for a row in a table.` & `An insert intention lock is a type of gap lock set by INSERT operations prior to row insertion.`。我同意你前面所说的,但是对于“然后其他事物想锁表的时候看到有个意向锁,就得阻塞等着”,我并不同意。在 http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/PPT-dir/ch15.ppt 里的第 25 页,有一个兼容性矩阵,比如说一个事务拥有 IS 类型表级意向锁,另外一个事务还是可以获取到 S 类型的表级锁的。
@lpts007 书籍的版本是第六版(中文版和英文版都没有包含 commited 限定),应该是书籍的小错误。谢谢啦。
@lpts007 谢谢。不过书中所说的跟 PPT 所说的不太一样。书中的"redoes all the actions since the most recent dump occurred"不是应该换成"redo all transactions that committed after the dump"吗?

书中所说的:
To recover from the loss of nonvolatile storage, the system restores the database to disk by using the most recent dump. Then, it consults the log and **redoes all the actions since the most recent dump occurred**.

PPT 所说的:
To recover from disk failure
1. restore database from most recent dump.
2. Consult the log and **redo all transactions that committed after the dump**.
@lpts007 你看看 https://www.db-book.com/db6/slide-dir/PPT-dir/ch16.ppt 的 25 页,并不是你所说的那样。

Recovery from failure: Two phases
1. Redo phase: replay updates of all transactions, whether they committed, aborted, or are incomplete
2. Undo phase: undo all incomplete transactions
@lpts007 文章所说的是“然后,根据日志,重做最近一次转储后所做的所有动作。”,是所有的动作,并不是已结束的事务所做的动作。
1 ... 27  28  29  30  31  32  33  34  35  36  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3264 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 22ms · UTC 14:17 · PVG 22:17 · LAX 07:17 · JFK 10:17
Developed with CodeLauncher
♥ Do have faith in what you're doing.