|
To implement a large-scale, busy, or highly reliable database application, to port substantial【səbˈstænʃl】 code from a different database system, or to tune MySQL performance, it is important to understand InnoDB locking and the InnoDB transaction model.
1.Shared and Exclusive Locks
InnoDB implements【ˈɪmplɪments 实施;执行;贯彻;使生效;】 standard row-level locking where there are two types of locks, shared (S) locks and exclusive (X) locks.
• A shared (S) lock permits the transaction that holds the lock to【to 此时的意思是为了,表目的】 read a row.
• An exclusive (X) lock permits the transaction that holds the lock to【to 此时的意思是为了,表目的】 update or delete a row.
If transaction T1 holds a shared (S) lock on row r, then requests from some distinct transaction T2 for a lock on row r are handled as follows:
• A request by T2 for an S lock can be granted immediately. As a result, both T1 and T2 hold an S lock on r.
• A request by T2 for an X lock cannot be granted immediately.
If a transaction T1 holds an exclusive (X) lock on row r, a request from some distinct transaction T2 for a lock of either type on r cannot be granted immediately. Instead, transaction T2 has to wait for transaction T1 to release its lock on row r.
2. Intention Locks
Intention--【ɪnˈtenʃn 打算;意图;目的;计划;】
InnoDB supports multiple granularity locking which permits coexistence【ˌkoʊɪɡˈzɪstəns 共存;共处;】 of row locks and table locks. For example, a statement such as LOCK TABLES ... WRITE takes an exclusive lock (an X lock) on the specified table. To make locking at multiple【ˈmʌltɪpl 数量多的;多种多样的;】 granularity【grænjʊ'lærɪtɪ 粒度;(颗,成)粒性;】 levels practical【ˈpræktɪkl 实际的;适用的;切实可行的;有用的;真实的;明智的;几乎完全的;客观存在的;心灵手巧的;】, InnoDB uses intention locks. Intention locks are table-level locks that indicate which type of lock (shared or exclusive) a transaction requires later for a row in a table. There are two types of intention locks:
• An intention shared lock (IS) indicates that a transaction intends to set a shared lock on individual【ˌɪndɪˈvɪdʒuəl 单独的;个别的;独特的;一个人的;与众不同的;供一人用的;】 rows in a table.
• An intention exclusive lock (IX) indicates that a transaction intends to set an exclusive lock on individual rows in a table.
For example, SELECT ... FOR SHARE sets an IS lock, and SELECT ... FOR UPDATE sets an IX lock.
The intention locking protocol【ˈproʊtəkɑːl 协议;议定书;规程;规约;礼仪;】 is as follows:
• Before a transaction can acquire【əˈkwaɪər (通过努力、能力、行为表现)获得;得到;购得;】 a shared lock on a row in a table, it must first acquire an IS lock or stronger on the table.
• Before a transaction can acquire【əˈkwaɪər (通过努力、能力、行为表现)获得;得到;购得;】 an exclusive lock on a row in a table, it must first acquire an IX lock on the table.
Table-level lock type compatibility is summarized in the following matrix【ˈmeɪtrɪks 矩阵;基体;杂基;(人或社会成长发展的)社会环境,政治局势;线路网;矩阵转接电路;道路网;】.
A lock is granted to a requesting transaction if it is compatible【kəmˈpætəbl 兼容的,可共存的;】 with existing locks, but not if it conflicts with existing locks. A transaction waits until the conflicting existing lock is released. If a lock request conflicts with an existing lock and cannot be granted because it would cause deadlock, an error occurs.
Intention locks do not block anything except full table requests (for example, LOCK TABLES ... WRITE). The main purpose of intention locks is to show that someone is locking a row, or going to lock a row in the table. ---意向锁的目的
Transaction data for an intention lock appears similar to the following in SHOW ENGINE INNODB STATUS and InnoDB monitor output:- TABLE LOCK table `test`.`t` trx id 10080 <strong>lock mode IX</strong>
复制代码 3.Record Locks
A record lock is a lock on an index record. For example, SELECT c1 FROM t WHERE c1 = 10 FOR UPDATE; prevents any other transaction from inserting, updating, or deleting rows where the value of t.c1 is 10.
Record locks always lock index records, even if a table is defined with no indexes. For such cases, InnoDB creates a hidden clustered index and uses this index for record locking.
Transaction data for a record lock appears similar to the following in SHOW ENGINE INNODB STATUS and InnoDB monitor output:- <strong>RECORD LOCKS</strong> space id 58 page no 3 n bits 72 index `PRIMARY` of table `test`.`t`
- trx id 10078 <strong>lock_mode X</strong> locks rec but not gap
- Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
- 0: len 4; hex 8000000a; asc ;;
- 1: len 6; hex 00000000274f; asc 'O;;
- 2: len 7; hex b60000019d0110; asc ;;
复制代码 4.Gap Locks
A gap lock is a lock on a gap between index records, or a lock on the gap before the first or after the last index record. For example, SELECT c1 FROM t WHERE c1 BETWEEN 10 and 20 FOR UPDATE; prevents other transactions from inserting a value of 15 into column t.c1, whether or not there was already any such value in the column, because the gaps between all existing values in the range are locked.
A gap might span【spæn 跨度,范围;跨距,宽度;】 a single index value, multiple index values, or even be empty.
Gap locks are part of the tradeoff【ˈtreɪˌdɔf 权衡;协调;交易;交换;】 between performance and concurrency【计)并发性,并行性;】, and are used in some transaction isolation levels and not others.
Gap locking is not needed for statements that lock rows using a unique index to search for a unique row. (This does not include the case that the search condition includes only some columns of a multiple-column unique index; in that case, gap locking does occur.) --这一段话,要嘻嘻揣摩,唯一index的查询,是否需要设置gap lock。For example, if the id column has a unique index, the following statement uses only an index-record lock for the row having id value 100 and it does not matter whether other sessions insert rows in the preceding【prɪˈsiːdɪŋ 在前的;前面的;】 gap:- SELECT * FROM child WHERE id = 100;
复制代码 If id is not indexed or has a nonunique index, the statement does lock the preceding gap.
It is also worth noting here that conflicting【kənˈflɪktɪŋ 互相斗争的;相冲突的;】 locks can be held on a gap by different transactions. For example, transaction A can hold a shared gap lock (gap S-lock) on a gap while transaction B holds an exclusive gap lock (gap X-lock) on the same gap. The reason conflicting gap locks are allowed is that if a record is purged【pɜːrdʒd 清除,清洗(组织中的异己分子);净化(心灵、风气等);涤荡(污秽);】 from an index, the gap locks held on the record by different transactions must be merged【mɜːrdʒd (使)合并,结合,并入;融入;相融;渐渐消失在某物中;】.
Gap locks in InnoDB are “purely【ˈpjʊrli 完全;仅仅;】 inhibitive【抑制性的;有阻化性的;】”, which means that their only purpose is to prevent other transactions from inserting to the gap. Gap locks can co-exist【ˌsiː ˈoʊ ɪɡˈzɪst 共存;同时存在;】. A gap lock taken by one transaction does not prevent another transaction from taking a gap lock on the same gap. There is no difference between shared and exclusive gap locks. They do not conflict with each other, and they perform the same function.--没区别,功能一样的。
Gap locking can be disabled explicitly. This occurs if you change the transaction isolation level to READ COMMITTED. In this case, gap locking is disabled for searches and index scans and is used only for foreign-key constraint checking and duplicate-key checking.
There are also other effects of using the READ COMMITTED isolation level. Record locks for nonmatching rows are released after MySQL has evaluated the WHERE condition. For UPDATE statements, InnoDB does a “semi-consistent” read, such that it returns the latest committed version to MySQL so that MySQL can determine whether the row matches the WHERE condition of the UPDATE.
5.Next-Key Locks
A next-key lock is a combination of a record lock on the index record and a gap lock on the gap before the index record.
InnoDB performs row-level locking in such a way that when it searches or scans a table index, it sets shared or exclusive locks on the index records it encounters. Thus, the row-level locks are actually index-record locks. A next-key lock on an index record also affects the “gap” before that index record. That is, a next-key lock is an index-record lock plus a gap lock on the gap preceding 【prɪˈsiːdɪŋ 在前的;前面的;】 the index record. If one session has a shared or exclusive lock on record R in an index, another session cannot insert a new index record in the gap immediately before R in the index order.
Suppose that an index contains the values 10, 11, 13, and 20. The possible next-key locks for this index cover the following intervals【ˈɪntərvəlz (时间上的)间隔,间隙,间歇;(戏剧、电影或音乐会的)幕间休息,休息时间;(其他事情)穿插出现的间隙;】, where a round【raʊnd 圆形的;整数的;环形的;圆弧的;球形的;弧形的;】 bracket【ˈbrækɪt 括号;】 denotes【dɪˈnoʊts 表示;标志;意指;象征;预示】 exclusion【ɪkˈskluːʒn 排斥;排除;排除在外;被排除在外的人(或事物);认为不可能;不包括在内的人(或事物);】 of the interval endpoint and a square bracket denotes inclusion of the endpoint:- (negative infinity, 10]
- (10, 11]
- (11, 13]
- (13, 20]
- (20, positive infinity)
复制代码 For the last interval, the next-key lock locks the gap above the largest value in the index and the “supremum” pseudo-record having a value higher than any value actually in the index. The supremum【上确界;最小上界;上限;】 is not a real index record, so, in effect, this next-key lock locks only the gap following the largest index value.
By default, InnoDB operates in REPEATABLE READ transaction isolation level. In this case, InnoDB uses next-key locks for searches and index scans, which prevents phantom rows.- RECORD LOCKS space id 58 page no 3 n bits 72 index `PRIMARY` of table `test`.`t`
- trx id 10080 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 3; compact format; info bits 0
- 0: len 4; hex 8000000a; asc ;;
- 1: len 6; hex 00000000274f; asc 'O;;
- 2: len 7; hex b60000019d0110; asc ;;
复制代码 6.Insert Intention Locks
An insert intention【ɪnˈtenʃn 打算;意图;目的;计划;】 lock is a type of gap lock set by INSERT operations prior to row insertion. This lock signals【ˈsɪɡnəlz 显示;表示;表明;标志;表达;发信号;示意;预示;发暗号;】 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.
The following example demonstrates【ˈdemənstreɪts 证明;演示;示范;论证;表现;说明;证实;表达;表露;显露;】 a transaction taking an insert intention lock prior to obtaining an exclusive lock on the inserted record. The example involves two clients, A and B.
Client A creates a table containing two index records (90 and 102) and then starts a transaction that places an exclusive lock on index records with an ID greater than 100. The exclusive lock includes a gap lock before record 102:- mysql> CREATE TABLE child (id int(11) NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB;
- mysql> INSERT INTO child (id) values (90),(102);
- mysql> START TRANSACTION;
- mysql> SELECT * FROM child WHERE id > 100 FOR UPDATE;
- +-----+
- | id |
- +-----+
- | 102 |
- +-----+
复制代码 Client B begins a transaction to insert a record into the gap. The transaction takes an insert intention lock while it waits to obtain an exclusive lock.- mysql> START TRANSACTION;
- mysql> INSERT INTO child (id) VALUES (101);
复制代码 Transaction data for an insert intention lock appears similar to the following in SHOW ENGINE INNODB STATUS and InnoDB monitor output:- RECORD LOCKS space id 31 page no 3 n bits 72 index `PRIMARY` of table `test`.`child`
- trx id 8731 lock_mode X locks gap before rec <strong>insert</strong><strong> intention</strong> waiting
- Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
- 0: len 4; hex 80000066; asc f;;
- 1: len 6; hex 000000002215; asc " ;;
- 2: len 7; hex 9000000172011c; asc r ;;...
复制代码 7.AUTO-INC Locks
An AUTO-INC lock is a special table-level lock taken by transactions inserting into tables with AUTO_INCREMENT columns. In the simplest case, if one transaction is inserting values into the table, any other transactions must wait to do their own inserts into that table, so that rows inserted by the first transaction receive consecutive【kənˈsekjətɪv 连续的;连续不断的;】 primary key values.
The innodb_autoinc_lock_mode variable controls the algorithm used for auto-increment locking. It allows you to choose how to trade off between predictable【prɪˈdɪktəbl 可预测的;可预见的;可预料的;意料之中的;老套乏味的;】 sequences of auto-increment values and maximum concurrency for insert operations.
8.Predicate Locks for Spatial Indexes
InnoDB supports SPATIAL indexing of columns containing spatial data.
To handle locking for operations involving SPATIAL indexes, next-key locking does not work well to support REPEATABLE READ or SERIALIZABLE transaction isolation levels. There is no absolute ordering concept in multidimensional【ˌmʌltidɪˈmɛnʃənəl 多维的;多面的;】 data, so it is not clear which is the “next” key.
To enable support of isolation levels for tables with SPATIAL indexes, InnoDB uses predicate【ˈpredɪkət , ˈpredɪkeɪt 述语的;谓项的;】 locks. A SPATIAL index contains minimum bounding【ˈbaʊndɪŋ 形成…的边界(或界限);跳跃着跑;】 rectangle【ˈrektæŋɡl 长方形;矩形;】 (MBR) values, so InnoDB enforces consistent read on the index by setting a predicate lock on the MBR value used for a query. Other transactions cannot insert or modify a row that would match the query condition.
来源:https://www.cnblogs.com/xuliuzai/p/18142019
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|