preface
In the previous article, WE talked about MySQL’s RR isolation level using MVCC+ next-key Locks to solve the phantom read problem. Let’s take a closer look at what these two mechanisms are.
MVCC(Multi-version Concurrency Control)
Most of Mysql’s transactional storage engine implementations are not simple row-level locking. To improve concurrency, multiple versions of concurrency control (MVCC) have been implemented, including Oracle and PostgreSQL. Implementations vary, however.
MVCC is implemented by saving a snapshot of the data at a point in time. This means that no matter how long the implementation takes, everything will see the same data.
It is divided into optimistic concurrency control and pressimistic concurrency control.
How MVCC works:
The version number
Hidden columns
- Create version number: When creating a row of data, assign the current system version number as the creation version number.
- Delete version number: When deleting a row of data, assign the current system version number as the deleted version number. If the deleted version number of the snapshot is greater than the current transaction version number, the snapshot is valid; otherwise, the snapshot has been deleted.
REPEATABLE READ How MVCC works at isolation level:
1. SELECT
1. InnoDB only looks for rows whose version is earlier than the current transaction version. This ensures that the transaction reads rows that either existed before the transaction started or were inserted or modified by the transaction itself.
Ensure that rows read by the transaction are not deleted before the transaction beginsThe transaction will not see rows that have expired before the transaction begins.
2. INSERT
3. DELETE
4. UPDATE
Perform DELETE first and then INSERT.
Save both versions so that most operations are unlocked. Makes data manipulation simple, performs well, and guarantees that only the rows required by the composition will be read. The downside is that each row requires additional storage, more row checking, and some additional maintenance.
MVCC only works at COMMITTED READ and REPEATABLE READ isolation levels.
MVCC can be considered a variant of row-level locking, but it avoids locking in many cases and costs less. Although the implementation mechanism varies from database to database, most implement non-blocking reads (reads are unlocked and avoid unrepeatable reads and phantom reads), and writes lock only the necessary rows (writes must be locked otherwise data inconsistency will occur due to concurrent writes from different transactions).
Snapshot read and current read
Snapshot Read, and the way to read the latest version of the database is called
Current read.
1. Read the snapshot
With MVCC, data is read from a snapshot, which reduces the overhead of locking.
2. The current reading
Read is the latest data, need to lock. The first statement below requires an S lock, and all others require an X lock.
select * from table where ? lock in share mode;
select * from table where ? for update;
insert;
update;
delete;Copy the code
How to solve phantom reading
- Isolation level using serialized reads
- MVCC+next-key LOCKS: Next-key locks consist of record locks(index locks) and gap locks(gap locks), which lock not only the data that needs to be used, but also the data in the vicinity of that data.
InnoDB has three algorithms for row locking:
Record Locks
Gap Locks
SELECT c FROM t WHERE c BETWEEN 10 and 20 FOR UPDATE;
Next-Key Locks
MVCC does not solve the phantom problem, which next-key Locks are designed to solve. At the REPEATABLE READ isolation level, the phantom problem is solved using MVCC + next-key Locks.
It is a combination of Record Locks and Gap Locks, locking not only the indexes on a Record, but also the gaps between the indexes.
CREATE TABLE `test` (
`id` int(11) primary key auto_increment,
`xid` int.KEY `xid` (`xid`))ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into test(xid) values (1), (3), (5), (8), (11);Copy the code
Take a look at the picture below:
This SQL statement locks the range of (5,8) and the next key value range is (8,11), so any insert between 5 and 11 will be locked and wait. Insert 5,6,7,8,9,10 will be locked. Inserting values outside this range is fine.
summary
Under this article summarizes the MVCC and InnoDB three row lock algorithm, the principle of these knowledge belongs to the MySQL level, with the understanding, the use of MySQL can be more handy in the future, but as I am concerned about the last question why xid is 11 will not be blocked when there’s a little don’t understand, Innodb’s B+ tree is ordered, so it does not block subsequent inserts. I’ll have to go back and look at the detailed implementation description of Next-Key locks in mysq Tech Insider to make more sense.