当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Initialization layer implementation

Akamai-1.75 version-_ Abck parameter generation JS reverse analysis

Deep learning series 47: Super sub model real esrgan

Nacos适配oracle11g-修改Nacos源码

MySQL mvcc multi version concurrency control

PSP code implementation

MySQL(八) — 执行计划(Explain)详解

deeplab v3 代码结构图

407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)

PSP代码实现
随机推荐
Traversal of binary tree and related knowledge
【AI实战】XGBRegressor模型加速训练,使用GPU秒级训练XGBRegressor
In depth learning series 46: face image super score gfp-gan
300. longest increasing subsequence
char和varchar区别
【2022毕业季】从毕业到转入职场
如何优雅的快速下载谷歌云盘的大文件 (二)
A small method of debugging equipment serial port information with ADB
Mysql事务隔离级别
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
312. 戳气球
306. 累加数
318. 最大单词长度乘积
【AI实战】机器学习数据处理之数据归一化、标准化
900. RLE iterator
Pspnet complete code implementation
GIS实战应用案例100篇(七十九)-多规整合底图的制作要点
GINet
322. 零钱兑换
Regular expression graph and text ultra detailed summary without rote memorization (Part 1)