Maison  >  Article  >  base de données  >  Comment implémenter le verrouillage des instructions MySQL

Comment implémenter le verrouillage des instructions MySQL

小云云
小云云original
2017-12-08 12:00:554164parcourir

Dans cet article, nous partagerons avec vous la méthode d'implémentation du verrouillage des instructions MySQL. L'analyse du verrouillage MySQL a toujours été un sujet difficile. Dans le cadre de mon travail, mes collègues posent souvent des questions à ce sujet. Aujourd'hui, nous parlerons brièvement de ce problème, en espérant aider tout le monde.

Regardez quels verrous sont ajoutés à l'instruction SQL suivante


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


(1) id n'est pas la clé primaire

(2) Quel est le niveau d'isolement du système actuel

(3) Si la colonne id n'est pas la clé primaire, y a-t-il un index sur l'id column

(4) id S'il y a un index secondaire sur la colonne, cet index est-il un index secondaire

(5) Quels sont les plans d'exécution des deux SQL ? Analyse d'index ou analyse de table complète

Le plan d'exécution réel doit être basé sur la sortie de MySQL

Première combinaison : la colonne id est la clé primaire, le niveau d'isolation RC
Combinaison deux : id La colonne est un index unique secondaire, niveau d'isolement RC
combinaison trois : la colonne id est un index secondaire non unique, niveau d'isolement RC
combinaison quatre : la colonne id n'a pas d'index, RC niveau d'isolement
combinaison cinq : la colonne id est la clé primaire, niveau d'isolement RR
combinaison six : la colonne id est un index unique secondaire, niveau d'isolement RR
combinaison sept : la colonne id est un non secondaire -index unique, niveau d'isolement RR
combinaison huit : la colonne id Il n'y a pas d'index, niveau d'isolement RR

Niveau d'isolement sérialisable

Sous le niveau d'isolement RR RC, SQL1 : la sélection n'est pas verrouillée et il s'agit d'un instantané lu ; ce qui suit ne traite que de SQL2 : verrouillage de l'opération de suppression
Percona

combinaison 1 : identifiant principal clé + RC
Percona


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


MySQL


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


combinaison 2 : La mise à jour de l'id unique index + RC
sur l'index unique nécessite deux verrous X dont un correspondant à l'index unique id=10 record, et un correspondant à l'index clusterisé name='d' record
Percona


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


MySQL


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


Combinaison trois : identifiant index non unique + RC
ID est répertorié comme un index normal , alors tous les enregistrements correspondants qui répondent aux conditions de requête SQL seront verrouillés en même temps, ces enregistrements sont dans l'index de clé primaire Enregistrements sur


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


MySQL



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


Combinaison quatre : identifiant non index + RC

Percona


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


MySQL



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


Combinaison cinq : clé primaire d'identifiant + RR

Combinaison de référence un

Combinaison six : identifiant unique index + RR

Référence combinaison deux

Combinaison sept : id index non unique + RR

Percona


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


MySQL



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


Combinaison huit : identifiant sans index + RR

Percona


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


MySQL



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


Combinaison neuf : Sérialisable


pour le Simple SQL susmentionné, le dernier cas : Niveau d'isolement sérialisable. Pour SQL2 : supprimer de t1 où id = 10 ;, le niveau d'isolement sérialisable est exactement le même que le niveau d'isolement de lecture répétable, il ne sera donc pas introduit.

Le niveau d'isolement sérialisable affecte SQL1 : sélectionnez * from t1 où id = 10 ; Ce SQL, sous les niveaux d'isolement RC et RR, est un instantané lu sans verrouillage. Cependant, au niveau d'isolation sérialisable, SQL1 ajoutera des verrous de lecture, ce qui signifie que les lectures d'instantanés n'existent plus et que le contrôle de concurrence MVCC est rétrogradé vers CC basé sur des verrous.

Dans MySQL/InnoDB, ce qu'on appelle la lecture sans verrouillage ne s'applique pas à toutes les situations, mais est liée au niveau d'isolement. Le niveau d'isolement sérialisable n'est plus valide si la lecture n'est pas verrouillée. Toutes les opérations de lecture sont des lectures en cours.

Recommandations associées :

Traitement des transactions de verrouillage à haute concurrence MySQL

Tutoriel Comment verrouiller des fichiers sous PHP_PHP

Transactions MySQL et mécanisme de verrouillage

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn