当前位置:网站首页>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 .
边栏推荐
- 20220620 uniformly completely observable (UCO)
- 312. 戳气球
- MySQL Niuke brush questions
- 309. the best time to buy and sell stocks includes the freezing period
- 307. area and retrieval - array modifiable
- 301. delete invalid brackets
- 897. incremental sequential search tree
- 【***数组***】
- 897. incremental sequential search tree
- 数据库原理实验测试题,关于图书分类表
猜你喜欢

406-双指针(27. 移除元素、977.有序数组的平方、15. 三数之和、18. 四数之和)

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

Project_ Filter to solve Chinese garbled code

How to migrate virtual machines from VirtualBox to hype-v

Configuration and compilation of mingw-w64, msys and ffmpeg

Akamai-1.75 version-_ Abck parameter generation JS reverse analysis
![[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed](/img/aa/0189beb065fa0d4b625390793cb79b.png)
[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed

Deeplab V3 code structure diagram

Advanced drawing skills of Excel lecture 100 (VIII) -excel drawing WiFi diagram

Learning and using quartz scheduling framework
随机推荐
20220620 uniformly completely observable (UCO)
20220621 Three Conjugates of Dual Quaternions
896. monotonic sequence
Analysis of personalized learning progress in maker Education
C DPI adaptation problem
PSP代码实现
Spock约束-调用频率/目标/方法参数
300. longest increasing subsequence
Mysql数据库的几个特点
Deep learning series 47: Super sub model real esrgan
Arthas-thread命令定位线程死锁
SSM整合
Verilog syntax explanation
306. Addenda
318. maximum word length product
对二进制的某一位操作
312. 戳气球
Spock-sub打桩
Regular expression graph and text ultra detailed summary without rote memorization (Part 1)
295. median data flow