数据库
首页 > 数据库> > 可重复读取和可序列化的MySQL InnoDB实现之间的实际区别是什么

可重复读取和可序列化的MySQL InnoDB实现之间的实际区别是什么

作者:互联网

根据SQL标准,可重复读取应防止模糊读取和脏读取,而可序列化也应防止幻像读取.

根据MySQL documentation

By default, InnoDB operates in REPEATABLE READ transaction isolation
level. In this case, InnoDB uses next-key locks for searches and index
scans, which prevents phantom rows (see Section 14.2.2.5, “Avoiding
the Phantom Problem Using Next-Key Locking”).

因此,如果“可重复读取”也可以防止幻像读取,那么Serializable可提供什么回报?

是Serializable防止写偏斜还是防止读偏斜,而Repeatable Read却没有?

解决方法:

答案也可以在mysql documentation中找到,引用:

This level is like REPEATABLE READ, but InnoDB implicitly converts all plain SELECT statements to SELECT … LOCK IN SHARE MODE if autocommit is disabled. If autocommit is enabled, the SELECT is its own transaction. It therefore is known to be read only and can be serialized if performed as a consistent (nonlocking) read and need not block for other transactions.

当采用两阶段锁定实现时,可序列化的事务调度可以防止读写时滞.这就是它在使用锁定的SQL Server或在PostgreSQL上使用其可序列化快照隔离的方式工作的方式.

如果在正在读取的任何资源上获取了共享锁,则也将防止读取偏斜和写入偏斜.

标签:isolation-level,transactions,mysql
来源: https://codeday.me/bug/20191119/2037316.html