数据库
首页 > 数据库> > 14.5.3 Locks Set by Different SQL Statements in In

14.5.3 Locks Set by Different SQL Statements in In

作者:互联网

A locking read, an UPDATE, or a DELETE generally set record locks on every index record that is scanned in the processing of the SQL statement. It does not matter whether there are WHERE conditions in the statement that would exclude the row. InnoDB does not remember the exact WHERE condition, but only knows which index ranges were scanned. The locks are normally next-key locks that also block inserts into the “gap” immediately before the record. However, gap locking can be disabled explicitly, which causes next-key locking not to be used. For more information, see Section 14.5.1, “InnoDB Locking”. The transaction isolation level also can affect which locks are set; see Section 14.5.2.1, “Transaction Isolation Levels”.

一般来说,锁定读,updte,delete等类型的SQL语句通常在执行的过程中会对被扫描的索引记录加记录锁,这跟有没有过滤行的where条件没有关系,InnoDB 不记得确切的where条件, 但只知道扫描了哪些索引范围。,锁通常是next-ket锁, 它还会在记录之前阻止插入到 "间隙" 中。,但是,gap锁是可以手动禁用的,禁掉后也就不会用到所谓的next-key锁了,更多内容,请看Section 14.5.1, “InnoDB Locking”,事务的隔离级别也会对锁产生影响,更多信息请看Section 14.5.2.1, “Transaction Isolation Levels”.

If a secondary index is used in a search and index record locks to be set are exclusive, InnoDB also retrieves the corresponding clustered index records and sets locks on them.
如果某个查询中用到了辅助索引并且对应记录加上了排他锁,那么innodb会检索相关的聚簇索引记录,并在其上面加上锁
Differences between shared and exclusive locks are described in Section 14.5.1, “InnoDB Locking”.
有关共享锁和排他锁的差异,请看Section 14.5.1, “InnoDB Locking”.
If you have no indexes suitable for your statement and MySQL must scan the entire table to process the statement, every row of the table becomes locked, which in turn blocks all inserts by other users to the table. It is important to create good indexes so that your queries do not unnecessarily scan many rows.
如果没有适合您的语句的索引,MySQL必须扫描整个表来处理语句,,那么表的每一行都将被锁定,表的每一行都会被锁定,这反过来会阻止其他用户对表的所有插入。 创建好的索引非常重要,这样您的查询就不会不必要地扫描很多行
For SELECT ... FOR UPDATE or SELECT ... LOCK IN SHARE MODE, locks are acquired for scanned rows, and expected to be released for rows that do not qualify for inclusion in the result set (for example, if they do not meet the criteria given in the WHERE clause). However, in some cases, rows might not be unlocked immediately because the relationship between a result row and its original source is lost during query execution. For example, in a UNION, scanned (and locked) rows from a table might be inserted into a temporary table before evaluation whether they qualify for the result set. In this circumstance, the relationship of the rows in the temporary table to the rows in the original table is lost and the latter rows are not unlocked until the end of query execution.
SELECT ... FOR UPDATE或者SELECT ... LOCK IN SHARE MODE会为扫描上的行加锁,并且会释放不在结果集中的记录的锁(例如,如果它们不符合WHERE子句中给出的条件),然而,在有些情况下,这些行上的锁不会马上被释放,因为在查询执行过程中,结果行和它的原始源之间的关系将丢失。例如,在存在union的sql中,从表中扫描 ( 和锁定 ) 的行可能插入到临时表中 , 然后再评估它们是否符合结果集的资格,在这种情况下,由于临时表中的表的行和原始表中的行的关系被丢失了,直到查询执行结束后才解锁后的行。
InnoDB sets specific types of locks as follows.
InnoDB设置锁的具体类型如下。

PREV:14.5.2.4 Locking Reads https://blog.51cto.com/itzhoujun/2354207
NEXT: 14.5.4 Phantom Rows https://blog.51cto.com/itzhoujun/2354219

标签:14.5,...,Different,Set,index,lock,locks,InnoDB,key
来源: https://blog.51cto.com/itzhoujun/2354216