Home Database Mysql Tutorial How to implement MySQL statement locking

How to implement MySQL statement locking

Dec 08, 2017 pm 12:00 PM
mysql Lock method

In this article, we will share with you the method of implementing MySQL statement locking. MySQL locking analysis has always been a difficult topic. In the course of my work, colleagues often ask questions in this regard. Today we will briefly talk about this problem, hoping to help everyone.

Look at what locks are added to the following SQL statement


1

2

SLQ1:select * from t1 where id = 10;

SQL2:delete from t1 where id = 10;

Copy after login


(1) Is id the primary key?

(2) What is the isolation level of the current system?

(3) If the id column is not the primary key, is there an index on the id column?

(4) On the id column If there is a secondary index, is this index a secondary index?

(5) What are the execution plans of the two SQLs? Index scan or full table scan

The actual execution plan needs to be based on the output of MySQL

Combination one: the id column is the primary key, RC isolation level
Combination two: id Column is a secondary unique index, RC isolation level
combination three: id column is a secondary non-unique index, RC isolation level
combination four: id column has no index, RC isolation level
combination five: id column Is the primary key, RR isolation level
combination six: id column is a secondary unique index, RR isolation level
combination seven: id column is a secondary non-unique index, RR isolation level
combination eight: on the id column There is no index, RR isolation level

Serializable isolation level

Under the RR RC isolation level, SQL1: select is not locked, and the It is a snapshot read; the following only discusses SQL2: locking of delete operation
Percona

Combination 1: id primary key + RC
Percona


1

2

3

4

5

---TRANSACTION 1286310, ACTIVE 9 sec

2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 1

MySQL thread id 341, OS thread handle 0x7f4d540d0700, query id 4510972 localhost root cleaning up

TABLE LOCK table `test`.`t1` trx id 1286310 lock mode IX

RECORD LOCKS space id 29 page no 3 n bits 80 index `PRIMARY` of table `test`.`t1` trx id 1286310 lock_mode X locks rec but not gap

Copy after login


MySQL


1

2

3

4

5

6

7

8

9

10

---TRANSACTION 5936, ACTIVE 171 sec

2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 1

MySQL thread id 2, OS thread handle 0x7f5677201700, query id 364 localhost root

TABLE LOCK table `test`.`t1` trx id 5936 lock mode IX

RECORD LOCKS space id 6 page no 3 n bits 80 index `PRIMARY` of table `test`.`t1` trx id 5936 lock_mode X locks rec but not gap

Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 4; hex 8000000a; asc   ;;

 1: len 6; hex 000000001730; asc   0;;

 2: len 7; hex 26000001550110; asc &  U ;;

 3: len 1; hex 61; asc a;;

Copy after login


Combination 2: unique id Index+RC
The update on the unique index requires two X locks, one corresponding to the unique index id=10 record, and one corresponding to the clustered index name='d' record
Percona


1

2

3

4

5

6

---TRANSACTION 1286327, ACTIVE 3 sec

3 lock struct(s), heap size 360, 2 row lock(s), undo log entries 1

MySQL thread id 344, OS thread handle 0x7f4d5404e700, query id 4510986 localhost root cleaning up

TABLE LOCK table `test`.`t2` trx id 1286327 lock mode IX

RECORD LOCKS space id 30 page no 4 n bits 80 index `id` of table `test`.`t2` trx id 1286327 lock_mode X locks rec but not gap

RECORD LOCKS space id 30 page no 3 n bits 80 index `PRIMARY` of table `test`.`t2` trx id 1286327 lock_mode X locks rec but not gap

Copy after login


MySQL


##

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

---TRANSACTION 5938, ACTIVE 3 sec

3 lock struct(s), heap size 360, 2 row lock(s), undo log entries 1

MySQL thread id 2, OS thread handle 0x7f5677201700, query id 374 localhost root

TABLE LOCK table `test`.`t2` trx id 5938 lock mode IX

RECORD LOCKS space id 7 page no 4 n bits 80 index `id` of table `test`.`t2` trx id 5938 lock_mode X locks rec but not gap

Record lock, heap no 7 PHYSICAL RECORD: n_fields 2; compact format; info bits 32

 0: len 4; hex 8000000a; asc   ;;

 1: len 1; hex 64; asc d;;

 

RECORD LOCKS space id 7 page no 3 n bits 80 index `PRIMARY` of table `test`.`t2` trx id 5938 lock_mode X locks rec but not gap

Record lock, heap no 7 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 64; asc d;;

 1: len 6; hex 000000001732; asc   2;;

 2: len 7; hex 27000001560110; asc '  V ;;

 3: len 4; hex 8000000a; asc   ;;

Copy after login


Combination 3: id non-unique index + RC

ID is listed as a normal index, then all corresponding records that meet the SQL query conditions will be locked; at the same time, these records on the primary key index will also be locked Lock
Percona


1

2

3

4

5

6

---TRANSACTION 1286339, ACTIVE 9 sec

3 lock struct(s), heap size 360, 4 row lock(s), undo log entries 2

MySQL thread id 347, OS thread handle 0x7f4b67fff700, query id 4511015 localhost root cleaning up

TABLE LOCK table `test`.`t3` trx id 1286339 lock mode IX

RECORD LOCKS space id 31 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 1286339 lock_mode X locks rec but not gap

RECORD LOCKS space id 31 page no 3 n bits 80 index `PRIMARY` of table `test`.`t3` trx id 1286339 lock_mode X locks rec but not gap

Copy after login


MySQL



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

---TRANSACTION 5940, ACTIVE 3 sec

3 lock struct(s), heap size 360, 4 row lock(s), undo log entries 2

MySQL thread id 2, OS thread handle 0x7f5677201700, query id 378 localhost root

TABLE LOCK table `test`.`t3` trx id 5940 lock mode IX

RECORD LOCKS space id 8 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 5940 lock_mode X locks rec but not gap

Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32

 0: len 4; hex 8000000a; asc   ;;

 1: len 1; hex 62; asc b;;

 

Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 32

 0: len 4; hex 8000000a; asc   ;;

 1: len 1; hex 64; asc d;;

 

RECORD LOCKS space id 8 page no 3 n bits 80 index `PRIMARY` of table `test`.`t3` trx id 5940 lock_mode X locks rec but not gap

Record lock, heap no 4 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 62; asc b;;

 1: len 6; hex 000000001734; asc   4;;

 2: len 7; hex 28000001570110; asc (  W ;;

 3: len 4; hex 8000000a; asc   ;;

 

Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 64; asc d;;

 1: len 6; hex 000000001734; asc   4;;

 2: len 7; hex 28000001570132; asc (  W 2;;

 3: len 4; hex 8000000a; asc   ;;

Copy after login


Combination four: id no index + RC

Percona

##

1

2

3

4

5

---TRANSACTION 1286373, ACTIVE 5 sec

2 lock struct(s), heap size 360, 2 row lock(s), undo log entries 2

MySQL thread id 348, OS thread handle 0x7f4d54193700, query id 4511037 localhost root cleaning up

TABLE LOCK table `test`.`t4` trx id 1286373 lock mode IX

RECORD LOCKS space id 33 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 1286373 lock_mode X locks rec but not gap

Copy after login


MySQL


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

---TRANSACTION 5946, ACTIVE 2 sec

2 lock struct(s), heap size 360, 2 row lock(s), undo log entries 2

MySQL thread id 2, OS thread handle 0x7f5677201700, query id 382 localhost root

TABLE LOCK table `test`.`t4` trx id 5946 lock mode IX

RECORD LOCKS space id 9 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 5946 lock_mode X locks rec but not gap

Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 62; asc b;;

 1: len 6; hex 00000000173a; asc   :;;

 2: len 7; hex 2b0000015a0110; asc +  Z ;;

 3: len 4; hex 8000000a; asc   ;;

 

Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 64; asc d;;

 1: len 6; hex 00000000173a; asc   :;;

 2: len 7; hex 2b0000015a012c; asc +  Z ,;;

 3: len 4; hex 8000000a; asc   ;;

Copy after login


Combination five: id primary key + RR

Reference combination one


Combination six: id unique index + RR

Reference combination two


Combination seven: id non-unique index + RR

Percona



##

1

2

3

4

5

6

7

8

---TRANSACTION 1592633, ACTIVE 24 sec

4 lock struct(s), heap size 1184, 5 row lock(s), undo log entries 2

MySQL thread id 794, OS thread handle 0x7f4d5404e700, query id 7801799 localhost root cleaning up

Trx read view will not see trx with id >= 1592634, sees < 1592634

TABLE LOCK table `test`.`t3` trx id 1592633 lock mode IX

RECORD LOCKS space id 31 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 1592633 lock_mode X

RECORD LOCKS space id 31 page no 3 n bits 80 index `PRIMARY` of table `test`.`t3` trx id 1592633 lock_mode X locks rec but not gap

RECORD LOCKS space id 31 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 1592633 lock_mode X locks gap before rec

Copy after login


MySQL


##

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

---TRANSACTION 5985, ACTIVE 7 sec

4 lock struct(s), heap size 1184, 5 row lock(s), undo log entries 2

MySQL thread id 12, OS thread handle 0x7f56770fd700, query id 500 localhost root

TABLE LOCK table `test`.`t3` trx id 5985 lock mode IX

RECORD LOCKS space id 8 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 5985 lock_mode X

Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32

 0: len 4; hex 8000000a; asc   ;;

 1: len 1; hex 64; asc d;;

 

Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 32

 0: len 4; hex 8000000a; asc   ;;

 1: len 1; hex 62; asc b;;

 

RECORD LOCKS space id 8 page no 3 n bits 80 index `PRIMARY` of table `test`.`t3` trx id 5985 lock_mode X locks rec but not gap

Record lock, heap no 4 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 64; asc d;;

 1: len 6; hex 000000001761; asc   a;;

 2: len 7; hex 3f0000016d0132; asc ?  m 2;;

 3: len 4; hex 8000000a; asc   ;;

 

Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 62; asc b;;

 1: len 6; hex 000000001761; asc   a;;

 2: len 7; hex 3f0000016d0110; asc ?  m ;;

 3: len 4; hex 8000000a; asc   ;;

 

RECORD LOCKS space id 8 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 5985 lock_mode X locks gap before rec

Record lock, heap no 8 PHYSICAL RECORD: n_fields 2; compact format; info bits 0

 0: len 4; hex 8000000b; asc   ;;

 1: len 1; hex 66; asc f;;

Copy after login


Combination eight: id no index+RR

Percona



1

2

3

4

5

---TRANSACTION 1592639, ACTIVE 4 sec

2 lock struct(s), heap size 360, 7 row lock(s), undo log entries 2

MySQL thread id 794, OS thread handle 0x7f4d5404e700, query id 7801804 localhost root cleaning up

TABLE LOCK table `test`.`t4` trx id 1592639 lock mode IX

RECORD LOCKS space id 33 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 1592639 lock_mode X

Copy after login


MySQL



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

---TRANSACTION 6000, ACTIVE 3 sec

2 lock struct(s), heap size 360, 7 row lock(s), undo log entries 2

MySQL thread id 12, OS thread handle 0x7f56770fd700, query id 546 localhost root

TABLE LOCK table `test`.`t4` trx id 6000 lock mode IX

RECORD LOCKS space id 9 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 6000 lock_mode X

Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0

 0: len 8; hex 73757072656d756d; asc supremum;;

 

Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0

 0: len 1; hex 61; asc a;;

 1: len 6; hex 000000001722; asc   ";;

 2: len 7; hex 9e0000014e0110; asc   N ;;

 3: len 4; hex 8000000f; asc   ;;

 

Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 62; asc b;;

 1: len 6; hex 000000001770; asc   p;;

 2: len 7; hex 47000001730110; asc G  s ;;

 3: len 4; hex 8000000a; asc   ;;

 

Record lock, heap no 4 PHYSICAL RECORD: n_fields 4; compact format; info bits 0

 0: len 1; hex 63; asc c;;

 1: len 6; hex 000000001722; asc   ";;

 2: len 7; hex 9e0000014e0122; asc   N ";;

 3: len 4; hex 80000006; asc   ;;

 

Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32

 0: len 1; hex 64; asc d;;

 1: len 6; hex 000000001770; asc   p;;

 2: len 7; hex 4700000173012c; asc G  s ,;;

 3: len 4; hex 8000000a; asc   ;;

 

Record lock, heap no 6 PHYSICAL RECORD: n_fields 4; compact format; info bits 0

 0: len 1; hex 66; asc f;;

 1: len 6; hex 000000001722; asc   ";;

 2: len 7; hex 9e0000014e0134; asc   N 4;;

 3: len 4; hex 8000000b; asc   ;;

 

Record lock, heap no 7 PHYSICAL RECORD: n_fields 4; compact format; info bits 0

 0: len 2; hex 7a7a; asc zz;;

 1: len 6; hex 000000001722; asc   ";;

 2: len 7; hex 9e0000014e013d; asc   N =;;

 3: len 4; hex 80000002; asc   ;;

Copy after login


Combination nine: Serializable


For the simple SQL mentioned earlier, the last situation: Serializable isolation level. For SQL2: delete from t1 where id = 10;, the Serializable isolation level is exactly the same as the Repeatable Read isolation level, so it will not be introduced.


Serializable isolation level affects SQL1: select * from t1 where id = 10; This SQL, under the RC and RR isolation levels, is a snapshot read without locking. However, at the Serializable isolation level, SQL1 will add read locks, which means that snapshot reads no longer exist, and MVCC concurrency control is downgraded to Lock-Based CC.

In MySQL/InnoDB, the so-called reading without locking does not apply to all situations, but is related to the isolation level. The Serializable isolation level is no longer valid if the read is not locked. All read operations are current reads.

Related recommendations:

Mysql High Concurrency Locking Transaction Processing

How to lock files under PHP_PHP Tutorial

MySQL transactions and locking mechanism

The above is the detailed content of How to implement MySQL statement locking. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) May 07, 2024 pm 05:55 PM

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors)

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

See all articles