1、什么是锁
2、lock与latch
3、innoDB存储引擎中的锁
3.1、锁的类型
3.2、一致性非锁定读
3.3、一致性锁定读
3.4、自增长与锁
3.5、外键与锁
4、锁的算法
4.1、行锁的三种算法
4.2、解决Phantom Problem
5、锁问题
5.1、脏读
5.2、不可重复读
5.3、丢失更新
6、阻塞
7、死锁
7.1、死锁的概念
7.2、死锁的概率
7.3、死锁的示例
8、锁升级
9、小结
主键锁一条记录:
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 214, OS thread handle 139988100773632, query id 680904 localhost root statistics
select * from t_user where id = 1 for update
------- TRX HAS BEEN WAITING 4 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 113 page no 4 n bits 128 index PRIMARY of table `decent_auction`.`t_user` trx id 51013 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 24; compact format; info bits 0
0: len 8; hex 8000000000000001; asc ;;
1: len 6; hex 00000000c73f; asc ?;;
2: len 7; hex 1e00004aee0392; asc J ;;
3: SQL NULL;
4: SQL NULL;
5: SQL NULL;
6: SQL NULL;
7: SQL NULL;
8: SQL NULL;
9: SQL NULL;
10: len 7; hex 31313131313131; asc 1111111;;
11: len 3; hex 637379; asc csy;;
12: len 6; hex 313233343536; asc 123456;;
13: SQL NULL;
14: SQL NULL;
15: SQL NULL;
16: len 4; hex 80000000; asc ;;
17: len 4; hex 80000000; asc ;;
18: SQL NULL;
19: SQL NULL;
20: len 3; hex 6a6f62; asc job;;
21: len 4; hex 5b93f200; asc [ ;;
22: SQL NULL;
23: len 4; hex 8000000a; asc ;;
主键锁多条记录:
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 214, OS thread handle 139988100773632, query id 680980 localhost root Sending data
select * from t_user where id in (3, 7, 8, 9) for update
------- TRX HAS BEEN WAITING 3 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 113 page no 4 n bits 128 index PRIMARY of table `decent_auction`.`t_user` trx id 51017 lock_mode X locks rec but not gap waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 24; compact format; info bits 0
0: len 8; hex 8000000000000003; asc ;;
1: len 6; hex 00000000c73f; asc ?;;
2: len 7; hex 1e00004aee03dc; asc J ;;
3: SQL NULL;
4: len 8; hex 5368616e67686169; asc Shanghai;;
5: len 9; hex 4368616e676e696e67; asc Changning;;
6: SQL NULL;
7: len 24; hex 76316f547a4a356c59395241564f5079654d354258673d3d; asc v1oTzJ5lY9RAVOPyeM5BXg==;;
8: len 28; hex 6f41666253354c524139523947327a364d4c75514d54645369507563; asc oAfbS5LRA9R9G2z6MLuQMTdSiPuc;;
9: SQL NULL;
10: len 30; hex 68747470733a2f2f77782e716c6f676f2e636e2f6d6d6f70656e2f76695f; asc https://wx.qlogo.cn/mmopen/vi_; (total 127 bytes);
11: len 9; hex e5bca0e890a5e8bda9; asc ;;
12: len 30; hex 636537396132373461303663346663333862366234323439316438313464; asc ce79a274a06c4fc38b6b42491d814d; (total 32 bytes);
13: SQL NULL;
14: SQL NULL;
15: SQL NULL;
16: len 4; hex 80000000; asc ;;
17: len 4; hex 80000000; asc ;;
18: SQL NULL;
19: SQL NULL;
20: len 3; hex 6a6f62; asc job;;
21: len 4; hex 5b93f200; asc [ ;;
22: SQL NULL;
23: len 4; hex 8000000a; asc ;;
非主键锁一条记录:
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 214, OS thread handle 139988100773632, query id 681152 localhost root Sending data
select * from t_user where city = 'Hefei'for update
------- TRX HAS BEEN WAITING 4 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 113 page no 4 n bits 128 index PRIMARY of table `decent_auction`.`t_user` trx id 51019 lock_mode X waiting //锁的不是索引
Record lock, heap no 2 PHYSICAL RECORD: n_fields 24; compact format; info bits 0
0: len 8; hex 8000000000000001; asc ;;
1: len 6; hex 00000000c73f; asc ?;;
2: len 7; hex 1e00004aee0392; asc J ;;
3: SQL NULL;
4: SQL NULL;
5: SQL NULL;
6: SQL NULL;
7: SQL NULL;
8: SQL NULL;
9: SQL NULL;
10: len 7; hex 31313131313131; asc 1111111;;
11: len 3; hex 637379; asc csy;;
12: len 6; hex 313233343536; asc 123456;;
13: SQL NULL;
14: SQL NULL;
15: SQL NULL;
16: len 4; hex 80000000; asc ;;
17: len 4; hex 80000000; asc ;;
18: SQL NULL;
19: SQL NULL;
20: len 3; hex 6a6f62; asc job;;
21: len 4; hex 5b93f200; asc [ ;;
22: SQL NULL;
23: len 4; hex 8000000a; asc ;;
非主键锁多条记录:
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 214, OS thread handle 139988100773632, query id 681177 localhost root Sending data
select * from t_user where city in('Changning', 'Hefei')for update
------- TRX HAS BEEN WAITING 3 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 113 page no 4 n bits 128 index PRIMARY of table `decent_auction`.`t_user` trx id 51021 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 24; compact format; info bits 0
0: len 8; hex 8000000000000001; asc ;;
1: len 6; hex 00000000c73f; asc ?;;
2: len 7; hex 1e00004aee0392; asc J ;;
3: SQL NULL;
4: SQL NULL;
5: SQL NULL;
6: SQL NULL;
7: SQL NULL;
8: SQL NULL;
9: SQL NULL;
10: len 7; hex 31313131313131; asc 1111111;;
11: len 3; hex 637379; asc csy;;
12: len 6; hex 313233343536; asc 123456;;
13: SQL NULL;
14: SQL NULL;
15: SQL NULL;
16: len 4; hex 80000000; asc ;;
17: len 4; hex 80000000; asc ;;
18: SQL NULL;
19: SQL NULL;
20: len 3; hex 6a6f62; asc job;;
21: len 4; hex 5b93f200; asc [ ;;
22: SQL NULL;
23: len 4; hex 8000000a; asc ;;
唯一索引单条锁:
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 215, OS thread handle 139988100773632, query id 681351 localhost root statistics
select * from t_payment_record where payment_no = '201806229938712' for update
------- TRX HAS BEEN WAITING 2 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 118 page no 4 n bits 184 index payment_no_UNIQUE of table `decent_auction`.`t_payment_record` trx id 51041 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 15; hex 323031383036323239393338373132; asc 201806229938712;;
1: len 8; hex 8000000000000001; asc ;;
主键索引锁三条记录:
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 215, OS thread handle 139988100773632, query id 681480 localhost root Sending data
select * from t_user where id in (2, 3) for update
------- TRX HAS BEEN WAITING 3 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 113 page no 4 n bits 128 index PRIMARY of table `decent_auction`.`t_user` trx id 51045 lock_mode X locks rec but not gap waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 24; compact format; info bits 0
0: len 8; hex 8000000000000002; asc ;;
1: len 6; hex 00000000c73f; asc ?;;
2: len 7; hex 1e00004aee03b7; asc J ;;
3: SQL NULL;
4: len 5; hex 416e687569; asc Anhui;;
5: len 5; hex 4865666569; asc Hefei;;
6: SQL NULL;
7: len 24; hex 764c64467366654c6a312f776f5052523475584c56673d3d; asc vLdFsfeLj1/woPRR4uXLVg==;;
8: len 28; hex 6f41666253354a34362d654442505a514f5555465838495f6e486649; asc oAfbS5J46-eDBPZQOUUFX8I_nHfI;;
9: SQL NULL;
10: len 30; hex 68747470733a2f2f77782e716c6f676f2e636e2f6d6d6f70656e2f76695f; asc https://wx.qlogo.cn/mmopen/vi_; (total 131 bytes);
11: len 30; hex e6af9be8b186e698afe4b8aae59d9ae5bcbae79a84e5b08fe69c8be58f8b; asc ;;
12: len 30; hex 313833313130316333373339346238323930626630643633313963313231; asc 1831101c37394b8290bf0d6319c121; (total 32 bytes);
13: SQL NULL;
14: SQL NULL;
15: SQL NULL;
16: len 4; hex 80000000; asc ;;
17: len 4; hex 80000000; asc ;;
18: SQL NULL;
19: SQL NULL;
20: len 3; hex 6a6f62; asc job;;
21: len 4; hex 5b93f200; asc [ ;;
22: SQL NULL;
23: len 4; hex 8000000a; asc ;;
唯一索引多条锁:
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 215, OS thread handle 139988100773632, query id 681372 localhost root Sending data
select * from t_payment_record where payment_no in ('201806229938712', '201806230251212', '201806230429110') for update
------- TRX HAS BEEN WAITING 2 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 118 page no 4 n bits 184 index payment_no_UNIQUE of table `decent_auction`.`t_payment_record` trx id 51043 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 15; hex 323031383036323239393338373132; asc 201806229938712;;
1: len 8; hex 8000000000000001; asc ;;
来源:oschina
链接:https://my.oschina.net/u/1170450/blog/2032405