Internet projects generally use what isolation level of mysql

Start our content, I believe we must come across the following scene for an interview

Interviewer: "There are a few talk about mysql transaction isolation level?"
You: "read uncommitted, read committed, repeatable read, four default serialization is repeatable read!"
Interviewer: "Why mysql can choose repeatable read isolation level as the default "?
(you cheeky color bitter, I do not know how to answer!)
interviewer:" you have selected a project which isolation level why "??
you:" of course the default repeatable read, for reasons. . uh ... "
(and then you can go back to such a notice!)

To avoid embarrassing scenes, read on!
Mysql default transaction isolation level is repeatable read (Repeatable the Read) , that the Internet project Mysql is using the default isolation level, not to change it?
OK, no, we generally used in the project read committed (Read Commited) this isolation level!
what! Actually read committed, not online say this isolation level presence 不可重复读and 幻读questions? Do not control it? Well, with our questions begin this article!

text

Let's think about a problem, Oracle, SqlServer is selected in Read Committed (Read Commited) as the default isolation level, why not choose Mysql Read Committed (Read Commited) as the selected default isolation level repeatable read ( Repeatable Read) as the default isolation level it?

Why?Why?Why?

This is a historical reason, of course, from our Lord's start from the copy!
Master-slave replication, replication is based on what?
It is based on the binlog replication! The concept here do not want to Quban binlog, and is simply understood as a binlog file database records change it ~
binlog There are several formats?
OK, three, namely,

  • statement: modify the SQL statement is recorded
  • row: record is changed for each row of actual data
  • Model statement row and mixed: mixed

That Mysql in this version 5.0 before, binlog only support STATEMENTthis format! And this format Read Committed (Read Commited) the isolation level at the master copy of a bug, so Mysql will be repeated read (Repeatable Read) as the default isolation level!
Next, we should talk about is when binlog STATEMENTformat and the isolation level is Read Committed (Read Commited) when, what bug it? As shown below, to perform the transaction on the primary (master)

Run the following statements on the primary (master)

select * from test;

Output follows

+---+
| b |
+---+
| 3 |
+---+
1 row in set

However, you execute the statement on the (slave) at this time, the output obtained as follows

Empty set

这样,你就出现了主从不一致性的问题!原因其实很简单,就是在master上执行的顺序为先删后插!而此时binlog为STATEMENT格式,它记录的顺序为先插后删!从(slave)同步的是binglog,因此从机执行的顺序和主机不一致!就会出现主从不一致!
如何解决?
解决方案有两种!
(1)隔离级别设为可重复读(Repeatable Read),在该隔离级别下引入间隙锁。当Session 1执行delete语句时,会锁住间隙。那么,Ssession 2执行插入语句就会阻塞住!
(2)将binglog的格式修改为row格式,此时是基于行的复制,自然就不会出现sql执行顺序不一样的问题!奈何这个格式在mysql5.1版本开始才引入。因此由于历史原因,mysql将默认的隔离级别设为可重复读(Repeatable Read),保证主从复制不出问题!

那么,当我们了解完mysql选可重复读(Repeatable Read)作为默认隔离级别的原因后,接下来我们将其和读已提交(Read Commited)进行对比,来说明为什么在互联网项目为什么将隔离级别设为读已提交(Read Commited)

对比

ok,我们先明白一点!项目中是不用读未提交(Read UnCommitted)串行化(Serializable)两个隔离级别,原因有二

  • 采用读未提交(Read UnCommitted),一个事务读到另一个事务未提交读数据,这个不用多说吧,从逻辑上都说不过去!
  • 采用串行化(Serializable),每个次读操作都会加锁,快照读失效,一般是使用mysql自带分布式事务功能时才使用该隔离级别!(笔者从未用过mysql自带的这个功能,因为这是XA事务,是强一致性事务,性能不佳!互联网的分布式方案,多采用最终一致性的事务解决方案!)

也就是说,我们该纠结都只有一个问题,究竟隔离级别是用读已经提交呢还是可重复读?
接下来对这两种级别进行对比,讲讲我们为什么选读已提交(Read Commited)作为事务隔离级别!
假设表结构如下

 CREATE TABLE `test` (
`id` int(11) NOT NULL, `color` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB

数据如下

+----+-------+
| id | color |
+----+-------+
|  1 |  red  |
|  2 | white |
|  5 |  red  |
|  7 | white |
+----+-------+

为了便于描述,下面将

  • 可重复读(Repeatable Read),简称为RR;
  • 读已提交(Read Commited),简称为RC;

缘由一:在RR隔离级别下,存在间隙锁,导致出现死锁的几率比RC大的多!
此时执行语句

select * from test where id <3 for update;

在RR隔离级别下,存在间隙锁,可以锁住(2,5)这个间隙,防止其他事务插入数据!
而在RC隔离级别下,不存在间隙锁,其他事务是可以插入数据!

ps:在RC隔离级别下并不是不会出现死锁,只是出现几率比RR低而已!

缘由二:在RR隔离级别下,条件列未命中索引会锁表!而在RC隔离级别下,只锁行
此时执行语句

update test set color = 'blue' where color = 'white'; 

在RC隔离级别下,其先走聚簇索引,进行全部扫描。加锁如下:

但在实际中,MySQL做了优化,在MySQL Server过滤条件,发现不满足后,会调用unlock_row方法,把不满足条件的记录放锁。
实际加锁如下

然而,在RR隔离级别下,走聚簇索引,进行全部扫描,最后会将整个表锁上,如下所示

缘由三:在RC隔离级别下,半一致性读(semi-consistent)特性增加了update操作的并发性!
在5.1.15的时候,innodb引入了一个概念叫做“semi-consistent”,减少了更新同一行记录时的冲突,减少锁等待。
所谓半一致性读就是,一个update语句,如果读到一行已经加锁的记录,此时InnoDB返回记录最近提交的版本,由MySQL上层判断此版本是否满足update的where条件。若满足(需要更新),则MySQL会重新发起一次读操作,此时会读取行的最新版本(并加锁)!
具体表现如下:
此时有两个Session,Session1和Session2!
Session1执行

update test set color = 'blue' where color = 'red'; 

先不Commit事务!
与此同时Ssession2执行

update test set color = 'blue' where color = 'white'; 

session 2尝试加锁的时候,发现行上已经存在锁,InnoDB会开启semi-consistent read,返回最新的committed版本(1,red),(2,white),(5,red),(7,white)。MySQL会重新发起一次读操作,此时会读取行的最新版本(并加锁)!
而在RR隔离级别下,Session2只能等待!

两个疑问

在RC级别下,不可重复读问题需要解决么?
不用解决,这个问题是可以接受的!毕竟你数据都已经提交了,读出来本身就没有太大问题!Oracle的默认隔离级别就是RC,你们改过Oracle的默认隔离级别么?

在RC级别下,主从复制用什么binlog格式?
OK,在该隔离级别下,用的binlog为row格式,是基于行的复制!Innodb的创始人也是建议binlog使用该格式!

总结

本文啰里八嗦了一篇文章只是为了说明一件事,互联网项目请用:读已提交(Read Commited)这个隔离级别!

Guess you like

Origin www.cnblogs.com/jywy/p/11058067.html