Heim  >  Artikel  >  Datenbank  >  So implementieren Sie die MySQL-Anweisungssperre

So implementieren Sie die MySQL-Anweisungssperre

小云云
小云云Original
2017-12-08 12:00:554164Durchsuche

In diesem Artikel werden wir Ihnen die Methode zur Implementierung der MySQL-Anweisungssperre vorstellen, die schon immer ein schwieriges Thema war. Im Rahmen meiner Arbeit stellen Kollegen häufig Fragen zu diesem Thema. Heute werden wir kurz über dieses Problem sprechen und hoffen, allen helfen zu können.

Schauen Sie sich an, welche Sperren der folgenden SQL-Anweisung hinzugefügt werden


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


(1) id ist nicht der Primärschlüssel

(2) Wie hoch ist die Isolationsstufe des aktuellen Systems

(3) Wenn die ID-Spalte nicht der Primärschlüssel ist, gibt es einen Index für die ID? Spalte

(4) id Wenn es einen sekundären Index für die Spalte gibt, ist dieser Index ein sekundärer Index

(5) Wie lauten die Ausführungspläne der beiden SQLs? Index-Scan oder vollständiger Tabellen-Scan

Der tatsächliche Ausführungsplan muss auf der Ausgabe von MySQL basieren

Kombination eins: Die ID-Spalte ist der Primärschlüssel, RC-Isolationsstufe
Kombination zwei: id Die Spalte ist ein sekundärer eindeutiger Index, RC-Isolationsstufe
Kombination drei: Die ID-Spalte ist ein sekundärer nicht eindeutiger Index, RC-Isolationsstufe
Kombination vier: Die ID-Spalte hat keinen Index, RC Isolationsstufe
Kombination fünf: Die ID-Spalte ist der Primärschlüssel, RR-Isolationsstufe
Kombination sechs: Die ID-Spalte ist ein sekundärer eindeutiger Index, RR-Isolationsstufe
Kombination sieben: Die ID-Spalte ist ein sekundärer Nicht-Index -eindeutiger Index, RR-Isolationsstufe
Kombination acht: Die ID-Spalte Es gibt keinen Index, RR-Isolationsstufe

Serialisierbare Isolationsstufe

Unter RR RC-Isolationsstufe ist SQL1: select nicht gesperrt, und im Folgenden wird nur SQL2: Sperrung des Löschvorgangs
Percona
Kombination eins: id primär besprochen Taste + 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;;


Kombination 2: Die Aktualisierung von id unique index + RC

auf dem eindeutigen Index erfordert zwei X-Sperren, von denen eine der eindeutigen Index-ID=10 entspricht Datensatz und einer, der dem Clustered-Index name='d' Datensatz

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 entspricht



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


Kombination drei: id nicht eindeutiger Index + RC

ID wird als normaler Index aufgeführt Dann werden alle entsprechenden Datensätze, die die SQL-Abfragebedingungen erfüllen, gleichzeitig gesperrt. Diese Datensätze befinden sich im Primärschlüsselindex Datensätze auf





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

Kombination vier: ID-Nr 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   ;;

Kombination fünf: ID-Primärschlüssel + RR
Referenzkombination eins

Kombination sechs: ID-Eindeutigkeitsindex + RR
Referenz Kombination zwei

Kombination sieben: id nicht eindeutiger Index + 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;;

Kombination acht: ID ohne 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   ;;

Kombination neun: Serialisierbar

für das oben genannte einfache SQL, der letzte Fall: Serialisierbare Isolationsstufe. Für SQL2: Löschen aus t1 mit id = 10; die Isolationsstufe „Serializable“ ist genau dieselbe wie die Isolationsstufe „Repeatable Read“ und wird daher nicht eingeführt.

Serialisierbare Isolationsstufe wirkt sich auf SQL1 aus: select * from t1 where id = 10; Diese SQL ist unter den Isolationsstufen RC und RR ein Snapshot-Lesen ohne Sperre. Auf der Isolationsstufe „Serialisierbar“ fügt SQL1 jedoch Lesesperren hinzu, was bedeutet, dass keine Snapshot-Lesevorgänge mehr vorhanden sind und die MVCC-Parallelitätskontrolle auf sperrenbasiertes CC herabgestuft wird.

In MySQL/InnoDB gilt das sogenannte Lesen ohne Sperren nicht für alle Situationen, sondern hängt mit der Isolationsstufe zusammen. Die Isolationsstufe „Serialisierbar“ ist nicht mehr gültig, wenn der Lesevorgang nicht gesperrt ist. Alle Lesevorgänge sind aktuelle Lesevorgänge.

Verwandte Empfehlungen:

Mysql-Transaktionsverarbeitung mit hoher Parallelität und Sperrung

So sperren Sie Dateien unter PHP_PHP-Tutorial

MySQL-Transaktionen und Sperrmechanismus

Das obige ist der detaillierte Inhalt vonSo implementieren Sie die MySQL-Anweisungssperre. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn