1. What type lock gap
(1) between the index record, or until the index is, the interval after the locking or index, is the gap lock. such as:
SELECT c1 FROM t WHERE c1 BETWEEN 10 and 20 FOR UPDATE;
Since c1 = 10 and c2 = 20 have been added gap between the lock, so if there are c1 = 15 this data regardless of the data in the table, this will prevent attempts to insert sql transaction c1 = 15.
(2) a gap lock may lock an index, a plurality of indexes, index or null.
(3) gap locking and concurrency performance tradeoff, and it is only used in specific isolation level.
2, when there will be a gap lock gap
The only query rows of data with a unique index, and no gap locks. such as:
SELECT * FROM child WHERE id = 100;
If id is the only index, does not produce gap locks; if not the index id or id is not the only index, then the gap will have a lock.
It is also worth noting here that conflicting 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 from an index, the gap locks held on the record by different transactions must be merged.
For example, transaction A has a shared lock on the gap, B transaction has an exclusive lock on the same gap.