当前位置:网站首页>MySQL optimized learning diary 10 - locking mechanism
MySQL optimized learning diary 10 - locking mechanism
2022-06-11 10:57:00 【herb. dr】
Catalog
2.1 Create a new table and add data
2.7 MySQL Lock mode of watch level lock
3.1 Create a new table and add data
One 、 Locking mechanism
1.1 effect
Solve the concurrency problem caused by resource sharing
Example : Buy the last dress
A、B You can see this dress A Buy first . The system is not updated in time , Lead to B Also bought one .
1.2 solve
Semaphore mechanism ,A It is locked during operation ,B Unable to operate .
1.3 classification
1、 By operation type
(1) Read the lock ( Shared lock ): For the same data ( clothes ), Multiple read operations can be performed simultaneously , Mutual interference .
(2) Write lock ( The mutex ): If the current write operation is not finished ( A series of operations for buying clothes ), No other lock reading is possible 、 Write lock
2、 Access classification by operation
(1) Table locks : Lock the whole table at one time . Such as MyISAM The storage engine uses table locks , Low overhead 、 Locked fast , No deadlock . But the range of locks is large , Prone to lock conflicts 、 Low concurrency .
(2) Row lock : Lock one piece of data at a time . Such as InnoDB The storage engine uses row locks , Spending big , Lock the slow , It's easy to deadlock , The range of the lock is small , Not prone to lock conflicts , High concurrency ( A very small probability of high occurrence and problems : Dirty reading 、 Fantasy reading 、 It can't be read repeatedly 、 Problems such as missing updates )
(3) Page locks
Two 、 Table locks
2.1 Create a new table and add data
create table tablelock
(
id int primary key auto_increment,
name varchar(20)
) engine myisam;
insert into tablelock(name) values('a1');
insert into tablelock(name) values('a2');
insert into tablelock(name) values('a3');
insert into tablelock(name) values('a4');
insert into tablelock(name) values('a5');
2.2 Check locking
show open tables;

2.3 conversation (session):
Every access to data dos Command line 、 Database client tool libraries are all one session
2.4 Add read lock :
conversation 0:lock table tablelock read;

conversation 0:select * from tablelock; —— read ( check ), Sure

conversation 0:delete from tablelock where id =1; —— Write ( Additions and deletions ), Can not be

conversation 1:select * from tablelock; —— read ( check ), Sure

conversation 1:delete from tablelock where id =1; —— Write ( Additions and deletions ), Meeting “ wait for ” conversation 0 Release the lock


summary :
conversation 0 to A The watch is locked , Operation of other sessions :
(1) Other tables can be (A A table other than a table ) Read 、 Write operations
(2) Yes A surface : You can read , Writing needs to wait
2.5 Release the lock
unlock tables;
2.6 Add write lock
conversation 0:lock table tablelock write;
Current session ( conversation 0) You can perform any operation on the table with write lock ( Additions and deletions ); But it doesn't work ( Add change check ) Other watches

Other conversations :
To conversation 0 A table with a write lock in The premise of adding, deleting, modifying and querying is : Wait for session 0 Release the write lock


2.7 MySQL Lock mode of watch level lock
MyISAM In the execution of the query statement (SELECT) front , Will automatically lock all tables involved , Is performing an update operation (DML) front , Will automatically lock the tables involved . So for MyISAM Table operation , There will be the following :
a、 Yes MyISAM Read operation of table ( Add read lock ), Will not block other processes ( conversation ) Read requests to the same table , But it blocks write requests to the same table . Only when the read lock is released , To perform other process write operations .
b、 Yes MyISAM Write operation of table ( Add write lock ), Will block other processes ( conversation ) Read and write operations on the same table , Only when the write lock is released , Will perform read and write operations of other processes .
2.8 Analysis table locking
See which tables are locked :show open tables; —— 1 Represents being locked

Analyze the severity of table locking :
show status like 'Table%';

Table_locks_immediate : That is, the lock that may be obtained
Table_ locks_ wai ted: Number of table locks to wait (A locked , B stay A Then lock it ,C Accessing the table requires two locks )( If the value is larger , It indicates that the greater the lock competition )
General advice :Table_locks_immediate / Table_ locks_ wai ted > 500, use InnoDB engine , Otherwise use MyISAM engine
3、 ... and 、 Row lock
3.1 Create a new table and add data
create table linelock
(
id int(5) primary key auto_increment,
name varchar(20)
)engine=innodb;
insert into linelock(name) values('1');
insert into linelock(name) values('2');
insert into linelock(name) values('3');
insert into linelock(name) values('4');
insert into linelock(name) values('5');3.2 commit
mysql Default Auto commit;oracle Default does not automatically commit;
To study row locks , For the time being, it will automatically commit close —— set autocommit=0; You need to commit Submit

3.3 test
conversation 0: Write operations insert into linelock values(6, 'a6');

conversation 1: Write operations update linelock set name='ax' where id=6;

conversation 0:t Submit operation commit; conversation 0 The request has timed out .

3.4 For row locks :
1、 If the session x For a piece of data a Conduct DML operation ( Automatic... Was turned off during the study commit), Other sessions need to wait for the session x End the business (commit / rollback) after To access the data a To operate .
2、 Watch lock is through unlock tables; End the business The row lock is through commit/rollback Transaction commit ends transaction
3.5 Precautions for row lock
1、 If there is no index , The row lock will be turned into a table lock
show index from linelock;

Add a new index alter table linelock add index idx_linelock_name(name);
边栏推荐
- The first day of the new year | at 8:00 p.m. tomorrow, pulsar Chinese developer and user group meeting registration
- 错误的导航分类横条代码版本
- 新西兰是道路安全做的最好的国家之一
- [K-means] K-means learning examples
- Série de démarrage C # (XI) - - tableaux multidimensionnels
- 985高校博士因文言文致谢走红!导师评价其不仅SCI写得好...
- Unity font spacing
- Hardware Description Language HDL
- C language course design topic
- 网上开户是安全的吗?普通人可以开吗?
猜你喜欢

MySQL (IX)

Leetcode 1961. Check whether the string is an array prefix

Summary of common constraints in MySQL foundation part I

Implementation of competition scoring system based on C language

International multilingual sea mall rebate product automatic matching order source code

Mxnet construction and implementation of alexnet model (comparison with lenet)

Remote monitoring project offline log specification

DROID-SLAM: 用于单目双目RGBD相机的深度视觉SLAM

When installing mysql, an error occurred because msvcr120 could not be found DLL, unable to continue code resolution "

封装组件系列-(一)-插槽及动态组件
随机推荐
Update更新 bytea类型失败 PostGresql
袋鼠云数栈基于CBO在Spark SQL优化上的探索
封装组件系列-(一)-插槽及动态组件
Golang compilation and linking parameters, runtime
Taking the cooperation between different banks as an example, the construction of small program ecology
NewOJ Week 2---BCD
985高校博士因文言文致谢走红!导师评价其不仅SCI写得好...
白屏时间、首屏时间
string类的常见构造及容量操作
Arbitrum infrastructure: a quick start
NFT products are alive
Store management skills: how to manage chain stores efficiently?
Rxjs Observable. Execute logical analysis of pipe passing in multiple operators
Linker and linker options, runtime libraries and runtime library settings, configuration settings, build process and methods
基于C语言实现比赛评分系统
2022健博会,北京大健康产业展,艾灸健康展,北京健康服务展
95后大厂程序员删库被判刑!只因项目被接手对领导心生不满
云画质助手iApp源码
2022年安全月各类活动方案汇报(28页)
When installing mysql, an error occurred because msvcr120 could not be found DLL, unable to continue code resolution "