当前位置:网站首页>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);
边栏推荐
- jszip 获取上传的zip包中的指定文件的file
- 1712. number of schemes for dividing the array into three sub arrays ●●
- 使用 Feign 实现声明式 REST 调用
- 2022 Beijing International Nutrition and Health Industry Expo, the 9th China Great Health Industry Exhibition
- MySQL (IX)
- 英文论文阅读知识总结
- MySQL download, installation and use - complete and detailed steps
- 白屏时间、首屏时间
- Jerry's ble spp open pin_ Code function [chapter]
- 杰理之BLE SPP 开启 pin_code 功能【篇】
猜你喜欢
![Electron desktop development (development of an alarm clock [End])](/img/2b/dd59ebc8d11bedfc53020d69f1aa69.png)
Electron desktop development (development of an alarm clock [End])

MN梦奈宝塔主机系统V1.5版本发布

95后大厂程序员删库被判刑!只因项目被接手对领导心生不满

Cloud image quality assistant IAPP source code

MySQL下载安装使用-完整详细步骤

概率论:计算置信区间

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

新西兰是道路安全做的最好的国家之一

Use of kingbasees UDP monitoring tool for gold warehouse database

基于C语言实现比赛评分系统
随机推荐
杰理之BLE SPP 开启 pin_code 功能【篇】
Unity font spacing
错误的导航分类横条代码版本
Introduction and usage of Eval function
Install MySQL version 5.7 or above on windows (install in compressed package)
新西兰是道路安全做的最好的国家之一
NewOJ Week 2---BCD
Jerry's ble spp open pin_ Code function [chapter]
Why does a ddrx power supply design require a VTT power supply
NFT will change data ownership in the metauniverse
The first day of the new year | at 8:00 p.m. tomorrow, pulsar Chinese developer and user group meeting registration
MySQL download, installation and use - complete and detailed steps
6.如何自动生成头文件依赖 -M
C language course design
Bad navigation category bar code version
2022健博会,北京大健康产业展,艾灸健康展,北京健康服务展
袋鼠云数栈基于CBO在Spark SQL优化上的探索
SAP Spartacus Reference App Structure
MN梦奈宝塔主机系统V1.5版本发布
使用 Ribbon 实现客户端负载均衡