当前位置:网站首页>MySQL table mechanism
MySQL table mechanism
2022-06-10 23:23:00 【Be considerate ZY】
Understanding of lock
1.1 The explanation of the lock
|
1.2 The importance of locks
|
1.3 The disadvantages of locks
|
1.4 A simple example
|
The type of lock
2.1 Table locks
species
|
|
Storage engine default lock
|
characteristic
|
Conclusion
|
Suggest
|
2.2 Row lock
species
|
|
Features storage engine default lock
|
|
The problem of transaction concurrency
The problem of transaction concurrency Dirty reading Easiest to understand . Another transaction modified the data , But has not yet submitted , And in this matter SELECT You'll read the uncommitted data . If , Go to the canteen for lunch , I saw a seat was small by my classmates Q Got it , I think this seat has been occupied , Just turn around and find another seat . lo , This classmate is young Q Got up and left . The facts : The student is young Q I just sat down temporarily , Not “ Submit ”. Don't repeat It's also two transactions operating on the same data , If you read some data at the beginning of a transaction , At this point, another transaction modifies the data , When the transaction reads this data again, it finds that it has changed , The data results read twice are inconsistent , There's no way to read a piece of data over and over again . If , Go to the canteen for lunch , See a seat is empty , He went to fetch rice , When I came back, I found that this seat was small by my classmates Q Occupied . Fantasy reading At the beginning of the transaction, no data was found according to a certain query condition , The result is due to another transaction , When I went to check again, I found the data , It's like hallucination , It's called Unreal reading . If , Go to the canteen for lunch , See a seat is empty , He went to fetch rice , After you come back , Found that these seats are still empty ( Repeated reading ), Steal joy . When I came to the front and was just about to sit down , But there was a dinosaur girl , Seriously affect appetite . It seems that the empty seat I saw before is “ The phantom ” equally . |
2.3 Four isolation levels of transactions
MySQL InnoDB There are four levels of transaction isolation , The default is “ Repeatable ”(REPEATABLE READ).
Uncommitted read (READ UNCOMMITTED)
Read uncommitted means that a transaction can be read without being committed , Obviously, this isolation level will lead to reading other uncommitted data , Once further processing is done based on the data read , Another transaction eventually rolls back the operation , Then the data will be messed up , And it's hard to track . On the whole , Reading uncommitted levels results in dirty reads .
Another transaction modified the data , But has not yet submitted , And in this matter SELECT You'll read the uncommitted data ( Dirty reading ).
Submit to read (READ COMMITTED)
Must be submitted before reading , Suppose you take a bank card to spend , Before you pay, you see that Cary has 2000 element , Your wife is shopping on Taobao at this time , I finished the payment ahead of you , At this time, when you pay again, you will be prompted that the balance is insufficient , But you can see that the money in the card is enough .
This is when two transactions are executed , Business A I read it at the beginning. The card has 2000 element , This is the time for business B Spend all the money on the card , Business A When we finally reconfirmed the balance, we found that the card had no money . Obviously , Read submit can solve the dirty read problem , But it can't be repeated .
This transaction reads the latest data ( After other transactions are committed ). The problem is , In the same business , Two times the same SELECT You'll read different results ( Don't repeat ).
( Default ) Repeatable (REPEATABLE READ)
Solve the problem of non repeatable reading , Business A Once the execution starts , Regardless of business B How to change the data , Business A What you always read is the value it just started reading . So here's the problem , What if B hold id by 1 The data in the table is changed to 2, Business A Don't know id There is a change , When a transaction A When we added new data, we found that 2 Of id It already exists , This is unreal reading .
In the same business ,SELECT The result is a point in time state at the start of the transaction , therefore , alike SELECT The results read by the operation will be consistent . however , There will be unreal reading ( Fantasy reading ).
Serialization (SERIALIZABLE)
All the transactions are executed one by one , Because there is no concurrent scenario , What kind of unreal reading 、 Dirty reading 、 It's not repeatable. It doesn't exist . But again , Basic concurrency will be very poor .
The read operation implicitly acquires the shared lock , It can ensure the mutual exclusion between different transactions .
How to lock ?
3.1 Table locks
Implicit locking ( Default , Automatic locking and automatic release )
|
|
Explicit locking ( Manual )
|
Unlock ( Manual )
|
| session01 | session02 |
|---|---|
| lock table teacher read;// Up reading lock | |
| select * from teacher; // It can be read normally | select * from teacher;// It can be read normally |
| update teacher set name = 3 where id =2;// An error is reported because the read lock is locked and the write operation cannot be performed | update teacher set name = 3 where id =2;// Blocked |
| unlock tables;// Unlock | |
| update teacher set name = 3 where id =2;// Update operation successful |
| session01 | session02 |
|---|---|
| lock table teacher write;// Write up lock | |
| select * from teacher; // It can be read normally | select * from teacher;// Blocked |
| update teacher set name = 3 where id =2;// You can update the operation normally | update teacher set name = 4 where id =2;// Blocked |
| unlock tables;// Unlock | |
| select * from teacher;// Read successful | |
| update teacher set name = 4 where id =2;// Update operation successful |
3.2 Row lock
Implicit locking ( Default , Automatic locking and automatic release )
|
|
Explicit locking ( Manual )
|
Unlock ( Manual )
|
| session01 | session02 |
|---|---|
| begin; | |
| select * from teacher where id = 2 lock in share mode;// Up reading lock | |
| select * from teacher where id = 2;// It can be read normally | |
| update teacher set name = 3 where id =2;// You can update the operation | update teacher set name = 5 where id =2;// Blocked |
| commit; | |
| update teacher set name = 5 where id =2;// Update operation successful |
| session01 | session02 |
|---|---|
| begin; | |
| select * from teacher where id = 2 for update;// Write up lock | |
| select * from teacher where id = 2;// It can be read normally | |
| update teacher set name = 3 where id =2;// You can update the operation | update teacher set name = 5 where id =2;// Blocked |
| rollback; | |
| update teacher set name = 5 where id =2;// Update operation successful |
|
4 Precautions for row lock
|
How to check the lock ?
5.1 Table locks
Check the table lock
1 |
|

Table lock analysis
1 |
|

|
5.2 Row lock
Line lock analysis
1 |
|

|
Deadlock
6.1 explain
|
6.2 The conditions of production
|
6.1 solve
|
6.1 How to avoid
|
Optimistic lock and pessimistic lock

7.1 Pessimistic locking
explain
|
Implementation mechanism
|
Implementation level
|
Applicable scenario
|
7.2 Optimism lock
explain
|
Implementation mechanism
|
Implementation level
|
Applicable scenario
|
8.select … for update Inquire about
select Queries are unlocked ,select…for update It will be locked , And it's a pessimistic lock , But the types of locks that are added in different query conditions ( Row lock , Table locks ) Is different .
select * from t_user where id = 1 for update;
Cause :
stay where The following query criteria are the primary key index , The only index is the row lock
When the query condition is a common field, a table lock is added
9. Commands that are easy to forget
- Open transaction :start transaction ;
- Pessimistic locking :select user_id ,user_name from huixin where user_id=1002 for update ;
- Commit transaction : commit ;
- View the current lock :show open tables where In_use >0;
- Clear all locks :unlock tables;
- Set the commit of the transaction :set autocommit = (off/0) or set autocommit =( on/1)
- Check whether the transaction is automatically committed : show variables like ‘autocommit’
- Roll back the transaction :rollback
边栏推荐
猜你喜欢

数据与信息资源共享平台(七)

Sealem Finance-基于Web3的全新去中心化金融平台

LeetCode+ 21 - 25

2022最新软件测试面试题常问的30道【持续更新~】
![30 frequently asked questions of 2022 latest software test interview questions [continuous update ~]](/img/43/12392286fe711c5ef15462c900e456.png)
30 frequently asked questions of 2022 latest software test interview questions [continuous update ~]

Lenovo explained for the first time the five advantages of hybrid cloud Lenovo xcloud and how to build an intelligent digital base

PwnTheBox,Web:hello

PwnTheBox,Pwn:tutorial1

Font spider Teaching -- ttf/otf font file compression

Gather for summer Yiping: not everything is reported to Robin Lee. They compete with Xiaomi Huawei by products
随机推荐
Design language testing for functional testing: what tests are included in functional testing? What is the role of each
Vulnhub's DC3
[论文分享] PATA: Fuzzing with Path Aware Taint Analysis
LeetCode+ 16 - 20
Object 有哪些常用方法
Dependencymanagement and dependencies
【QPSK中频】基于FPGA的QPSK中频信号产生模块verilog设计
上海股票开户是安全的吗?
司空见惯 - 大哲学家康德的作息时间表
在Oracle表中如何进行关键词搜索
Ride the storm and explore the secret behind the open source of flyfish, a data visualization development platform!
宝塔计划任务Shell脚本定时删除某各目录下所有文件【记录】
宁愿“大小周”、每天只写 200 行代码、月薪 8k-17k 人群再涨
IP anti query domain name
Is it too late to teach yourself programming at 28? Is it reliable?
Ribbon负载均衡策略
[Interface tutorial] how does easycvr set platform cascading through the interface?
我们对产业互联网的认识,还是困囿于互联网式的平台和中心的逻辑之中
Example analysis of SQL query optimization principle
Executor - Shutdown、ShutdownNow、awaitTermination 详解与实战