• 6. The worst interview question ever asked.

1. Four isolation levels

READ UNCOMMITTED

Read uncommitted. The contents of uncommitted transactions can be seen by the current transaction. Generates dirty reads. It's not usually appliedCopy the code

READ COMMITTED

Read submitted. The current transaction can see the contents of the committed transaction. That is, the result of multiple select may be different. But normally it's normal. This is also the default database transaction level in a typical company.Copy the code

REPEATABLE READ

Repeatable. That is, the result of multiple select is the same. But there will be illusory situations. The illusory case is one in which the results of multiple select results differ. MySQL is RR by default.Copy the code

SERIALIZABLE

Serialization. Read operations do not block. Write operations will block. There will be no dirty read, phantom read.Copy the code

Why is the default transaction isolation level RC and not RR?

First, primary/secondary replication prior to MySQL5.0 has problems at the RC level. There is no detailed understanding of this problem. Therefore, the default transaction isolation level of MySQL database is RR 2, RR will generate gap locks, which is easier to generate lock wait in the process of frequent inserts and updates. Even deadlocks. RC, on the other hand, only locks the corresponding records (row locks) during updates and modifications. 3, RR is a gap lock when there is an index, RR is a full lock when there is no index.Copy the code

3. Solve phantom reading at RR level.

1. MVCC is only available at RR and RC levels. MySQL uses MVCC(Multi-version concurrency control) + gap lock at RR level to solve phantom read problems.Copy the code

4. Continue to record the principle of gap lock and MVCC.