当前位置:网站首页>MySQL (V) - locks and transactions

MySQL (V) - locks and transactions

2022-06-23 07:17:00 The bad guy is stupid

MySQL Lock of

Mysql Different storage engines support different locking mechanisms . such as ,MyISAM and MEMORY The storage engine uses table level locks (table-level locking);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 .

MySQL Lock classification

  • 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 (cap lock , Clearance 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 .

Only from the point of view of lock :
Table-level locking is better suited for query-oriented locking , Only a small number of applications update data by index criteria , Such as OLAP System .
Row level lock is more suitable for a large number of concurrent updates of different data according to index conditions , At the same time, there are applications of concurrent queries , Like some online transactions (OLTP) System .

Table locks

MySQL There are two modes of middle meter level lock :

  • Table share read lock
  • Table Write Lock
lock usage
Read the lock

Lock :lock table < Table name > read

Delete lock :unlock tables

Write lock

Lock :lock table < Table name > write

Delete selected :unlock tables

Be careful : 

  • Yes MyISAM Read operation of table , Does not block other users' read requests for the same table .
  • Yes MyISAM Read operation of table , It won't block the current session Read the table , When the table is modified, an error will be reported
  • One session Use LOCK TABLE Order to the table f With a reading lock , This session You can query records in a locked table , But updating or accessing other tables will prompt an error ;
  • Yes MyISAM Write operation of table , Will block other users to read and write to the same table ;
  • Yes MyISAM Write operation of table , At present session This table can be CRUD, However, an error will be reported when operating on other tables .

Row lock

  • Shared lock ( Read the lock ): When a transaction reads locks on a number of rows , Allow other transactions to read these lines , But it's not allowed to write , No other     The transaction locks these rows exclusively , But the lock is allowed to be read .
  • Exclusive lock ( Write lock ): When a transaction writes a lock to several , Other transactions are not allowed to write , But allowed to read . No other transaction is allowed to lock these lines . Including write lock .
lock usage
Shared lock

usage :lock in share mode

Such as :select * from table where Conditions lock in share mode;

Exclusive lock

usage :for update

Such as :select * from table where Conditions for update

  Be careful :

  • Two transactions cannot lock the same index .
  • insert ,delete , update It's automatically locked in transactions by default .
  • A row lock must have an index to implement , Otherwise, it will lock the whole meter automatically , So it's not a row lock .

Business  

The nature of transactions

  • Atomicity (atomicity): A transaction is an indivisible unit of work , All operations included in a transaction are either done , Either not .
  • Uniformity (consistency): The transaction must be to change the database from one consistency state to another . Consistency is closely related to atomicity .
  • Isolation, (isolation): The execution of one transaction cannot be interfered by other transactions . That is, the operations and data used within a transaction are isolated from other concurrent transactions , Transactions that execute concurrently cannot interfere with each other .
  • persistence (durability): Once a transaction is committed , Its changes to the data in the database should be permanent . Subsequent operations or failures should not have any impact on it .

Database transaction isolation level  

Isolation, (isolation): Isolation requires a transaction to modify the data in the database , It is invisible to other transactions until the commit is completed .

Problems caused by transaction concurrency :

  • Dirty reading : Business A Read transaction B Updated data , then B Rollback operation , that A The data read is dirty
  • It can't be read repeatedly : Business A Read the same data multiple times , Business B In the transaction A During multiple reads , The data has been updated and submitted , Cause transaction A When reading the same data multiple times , result atypism .
  • Fantasy reading : System administrator A Change the scores of all students in the database from specific scores to ABCDE Grade , But the system administrator B At this time, a specific score record was inserted , When the system administrator A After the change, I found that there is another record that hasn't been changed , It's like an illusion , This is called Unreal reading .
Transaction isolation level Dirty reading It can't be read repeatedly Fantasy reading
Read uncommitted (read-uncommited) yes yes yes
Read submitted (read-commited) no yes yes
Repeatable (repeatable-read) no no yes
Serialization (serializable) no no no

When the transaction isolation level is repeatable , If there is an index ( Include primary key index ) When , Update data on index columns , There will be gaps between locks 、 Row lock 、 Page lock problem , To lock up some lines ; If there is no index , When updating data, the entire table will be locked .

When the transaction isolation level is serialization , Reading and writing data will lock the whole table .

  Higher isolation level , The more data integrity and consistency can be guaranteed , But the greater the impact on concurrent performance , For most applications , Priority can be given to setting the isolation level of the database system to Read Committed, It avoids dirty reads , And it has better concurrent performance .

 

原网站

版权声明
本文为[The bad guy is stupid]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230623048312.html