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

What's the actual difference between MySQL InnoDB implementations of Repeatable Read and Serializable

根据SQL标准,Repeatable Read应该防止模糊读和脏读,而Serializable也应该防止幻读。

根据 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”).

那么如果可重复读也可以防止幻读,那么在 return 中可序列化提供什么?

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 服务器上使用锁定或在 PostgreSQL 上使用其可序列化快照隔离的工作方式。

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