当前位置:网站首页>MySQL optimized learning diary 10 - locking mechanism

MySQL optimized learning diary 10 - locking mechanism

2022-06-11 10:57:00 herb. dr

Catalog

One 、 Locking mechanism

1.1 effect

1.2 solve

1.3 classification

Two 、 Table locks

2.1 Create a new table and add data

2.2 Check locking

2.3 conversation (session):

2.4 Add read lock :

2.5 Release the lock

2.6 Add write lock

2.7 MySQL Lock mode of watch level lock

2.8 Analysis table locking

3、 ... and 、 Row lock

3.1  Create a new table and add data

3.2 commit

3.3 test

3.4 For row locks : 

3.5  Precautions for row lock


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);

 

 

原网站

版权声明
本文为[herb. dr]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012222098572.html