当前位置:网站首页>MySQL lock details

MySQL lock details

2022-06-27 08:16:00 Little moon 6

MySQL Lock classification

 

A lock is a mechanism by which a computer coordinates multiple processes or threads to access a resource concurrently .

Read lock and write lock :

Read the lock

to student Add read lock

lock table student read;

Unlock unlock tables;

When this thread locks the table

When other threads operate

You can see , When it is modified, it will enter the blocking state

After unlocking

Modification successful :

I've been waiting for 2 min 54.03 sec

It should be mentioned that , If student After the lock , You can't look up other tables , It throws an exception

Write lock

When other threads operate

Both read and write will be blocked

When the write lock is released

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql>
 

 

Conclusion of the case
MyISAM In the execution of the query statement (SELECT) front , Will automatically lock all tables involved , Before adding, deleting, or modifying , Will automatically lock the tables involved .
MySQL There are two modes of table level lock : 
Table share read lock (Table Read Lock)
Table Write Lock (Table Write Lock)
Combined with the above table , So for MyISAM Table operation , There will be the following :
1、 Yes MyISAM Read operation of table ( Add read lock ), It will not block other processes' reading requests to the same table , But it blocks write requests to the same table . Only when the read lock is released ,
To perform other process write operations .
2、 Yes MyISAM Write operation of table ( Add write lock ), Will block other processes to read and write to the same table , Only when the write lock is released , Will execute the read / write operations of other processes
do .
 

In short , It's just that reading locks block writing , But it doesn't block reading . The write lock blocks both reading and writing .

 

Business :

The following row of locks will be related to now. Let's talk about it in advance


The business is made up of a group of SQL A logical processing unit made up of statements , The transaction has the following 4 Attributes , Usually referred to as transaction ACID attribute .
Atomicity (Atomicity) : A transaction is an atomic unit of operation , Its modification of data , Or do it all , Or none of them .
Uniformity (Consistent) : At the beginning and end of the transaction , All data must be kept in one To state . This means that all relevant data rules must be applied to the modification of the transaction , To maintain the integrity of the data ; At the end of the transaction , All internal data structures ( Such as B Tree index or double linked list ) They have to be right .
Isolation, (Isolation) : The database system provides a certain isolation mechanism , Ensure that transactions are not affected by external concurrent operations “ Independent ” Environmental execution . This means that the intermediate state in the transaction process is not visible to the outside , vice versa .
persistence (Durable): After the transaction completes , Its modification of data is permanent , Even if there is a system failure, it can keep .


Problems caused by concurrent transaction processing

When two or more transactions select the same row , When the row is then updated based on the originally selected value , Because each transaction does not know the existence of other transactions , There will be lost updates , The result is that the last update overwrites the updates made by other transactions .
        for example , Two programmers modify the same java file . Each programmer changes its copy independently , Then save the changed copy , This overwrites the original document . Finally, the editor who saved a copy of his changes overwrites the changes made by the previous programmer .
If before a programmer completes and commits a transaction , Another programmer can't access the same file , This problem can be avoided .

Dirty reading

      A transaction is modifying a record , Before this transaction is completed and committed , The data of this record is inconsistent ; At this time , another - A transaction also reads the same record , If not controlled , The second transaction reads these “ dirty ” data , And make a further step accordingly Step processing , Uncommitted data dependency will be generated . This phenomenon is vividly called ” Dirty reading ”.
      Sentence : Business A Transaction read B Data that has been modified but not yet submitted , On the basis of this data, we have done the operation . here , If B Transaction rollback ,A Read . Invalid data for , Does not meet conformance requirements .

 

Dirty reading : Read uncommitted transactions ( Has been modified to submit ), Then a transaction rollback occurs

Business 1, modify number=100
Business 2, Read number=100
Business 1, Exception rollback number Change to the original 50
Business 2, Reading is dirty data

 

It can't be read repeatedly

        A time after a transaction reads some data , Read the previously read data again , But the data they read has changed 、 Or some records have been deleted ! This phenomenon is called “ It can't be read repeatedly ”.

It can't be read repeatedly : Read the committed new transaction , Refers to the update operation (update)

Business 2, Read number=100
Business 1, modify number=50
Business 2, When reading again number=50

Nonconformance isolation    Business A Transaction read B Revised data submitted

Fantasy reading

A transaction re reads the previously retrieved data according to the same query criteria , However, it is found that other transactions have inserted new data satisfying their query criteria , This phenomenon is called Unreal reading     for example : Read the new data submitted , As read A The element is inserted after B
In a word : Business A Transaction read B New data submitted by body , Nonconformance isolation .
Many say : It's similar to dirty reading ,
Dirty reading is business B It's modified the data ,
Unreal reading is business B There's new data .

The isolation level of the transaction

Dirty reading ”、“ It can't be read repeatedly ” and “ Fantasy reading ”, In fact, it's all about database read consistency , The database must provide some transaction isolation mechanism to solve .

The more strict the transaction isolation of the database , The less side effects , But the more it costs , Because transaction isolation essentially makes transactions to a certain extent “ Serialization ” Conduct , This is obviously related to “ Concurrent ” Is contradictory . meanwhile , Different application pairs read one The requirements for consistency and transaction isolation are also different , For example, many applications are right for “ It can't be read repeatedly ” and “ Fantasy reading ” It's not sensitive , Perhaps more concerned about the ability of concurrent data access .

mysql The default is repeatable reading

Create tables to base row locks

mysql> create table test_innodb_lock(a int(11),b varchar(16))engine=innodb;
Query OK, 0 rows affected (0.24 sec)

mysql> insert into test_innodb_lock values(1,'b2');
Query OK, 1 row affected (0.31 sec)

mysql> insert into test_innodb_lock values(2,'b1');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_innodb_lock values(3,'4000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_innodb_lock values(4,'5000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_innodb_lock values(5,'5000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_innodb_lock values(6,'6000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_innodb_lock values(7,'7000');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_innodb_lock values(8,'8000');
Query OK, 1 row affected (0.01 sec)

mysql> create index test_innodb_a_ind on test_innodb_lock(a);
Query OK, 0 rows affected (0.37 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create index test_innodb_b_ind on test_innodb_lock(b);
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from test_innodb_lock;
+------+------+
| a    | b    |
+------+------+
|    1 | b2   |
|    2 | b1   |
|    3 | 4000 |
|    4 | 5000 |
|    5 | 5000 |
|    6 | 6000 |
|    7 | 7000 |
|    8 | 8000 |
+------+------+
8 rows in set (0.00 sec)

 

Row lock

InnoDB( The row lock is enabled by default , Default or auto submit , Turn off auto submit during Demo set autocommit=0;)

deviation InnoDB Storage engine , Spending big , Lock the slow ; A deadlock occurs ; Locking granularity minimum , The lowest probability of lock collisions , The highest degree of concurrency .


InnoDB And MyISAM There are two big differences : One is to support affairs (TRANSACTION) ; The second is the use of row level lock

Thread one :

Thread two

Find is found a=3 Corresponding b be equal to 4000

Because no submission commit;

below commit Then you can find the corresponding data

Left thread one , Thread 2 on the right ( must commit, Otherwise, it cannot be read , because Mysql The default transaction isolation level is repeatable read )

When a pair of threads a be equal to 3 When modifying , Thread 2 is modified again a be equal to 3 It will enter the blocking state

When the thread reads a be equal to 3 When modifying , Thread 2 can also be used for a==2 modify ;

When the index fails, the row lock becomes a table lock

The two indexes we created at the beginning test_innodb_a_ind and test_innodb_b_ind

       create table test_innodb_lock(a int(11),b varchar(16))engine=innodb;

If we use b When removing double quotation marks during query , The index will fail ( Because row locks can only be created on the index )

for example :

Return to the right topic :

because b The original type is b(varchar), Mistake b Take it for granted. int Types of queries ( Forget to use double quotation marks ),test_innodb_b_ind The index will fail , So when thread 2 modifies other rows, it will also be blocked

Clearance lock

Although there is no a==2 The data of , But it belongs to the clearance lock , So thread 2 adds a==2 It's also blocked

 

What is clearance lock


        When we retrieve data using range conditions rather than equality conditions , And request sharing or exclusive lock ,InnoDB It will lock the index entries of existing data records that meet the conditions ; For records where the key value is in the condition range but does not exist , be called “ The gap (GAP)”,
        InnoDB It's also about this “ The gap ” Lock , This kind of lock mechanism is called gap lock (Next-Key lock ) .


harm


        because Query If you have passed the scope search during execution , It will lock all the key values in the whole range , Even if the key does not exist . The gap lock has a fatal weakness , After locking a range key value , Even if some key values do not exist, they will be locked innocently , When locking, you cannot insert any data within the lock key value range . In some scenarios, this can be very detrimental to performance
 

How to lock a specific row of data

( Such as a==3)

The premise is that the index is required , No index is a table lock , Thread 2 access is also blocked  

The following figure shows that locking is successful ,

Line lock summary :

Innodb The storage engine implements row level locking , Although the performance loss caused by the implementation of locking mechanism may be higher than that of table level locking - some , But it's much better than MyISAM The watch level is locked . When the system concurrency is high ,Innodb The overall performance and MyISAM There will be obvious advantages in comparison .|
however ,Innodb The line of Level locking also has its weak side , When we don't use it properly , May let Innodb The overall performance of MyISAM high , Even worse .
 

How to analyze row locking


clear through InnoDB_ row_ lock State variables are used to analyze the contention of row locks on the system
mysq|l> show status like 'innodb_row_lock%’
 

The description of each state quantity is as follows :
Innodb_ row_lock__current_waits: The number of currently waiting locks ;
Innodb_ row_ lock_time: The total length of time from system startup to lock up ;
Innodb_row_ lock_time_avg: The average time it takes to wait ;
Innodb_row_lock_timne_max: Time spent waiting for the most frequent time from system startup to now ;
Innodb_row_lock_waits: The total number of times the system has been waiting since it was started ;
For this 5 Two state variables , The more important thing is
Innodb_row_lock_time_avg ( Average waiting time ),
Innodb_row_lock_waits ( Total waiting times )
Innodb_row_lock_time ( The total waiting time ) These three items .

Especially when waiting times are high , And every time the waiting time is not small , We need to analyze why there are so many waits in the system , Then, according to the results of the analysis, we start to specify the optimization plan .


Optimization Suggestions

 

As far as possible, all data retrieval should be done through index , Avoid upgrading non indexed row locks to table locks .
Design index reasonably , Try to narrow down the range of locks
As few search conditions as possible , Avoid gap locks
Try to control the transaction size , Reduce the amount of locked resources and the length of time
As low level transaction isolation as possible
 

 

 

What are the types of locks


Lock based attribute classification : Shared lock 、 Exclusive lock .


Lock based granularity classification : Row-level locks (INNODB)、 Table lock (INNODB、MYISAM)、 Page level lock (BDB engine )、 Record locks 、 Clearance lock 、 Temporary key lock .


Lock based state classification : Intention sharing lock 、 Intention exclusive lock .


   ●    Shared lock (Share Lock)


        Shared lock is also called read lock , abbreviation S lock ; When a transaction adds a read lock to the data , Other transactions can only lock the data , You can't write lock the data , Until all read locks are released, other transactions cannot add write locks to them . The shared lock feature is mainly to support concurrent data reading , Modification is not supported when reading data , Avoid the problem of repeated reading .


   ●    Exclusive lock (eXclusive Lock)


          Exclusive lock is also called writing lock , abbreviation X lock ; When a thing When writing lock is applied to data , Other requests will no longer be able to lock the data , Until the lock is released , Only other transactions can lock the data . The purpose of exclusive lock is to modify data , Others are not allowed to modify , And no one else is allowed to read . Avoid the problem of dirty data and dirty reading .


   ●    Table locks


          Watch lock means that the whole watch is locked when locked , When the next transaction accesses the table , You must wait for the previous transaction to release the lock before you can access the table ;
        characteristic : Large grain size , Locking is simple , Easy to conflict ;


  ●    Row lock  


        Row lock refers to locking one or more rows of records in the table when locking , When other transactions access the same table , Only locked records cannot be accessed , Other records can be accessed normally ; 
        characteristic : Small particle size , Locking is more troublesome than watch locking , Not easy to study , It supports higher concurrency than table locks ;


  ●    Record locks (Record Lock)


        Record lock also belongs to a kind of row lock , Only the scope of the record lock is only a record in the table , A record lock means that a small service locks only one record of the table after locking .
        Accurate condition hit , And the hit condition field is the only index
        After adding a record lock, the data can avoid the problem of repeated reading when the data is modified during query , It also avoids the dirty read problem that the modified transaction is read by other transactions before it is committed .
 

  ●    Page locks


      Page level lock is MySQL A lock whose granularity is between row level lock and table level lock . The watch lock is fast , But there are many conflicts , Less line level conflict , But the speed is slow. . So I took a compromise page level , Lock an adjacent set of records at a time .
characteristic : 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


  ●    Clearance lock (Gap Lock)


          It belongs to a kind of row lock , The clearance lock is used to lock a certain interval of the table record after the small service is locked , When a table is adjacent ID If there is a gap between them, it will form . Intervals , Follow the principle of left opening and right closing .
          Range query and query Miss records , The query condition must hit the index 、 Clearance locks only appear in REPEATABLE READ ( Repeated reading ) At the transaction level of .
          The trigger condition : Prevent phantom reading problems , When transactions are concurrent , If there is no clearance lock , The following problem will occur , In the same business ,A The results of two transaction queries will be different .
          For example, the data in the table ID by 1,4,5,7,10 , Then the following gaps will be formed ,-n-1 Section ,1-4 Section ,7-10 Section ,10-n Section (-n Represents negative infinity , n Represents positive infinity )


  ●  Temporary lock (Next-Key Lock)


        It also belongs to a kind of row lock , And it's INNODB The row lock default algorithm of , In summary, it is a combination of record lock and clearance lock , The temporary key lock will lock the records found , At the same time, all the gap spaces within the range of query will also be locked , Then it will lock the next adjacent interval
        The trigger condition : Range query and hit , Query hit index .  
          Combine the characteristics of recording lock and clearance lock , Temporary key lock avoids dirty reading in range query 、 Repeated reading 、 The problem of unreal reading . After the temporary key lock is added , Data is not allowed to be modified or inserted in the range range .
 

Intent locks

If it's a business A After locking is successful, set a status to tell the following people , Someone has added an exclusive lock to the rows in the table , You can't put shared or exclusive locks on entire table , Then, the person who needs to lock the whole table only needs to obtain this state to know whether he can lock the table , Avoid the entire index | Each node of the tree is scanned for locking , And this state is intention lock .
   ●      Intention sharing lock
          Before a transaction attempts to apply a shared lock to the entire table , First, you need to obtain the intentional co word lock of the table .
   ●      Intention exclusive lock
          Before a transaction attempts to lock the entire table , First, you need to obtain the intention exclusive lock of the table .
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原网站

版权声明
本文为[Little moon 6]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270806051182.html