One 、 classification
MySQL Different storage engines support different lock mechanisms , It is divided into watch level locks 、 Row-level locks 、 Page lock .MyISAM and MEMORY The storage engine uses table level locks （table-level locking）;BDB The storage engine uses page locks （page-level locking）, But it also supports table level locks ;InnoDB The storage engine supports both row level locks （row-level locking）, Table level locks are also supported , But the default is row level locking
Table lock ： Low overhead , Locked fast ; A deadlock will not occur ; Large locking size , The highest probability of lock collisions , Lowest degree of concurrency .
Row-level locks ： Spending big , Lock the slow ; A deadlock occurs ; Locking granularity minimum , The lowest probability of lock collisions , The highest degree of concurrency .
Page lock ： Cost and lock time are between table lock and row lock ; A deadlock occurs ; Lock granularity is between table lock and row lock , The concurrency is average
Two 、 Table lock
MySQL There are two modes of table level lock ： Table share read lock （Table Read Lock） and Table Write Lock （Table Write Lock）.
Yes MyISAM Read operation of table , Does not block other users' read requests for the same table , But it blocks write requests to the same table ; Yes MyISAM Write operation of table , Will block other users to read and write to the same table .
MyISAM In the execution of the query statement （SELECT） front , Will automatically lock all tables involved , Is performing an update operation （UPDATE、DELETE、INSERT etc. ） front , Will automatically lock the tables involved , This process does not require user intervention , therefore , Users generally don't need to use LOCK TABLE Order to MyISAM The watch is explicitly locked .
3、 ... and 、 Row-level locks
InnoDB And MyISAM There are two big differences ： One is to support affairs （TRANSACTION）; The second is the use of row level lock .
Row level locks also support read locks and write locks .
1、 How to lock ？
mysql InnoDB The default data modification statement of the engine ：update,delete,insert Will automatically add an exclusive lock to the data involved ,select Statement will not add any lock type by default , If an exclusive lock is added, it can be used select …for update sentence , Add shared lock to use select … lock in share mode sentence . Therefore, data lines with exclusive locks cannot be modified in other transactions , It can't pass for update and lock in share mode Query data by lock , But you can go straight through select …from… Query data , Because normal queries don't have any locking mechanism .
2、 Realization principle
InnoDB Row locking is achieved by locking the index entries on the index , This point MySQL And Oracle Different , The latter is realized by locking the corresponding data row in the data block .InnoDB This row lock implementation feature means ： Data is retrieved only through index conditions , And request sharing or exclusive lock ,InnoDB To use row level locks , otherwise ,InnoDB Table locks will be used ！
Four 、 Lock algorithm
record lock： Locks for individual records .
gap lock： Clearance lock , Lock a range , Not including the record itself
next-key lock：gap lock+record lock
Default isolation level （ Repeatable ） Next , The default addition is next-key lock（ To solve the problem of unreal reading ）, When the index contains unique attributes （ unique index , primary key ）, Will be relegated to record lock.
Under read committed isolation level , Plus record lock
1、 example 1
Now watch z, Yes a,b Two ,a It's the primary key , The whole table has only one primary key index . Now the record is as follows ：（1,1）（3,1）（5,3）（7,6）（10,8）
select * from z where b=3 for update
because b No index , So it's a full scan . Because locking is achieved by locking the index , Because there is no index , All will lock the whole table , That is, watch lock
2、 example 2
Now watch z, Yes a,b Two ,a It's the primary key index ,b Building a secondary index . Now the record is as follows ：（1,1）（3,1）（5,3）（7,6）（10,8）
select * from z where a=3 for update
The lock on the primary key index is record lock, To record （3,1） Lock
select * from z where b=3 for update
Because locking is achieved by locking the index . So we need to lock the primary key index and the secondary index , The primary key index will be locked by next-key The lock degenerates into record lock, The lock on the secondary index is next-key lock, The lock range is （1,3）、3、（3,6）
5、 ... and 、select Several types of
1、 Read the snapshot
The snapshot version is read , That's the historical version . ordinary SELECT That's snapshot reading
2、 The current reading
Read the latest version .
UPDATE、DELETE、INSERT、SELECT ... LOCK IN SHARE MODE、SELECT ... FOR UPDATE It's the current reading .
Default repeatable read isolation level , Using snapshot read
Read committed uses the current read
3、 Consistent non-locked reads
The realization principle is through MVCC Mechanism realization , If the read row is in update or delete in , Read operations don't wait on the line X The release of the lock , It's to read the snapshot data of the row .
MVCC, Multi version concurrency control technology . stay InnoDB in , Add two hidden columns after each row of records , Record creation version number and delete version number . By version number and line lock , So as to improve the concurrent performance of database system .
Consistent non lock read can greatly improve the concurrent performance
Different transaction isolation levels , The snapshot version read is different
Read committed isolation level , Always read the latest snapshot version . It may lead to phantom reading
Repeatable read isolation level , Always read the snapshot version read for the first time since the transaction started . It can avoid the production of unreal reading
4、 Consistency lock read
Under default configuration , Use a repeatable isolation level , Read data to take a consistent non lock read .
But in some scenarios, it is necessary to lock the read operation to ensure strict data consistency , At this time, you can explicitly lock the read record ：
select *** for update( Add... To the read record X lock ）
Lock the index record , In this case, follow UPDATE The lock case is the same
select *** lock in share model（ Add... To the read record S lock ）
Assume Shared locks for records , In this way , Other transactions can only be read and cannot be modified , Until the current transaction commits
If you think the article is good , At the end of the article ???? It's back , Remember to give it to me 「 give the thumbs-up 」 and 「 Looking at 」 Oh ~