Implementation Analysis of MySQL statement locking, mysql statement implementation

Source: Internet
Author: User
Tags percona

Implementation Analysis of MySQL statement locking, mysql statement implementation

Abstract: Analysis of Two SQL statement locks in MySQL

Check the lock applied to the SQL statement under explain.

SLQ1:select * from t1 where id = 10;SQL2:delete from t1 where id = 10;

(1) Whether id is a primary key

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

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

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

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

The actual execution plan depends on the MySQL output.

Combination 1: id column is the primary key, RC isolation level
Combination 2: The id column is a unique secondary index, and the RC isolation level
Combination 3: id column is a secondary non-unique index, RC isolation level
Combination 4: No index for the id column, RC isolation level
Combination 5: The id column is the primary key and the RR isolation level
Combination 6: id column is a unique secondary index, RR isolation level
Combination 7: id column is a secondary non-unique index, RR isolation level
Combination 8: No index on the id column, RR isolation level

Serializable isolation level

At the rr rc isolation level, SQL1: select does not lock, and snapshot reading is used. Here we only discuss the locking of SQL2: delete operations.
Percona

Combination 1: id Primary Key + RC
Percona

---TRANSACTION 1286310, ACTIVE 9 sec2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 1MySQL thread id 341, OS thread handle 0x7f4d540d0700, query id 4510972 localhost root cleaning upTABLE LOCK table `test`.`t1` trx id 1286310 lock mode IXRECORD 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

MySQL

---TRANSACTION 5936, ACTIVE 171 sec2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 1MySQL thread id 2, OS thread handle 0x7f5677201700, query id 364 localhost rootTABLE LOCK table `test`.`t1` trx id 5936 lock mode IXRECORD 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 gapRecord 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;;

Combination 2: Unique id index + RC
Update on a unique index requires two X locks, one corresponding to the unique index id = 10 record, and the other corresponding to the clustered index name = 'D' record.
Percona

---TRANSACTION 1286327, ACTIVE 3 sec3 lock struct(s), heap size 360, 2 row lock(s), undo log entries 1MySQL thread id 344, OS thread handle 0x7f4d5404e700, query id 4510986 localhost root cleaning upTABLE LOCK table `test`.`t2` trx id 1286327 lock mode IXRECORD 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 gapRECORD 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

MySQL

---TRANSACTION 5938, ACTIVE 3 sec3 lock struct(s), heap size 360, 2 row lock(s), undo log entries 1MySQL thread id 2, OS thread handle 0x7f5677201700, query id 374 localhost rootTABLE LOCK table `test`.`t2` trx id 5938 lock mode IXRECORD 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 gapRecord 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 gapRecord 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   ;;

Combination 3: id non-unique index + RC
If the ID column is a common index, 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.
Percona

---TRANSACTION 1286339, ACTIVE 9 sec3 lock struct(s), heap size 360, 4 row lock(s), undo log entries 2MySQL thread id 347, OS thread handle 0x7f4b67fff700, query id 4511015 localhost root cleaning upTABLE LOCK table `test`.`t3` trx id 1286339 lock mode IXRECORD 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 gapRECORD 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

MySQL

---TRANSACTION 5940, ACTIVE 3 sec3 lock struct(s), heap size 360, 4 row lock(s), undo log entries 2MySQL thread id 2, OS thread handle 0x7f5677201700, query id 378 localhost rootTABLE LOCK table `test`.`t3` trx id 5940 lock mode IXRECORD 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 gapRecord 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 gapRecord 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   ;;

Combination 4: No id index + RC
Percona

---TRANSACTION 1286373, ACTIVE 5 sec2 lock struct(s), heap size 360, 2 row lock(s), undo log entries 2MySQL thread id 348, OS thread handle 0x7f4d54193700, query id 4511037 localhost root cleaning upTABLE LOCK table `test`.`t4` trx id 1286373 lock mode IXRECORD 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

MySQL

---TRANSACTION 5946, ACTIVE 2 sec2 lock struct(s), heap size 360, 2 row lock(s), undo log entries 2MySQL thread id 2, OS thread handle 0x7f5677201700, query id 382 localhost rootTABLE LOCK table `test`.`t4` trx id 5946 lock mode IXRECORD 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 gapRecord 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   ;;

Combination 5: id Primary Key + RR
Reference combination 1

Combination 6: Unique id index + RR
Reference combination 2

Combination 7: id non-unique index + RR
Percona

---TRANSACTION 1592633, ACTIVE 24 sec4 lock struct(s), heap size 1184, 5 row lock(s), undo log entries 2MySQL thread id 794, OS thread handle 0x7f4d5404e700, query id 7801799 localhost root cleaning upTrx read view will not see trx with id >= 1592634, sees < 1592634TABLE LOCK table `test`.`t3` trx id 1592633 lock mode IXRECORD LOCKS space id 31 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 1592633 lock_mode XRECORD 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 gapRECORD 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

MySQL

---TRANSACTION 5985, ACTIVE 7 sec4 lock struct(s), heap size 1184, 5 row lock(s), undo log entries 2MySQL thread id 12, OS thread handle 0x7f56770fd700, query id 500 localhost rootTABLE LOCK table `test`.`t3` trx id 5985 lock mode IXRECORD LOCKS space id 8 page no 4 n bits 80 index `idx_key` of table `test`.`t3` trx id 5985 lock_mode XRecord 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 gapRecord 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 recRecord 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;;

Combination 8: No id index + RR
Percona

---TRANSACTION 1592639, ACTIVE 4 sec2 lock struct(s), heap size 360, 7 row lock(s), undo log entries 2MySQL thread id 794, OS thread handle 0x7f4d5404e700, query id 7801804 localhost root cleaning upTABLE LOCK table `test`.`t4` trx id 1592639 lock mode IXRECORD LOCKS space id 33 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 1592639 lock_mode X

MySQL

---TRANSACTION 6000, ACTIVE 3 sec2 lock struct(s), heap size 360, 7 row lock(s), undo log entries 2MySQL thread id 12, OS thread handle 0x7f56770fd700, query id 546 localhost rootTABLE LOCK table `test`.`t4` trx id 6000 lock mode IXRECORD LOCKS space id 9 page no 3 n bits 80 index `PRIMARY` of table `test`.`t4` trx id 6000 lock_mode XRecord 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   ;;

Composite 9: Serializable

For the simple SQL mentioned above, the last case is the 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.

Serializable isolation level, which affects SQL1: select * from t1 where id = 10; this SQL statement, at the RC and RR isolation level, is read by snapshots without locking. However, at the Serializable isolation level, SQL1 will apply a read Lock, that is to say, snapshot reading no longer exists, and MVCC concurrency control will be downgraded to Lock-Based CC.

Conclusion: in MySQL/InnoDB, the so-called read lock does not apply to all cases, but is related to the isolation level. Serializable isolation level, read is no longer valid without locking, all read operations are the current read.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.