innodbロック-意向ロックを挿入

3172 ワード

前言:Insert Intention Locksは挿入意向ロックと訳し、まず挿入意向ロックがギャップロックの一種であることを強調し、本稿では公式ドキュメントを参考に学習説明を行う
データベースのバージョン:
SELECT VERSION(); ±-----------+ | version() | ±-----------+ | 5.6.34-log | ±-----------+
データベースエンジン:
show variables like ‘%engine%’; ±---------------------------±-------+ | Variable_name | Value | ±---------------------------±-------+ | default_storage_engine | InnoDB | | default_tmp_storage_engine | InnoDB | | storage_engine | InnoDB | ±---------------------------±-------+
バージョン情報に基づいて、公式ドキュメントを参照してください.
1.MySQL 5.6 Reference Manual-InnoDB Locking-Insert Inention Locks(意向ロックを挿入)
意向ロックを挿入
Gap Lockにはinsert操作時に発生する挿入意向ロック(Insert Intention Lock)が存在する.複数のトランザクションが同じインデックスギャップに異なるデータを同時に書き込む場合、他のトランザクションの完了を待つ必要はなく、ロック待機は発生しません.レコードインデックスにキー値4と7が含まれていると仮定し、異なるトランザクションはそれぞれ5と6を挿入し、各トランザクションは4-7の間に追加された挿入意向ロックを生成し、挿入行の排他ロックを取得しますが、データ行が衝突しないため、互いにロックされません.
An insert intention lock is a type of gap lock set by INSERT operations prior to row insertion. This lock signals the intent to insert in such a way that multiple transactions inserting into the same index gap need not wait for each other if they are not inserting at the same position within the gap. Suppose that there are index records with values of 4 and 7. Separate transactions that attempt to insert values of 5 and 6, respectively, each lock the gap between 4 and 7 with insert intention locks prior to obtaining the exclusive lock on the inserted row, but do not block each other because the rows are nonconflicting.
挿入意向ロックが存在するかどうかをテストする.テーブルの作成:
CREATE TABLE `test_user` (
  `user_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` char(10) NOT NULL,
  PRIMARY KEY (`user_id`),
  KEY `index_user` (`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

2.データの挿入:
INSERT INTO `test_user` VALUES (1,'a');
INSERT INTO `test_user` VALUES (3,'c');

トランザクション1:
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test_user where name='a' lock in share mode;
+---------+------+
| user_id | name |
+---------+------+
|       3 | c    |
+---------+------+
1 row in set (0.00 sec)

物事2:
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test_user(user_id,name) values(2,'b');

「show engine innodb status」出力innodbモニタリングにより、意向ロックを挿入する情報を表示します.
insert into test_user(user_id,name,age) values(2,'b',10)
------- TRX HAS BEEN WAITING 18 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 6628 page no 4 n bits 72 index `index_user` of table `test`.`test_user` trx id 117851203 
     
lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

個人まとめ:1.意向ロックを挿入する目的は、挿入性能を向上させるためであり、複数のトランザクションが、同じインデックス、同じ範囲区間でレコードを挿入する場合、挿入された位置が衝突しなければ、互いにブロックされず、主に排他ロックを申請する必要はありません.2.排他ロックは強力なロックで、他のタイプのロックと互換性がありません.これもよく理解できますが、あるローを修正したり削除したりするときは、データの一貫性を保障するために、強いロックを取得しなければなりません.