当前位置:网站首页>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 .
边栏推荐
- Specific help of OSI layered model to work
- Analyzing the creation principle in maker Education
- 306. Addenda
- How to achieve efficient network information dissemination
- csrf攻击在laravel中如何解决
- 295. 数据流的中位数
- Interpreting the spirit of unity and cooperation in maker Education
- 什么是分布式?
- 406-双指针(27. 移除元素、977.有序数组的平方、15. 三数之和、18. 四数之和)
- 312. 戳气球
猜你喜欢
随机推荐
Redis设置密码
'Latin-1' codec can't encode characters in position 103-115: body ('string of Chinese ') is not valid Latin-1
Mysql事务隔离级别
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
Deeplab V3 code structure diagram
899. ordered queue
Lombok的使用
Arthas thread command locates thread deadlock
318. 最大单词长度乘积
【博弈论】基础知识
A small method of debugging equipment serial port information with ADB
TensorFlow中的数据类型
301. delete invalid brackets
Interpreting the spirit of unity and cooperation in maker Education
407-栈与队列(232.用栈实现队列、225. 用队列实现栈)
306. 累加数
Technical article writing guide
MySQL(八) — 执行计划(Explain)详解
C language learning summary
什么是分布式?









