官网地址
https://dev.mysql.com/doc/refman/5.5/en/innodb-transaction-isolation-levels.html
这里主要是说事务隔离级别,以及对锁的影响
Transaction isolation is one of the foundations of database processing. Isolation is the I in the acronym ACID; the isolation level is the setting that fine-tunes the balance between performance and reliability, consistency, and reproducibility of results when multiple transactions are making changes and performing queries at the same time.
InnoDB
offers all four transaction isolation levels described by the SQL:1992 standard: READ UNCOMMITTED
, READ COMMITTED
, REPEATABLE READ
, and SERIALIZABLE
. The default isolation level for InnoDB
is REPEATABLE READ
.
A user can change the isolation level for a single session or for all subsequent connections with the SET TRANSACTION
statement. To set the server's default isolation level for all connections, use the --transaction-isolation
option on the command line or in an option file. For detailed information about isolation levels and level-setting syntax, see Section 13.3.6, “SET TRANSACTION Syntax”.
InnoDB
supports each of the transaction isolation levels described here using different locking strategies. You can enforce a high degree of consistency with the default REPEATABLE READ
level, for operations on crucial data where ACID compliance is important. Or you can relax the consistency rules with READ COMMITTED
or even READ UNCOMMITTED
, in situations such as bulk reporting where precise consistency and repeatable results are less important than minimizing the amount of overhead for locking. SERIALIZABLE
enforces even stricter rules than REPEATABLE READ
, and is used mainly in specialized situations, such as with XA transactions and for troubleshooting issues with concurrency and deadlocks.
The following list describes how MySQL supports the different transaction levels. The list goes from the most commonly used level to the least used.
-
REPEATABLE READ
This is the default isolation level for
InnoDB
. Consistent reads within the same transaction read the snapshot established by the first read. This means that if you issue several plain (nonlocking)SELECT
statements within the same transaction, theseSELECT
statements are consistent also with respect to each other. See Section 14.8.2.3, “Consistent Nonlocking Reads”.For locking reads (
SELECT
withFOR UPDATE
orLOCK IN SHARE MODE
),UPDATE
, andDELETE
statements, locking depends on whether the statement uses a unique index with a unique search condition, or a range-type search condition.For a unique index with a unique search condition,
InnoDB
locks only the index record found, not the gap before it.For other search conditions,
InnoDB
locks the index range scanned, using gap locks or next-key locks to block insertions by other sessions into the gaps covered by the range. For information about gap locks and next-key locks, see Section 14.8.1, “InnoDB Locking”.
-
READ COMMITTED
Each consistent read, even within the same transaction, sets and reads its own fresh snapshot. For information about consistent reads, see Section 14.8.2.3, “Consistent Nonlocking Reads”.
For locking reads (
SELECT
withFOR UPDATE
orLOCK IN SHARE MODE
),UPDATE
statements, andDELETE
statements,InnoDB
locks only index records, not the gaps before them, and thus permits the free insertion of new records next to locked records. Gap locking is only used for foreign-key constraint checking and duplicate-key checking.Because gap locking is disabled, phantom problems may occur, as other sessions can insert new rows into the gaps. For information about phantoms, see Section 14.8.4, “Phantom Rows”.
If you use
READ COMMITTED
, you must use row-based binary logging.Using
READ COMMITTED
has additional effects:For
UPDATE
orDELETE
statements,InnoDB
holds locks only for rows that it updates or deletes. Record locks for nonmatching rows are released after MySQL has evaluated theWHERE
condition. This greatly reduces the probability of deadlocks, but they can still happen.For
UPDATE
statements, if a row is already locked,InnoDB
performs a “semi-consistent” read, returning the latest committed version to MySQL so that MySQL can determine whether the row matches theWHERE
condition of theUPDATE
. If the row matches (must be updated), MySQL reads the row again and this timeInnoDB
either locks it or waits for a lock on it.
Consider the following example, beginning with this table:
CREATE TABLE t (a INT NOT NULL, b INT) ENGINE = InnoDB;
INSERT INTO t VALUES (1,2),(2,3),(3,2),(4,3),(5,2);
COMMIT;
In this case, table has no indexes, so searches and index scans use the hidden clustered index for record locking (see Section 14.11.2.1, “Clustered and Secondary Indexes”).
Suppose that one client performs an UPDATE
using these statements:
SET autocommit = 0;
UPDATE t SET b = 5 WHERE b = 3;
Suppose also that a second client performs an UPDATE
by executing these statements following those of the first client:
SET autocommit = 0;
UPDATE t SET b = 4 WHERE b = 2;
As InnoDB
executes each UPDATE
, it first acquires an exclusive lock for each row, and then determines whether to modify it. If InnoDB
does not modify the row, it releases the lock. Otherwise, InnoDB
retains the lock until the end of the transaction. This affects transaction processing as follows.
When using the default REPEATABLE READ
isolation level, the first UPDATE
acquires x-locks and does not release any of them:
x-lock(1,2); retain x-lock
x-lock(2,3); update(2,3) to (2,5); retain x-lock
x-lock(3,2); retain x-lock
x-lock(4,3); update(4,3) to (4,5); retain x-lock
x-lock(5,2); retain x-lock
The second UPDATE
blocks as soon as it tries to acquire any locks (because first update has retained locks on all rows), and does not proceed until the first UPDATE
commits or rolls back:
--comment: REPEATABLE-READ级别的情况下,由于所有记录没有索引,扫描所有记录的时候不管是否匹配条件,这些记录都被锁上
x-lock(1,2); block and wait for first UPDATE to commit or roll back
If READ COMMITTED
is used instead, the first UPDATE
acquires x-locks and releases those for rows that it does not modify:
--comment: 而在READ-COMMITTED级别的情况,同样会扫描所有记录并对所有记录上锁,但会对比匹配的记录解锁
x-lock(1,2); unlock(1,2)
x-lock(2,3); update(2,3) to (2,5); retain x-lock
x-lock(3,2); unlock(3,2)
x-lock(4,3); update(4,3) to (4,5); retain x-lock
x-lock(5,2); unlock(5,2)
For the second UPDATE
, InnoDB
does a “semi-consistent” read, returning the latest committed version of each row to MySQL so that MySQL can determine whether the row matches the WHERE
condition of the UPDATE
:
x-lock(1,2); update(1,2) to (1,4); retain x-lock
x-lock(2,3); unlock(2,3)
x-lock(3,2); update(3,2) to (3,4); retain x-lock
x-lock(4,3); unlock(4,3)
x-lock(5,2); update(5,2) to (5,4); retain x-lock
-
The effects of using the
READ COMMITTED
isolation level are the same as enabling theinnodb_locks_unsafe_for_binlog
configuration option, with these exceptions:Enabling
innodb_locks_unsafe_for_binlog
is a global setting and affects all sessions, whereas the isolation level can be set globally for all sessions, or individually per session.innodb_locks_unsafe_for_binlog
can be set only at server startup, whereas the isolation level can be set at startup or changed at runtime.
READ COMMITTED
therefore offers finer and more flexible control thaninnodb_locks_unsafe_for_binlog
. -
READ UNCOMMITTED
SELECT
statements are performed in a nonlocking fashion, but a possible earlier version of a row might be used. Thus, using this isolation level, such reads are not consistent. This is also called a “dirty read.” Otherwise, this isolation level works likeREAD COMMITTED
. -
SERIALIZABLE
This level is like
REPEATABLE READ
, butInnoDB
implicitly converts all plainSELECT
statements toSELECT ... LOCK IN SHARE MODE
ifautocommit
is disabled. Ifautocommit
is enabled, theSELECT
is its own transaction. It therefore is known to be read only and can be serialized if performed as a consistent (nonlocking) read and need not block for other transactions. (To force a plainSELECT
to block if other transactions have modified the selected rows, disableautocommit
.)
mysql 锁2的更多相关文章
-
mysql锁
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有数 ...
-
Mysql锁初步
存储引擎 要了解mysql的锁,就要先从存储引擎说起. 常用存储引擎列表如下图所示: 最常使用的两种存储引擎: Myisam是Mysql的默认存储引擎.当create创建新表时,未指定新表的存储引擎时 ...
-
mysql锁表机制及相关优化
(该文章为方便自己查阅,也希望对大家有所帮助,转载于互联网) 1. 锁机制 当前MySQL支持 ISAM, MyISAM, MEMORY (HEAP) 类型表的表级锁,BDB 表支持页级锁,InnoD ...
-
MySQL锁系列3 MDL锁
http://www.cnblogs.com/xpchild/p/3790139.html MySQL为了保护数据字典元数据,使用了metadata lock,即MDL锁,保证在并发的情况下,结构 ...
-
01 MySQL锁概述
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O 等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有 ...
-
Mysql锁机制介绍
Mysql锁机制介绍 一.概况MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking ...
-
MySQL锁等待分析【2】
MySQL锁等待分析[1]中对锁等待的分析是一步一步来的.虽然最后是分析出来了,可是用时是比较长的:理清各个表之间的关系后,得到如下SQL语句,方便以后使用 select block_trx.trx_ ...
-
MySQL锁与MVCC
--MySQL锁与MVCC --------------------2014/06/29 myisam表锁比较简单,这里主要讨论一下innodb的锁相关问题. innodb相比oracle锁机制简单许 ...
-
MySQL锁总结
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/78 MySQL 锁基础 参考了何登成老师文章的结构MySQL 加 ...
-
Mysql锁机制--并发事务带来的更新丢失问题
Mysql 系列文章主页 =============== 刚开始学习 Mysql 锁的时候,觉得 Mysql 使用的是行锁,再加上其默认的可重复读的隔离级别,那就应该能够自动解决并发事务更新的问题.可 ...
随机推荐
-
Java-URL类详解
类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针.资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询. URL(String spec) 根据 S ...
-
iOS开发人员不容错过的10大工具
内容简介 1.iOS简介 2.iOS开发十大实用工具之开发环境 3.iOS开发十大实用工具之图标设计 4.iOS开发十大实用工具之原型设计 5.iOS开发十大实用工具之演示工具 6.iOS开发十大实用 ...
-
Netsharp快速入门(之17) Netsharp基础功能(参照高级设置)
5.2 参照高级设置 1. 以往来字段为例,打开平台工具-界面管理-列表管理,找到往来单位的资源节点,记下列表项目中的名称 2.记下往来单位部件工作区的id 3. 打开平台工具-界面管理-参照 ...
-
js数组的splice方法
w3school文章链接:http://www.w3school.com.cn/jsref/jsref_splice.asp splice:拼接,结合. splice()方法向数组添加/删除元素,返回 ...
-
Javascript保证精度的小数乘法
众所周知,js的小数乘法很容易丢失精度,这是一件很恶心的事情.所以我写了这个方法,保证计算精度./** * js小数乘法 *@parameter arg1:被乘数(接受小数和整数) *@paramet ...
-
侯哥的Python分享
侯哥语录 我曾经是一个职业教育者,现在是一个*开发者.我希望我的分享可以和更多人一起进步.分享一段我喜欢的话给大家:"我所理解的*不是想干什么就干什么,而是想不干什么就不干什么.当你还没 ...
-
centos7 登陆报错 grep:write error
出现这个原因是因为磁盘空间满了 通过df -h查看存储空间 发现磁盘空间满了,可以用 find / -type f -size +1000M 查找大于1000M的文件删除 然后找到用rm -rf 命令 ...
-
Vmware ESXi添加共享磁盘
1,ssh登录(两个虚拟机所在的宿主机)物理主机,创建共享磁盘(该步骤也可图形化操作) #cd /vmfs/volumes/4ffd9951-2c12a99a-ff2a-3440b59cd4f ...
-
Laya1.x Timer小记
Timer是时钟管理类,在Laya初始化的时候会创建一个实例,通过Laya.timer访问. TimerHandler TimerHandler是对每一个定时任务的封装,每次调用frameOnce.f ...
-
openerp修改logo和title
LOGO的修改addons\web\static\src\img下面的:favicon.ico文件和logo2.png \addons\web\static\src\xml\base.xml#82 l ...