Locks in InnoDB (1)

Get into the habit of writing together! This is the 7th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Whenever there is concurrency, locks must be discussed. The purpose of locks is to maintain consistency, and different lock implementations have very different effects on performance. For example, MyISAM uses table locks, which will lead to lower concurrent insert performance.

Shared locks and exclusive locks

In fact, it is the read lock and write lock we learned.

Consistent non-locking read

The InnoDB storage engine reads the data of rows in the database at the current execution time by means of row multi versioning. When the read row has an exclusive lock, the storage engine will read its corresponding snapshot data.

Snapshots are implemented through undo segments. And undo is used to roll back the data in the transaction, so the snapshot data itself has no additional overhead. In addition, reading snapshot data does not require locking, because no transaction needs to modify historical data.

Snapshot data is the historical version of the current row data. A row of data can have many historical versions. When the transaction isolation level is committed read, the non-locking read is the latest version. When the transaction isolation level is repeatable read, the non-locking The read is the version of the row data at the beginning of the transaction.

Consistent lock read

For some cases, such as select For UPDATE, it needs to be locked when reading.

Use of locks during auto-increment

Before Mysql 5.1.22, the implementation of self-growth was through the AUTO-INCLocking method of table locks. In order to ensure the concurrency efficiency as much as possible, this table lock will be released after the insertion is successful instead of waiting for the transaction to complete. But in fact, the performance of concurrent insertion will still be affected, so we need a lightweight self-growth implementation mechanism.

We first classify the insertion as followsimage.png

For "simple inserts", an operation that increments an in-memory counter using a mutex. For "bulk inserts", use AUTO-INCLocking again.

Guess you like

Origin juejin.im/post/7084625448944009223