diff --git a/mysql/.DS_Store b/mysql/.DS_Store new file mode 100644 index 0000000..e6b5723 Binary files /dev/null and b/mysql/.DS_Store differ diff --git a/mysql/03e055924241608d625a97261360bb6c.webp b/mysql/03e055924241608d625a97261360bb6c.webp new file mode 100644 index 0000000..533d927 Binary files /dev/null and b/mysql/03e055924241608d625a97261360bb6c.webp differ diff --git a/mysql/0493bc2ab4f92163f78e905bab5c71e0.webp b/mysql/0493bc2ab4f92163f78e905bab5c71e0.webp new file mode 100644 index 0000000..7db7c5d Binary files /dev/null and b/mysql/0493bc2ab4f92163f78e905bab5c71e0.webp differ diff --git a/mysql/0972196b94797441b9aeb8c894bdd647.webp b/mysql/0972196b94797441b9aeb8c894bdd647.webp new file mode 100644 index 0000000..1b8e5c8 Binary files /dev/null and b/mysql/0972196b94797441b9aeb8c894bdd647.webp differ diff --git a/mysql/0c95bbe1f0a5c777c00526e790184cf6.webp b/mysql/0c95bbe1f0a5c777c00526e790184cf6.webp new file mode 100644 index 0000000..e38433f Binary files /dev/null and b/mysql/0c95bbe1f0a5c777c00526e790184cf6.webp differ diff --git a/mysql/1aa886fe95e7bc791c296e2d342fa435.png b/mysql/1aa886fe95e7bc791c296e2d342fa435.png new file mode 100644 index 0000000..ae93090 Binary files /dev/null and b/mysql/1aa886fe95e7bc791c296e2d342fa435.png differ diff --git a/mysql/21a088ec9d58b07edf61b11724959b60.webp b/mysql/21a088ec9d58b07edf61b11724959b60.webp new file mode 100644 index 0000000..f9ef54e Binary files /dev/null and b/mysql/21a088ec9d58b07edf61b11724959b60.webp differ diff --git a/mysql/2f6ab1ab7666e5fe8219e9f777cce349.webp b/mysql/2f6ab1ab7666e5fe8219e9f777cce349.webp new file mode 100644 index 0000000..1b7e476 Binary files /dev/null and b/mysql/2f6ab1ab7666e5fe8219e9f777cce349.webp differ diff --git a/mysql/300f23aff81d20aee05f459374398e93.webp b/mysql/300f23aff81d20aee05f459374398e93.webp new file mode 100644 index 0000000..a11ba25 Binary files /dev/null and b/mysql/300f23aff81d20aee05f459374398e93.webp differ diff --git a/mysql/34bff193d46ed46517f2b696a99d6e54.webp b/mysql/34bff193d46ed46517f2b696a99d6e54.webp new file mode 100644 index 0000000..799fb51 Binary files /dev/null and b/mysql/34bff193d46ed46517f2b696a99d6e54.webp differ diff --git a/mysql/36cd86b2e4c5af513954a6637d95f5ac.webp b/mysql/36cd86b2e4c5af513954a6637d95f5ac.webp new file mode 100644 index 0000000..2e1d1fe Binary files /dev/null and b/mysql/36cd86b2e4c5af513954a6637d95f5ac.webp differ diff --git a/mysql/3c3af16e7a948833ccb6409e8b51daf8.png b/mysql/3c3af16e7a948833ccb6409e8b51daf8.png new file mode 100644 index 0000000..88d5f01 Binary files /dev/null and b/mysql/3c3af16e7a948833ccb6409e8b51daf8.png differ diff --git a/mysql/424be75679dd914d4c090f7ef157823a.webp b/mysql/424be75679dd914d4c090f7ef157823a.webp new file mode 100644 index 0000000..29734f5 Binary files /dev/null and b/mysql/424be75679dd914d4c090f7ef157823a.webp differ diff --git a/mysql/473ba924370114aa265371d827e2f980.webp b/mysql/473ba924370114aa265371d827e2f980.webp new file mode 100644 index 0000000..021142f Binary files /dev/null and b/mysql/473ba924370114aa265371d827e2f980.webp differ diff --git a/mysql/4a4875e40dbdcd3188231b9be8b24855.webp b/mysql/4a4875e40dbdcd3188231b9be8b24855.webp new file mode 100644 index 0000000..5128eae Binary files /dev/null and b/mysql/4a4875e40dbdcd3188231b9be8b24855.webp differ diff --git a/mysql/4a68beb82ca7ce2ab24e77939b9273dc.webp b/mysql/4a68beb82ca7ce2ab24e77939b9273dc.webp new file mode 100644 index 0000000..ab1553b Binary files /dev/null and b/mysql/4a68beb82ca7ce2ab24e77939b9273dc.webp differ diff --git a/mysql/4bc24e0894c580d13755d89179e983ff.webp b/mysql/4bc24e0894c580d13755d89179e983ff.webp new file mode 100644 index 0000000..4cc05cc Binary files /dev/null and b/mysql/4bc24e0894c580d13755d89179e983ff.webp differ diff --git a/mysql/4e42b9feeffac0dfd6dd4cae65c64599.webp b/mysql/4e42b9feeffac0dfd6dd4cae65c64599.webp new file mode 100644 index 0000000..8e8ccbb Binary files /dev/null and b/mysql/4e42b9feeffac0dfd6dd4cae65c64599.webp differ diff --git a/mysql/525989f60b9186f934c3e89344306491.webp b/mysql/525989f60b9186f934c3e89344306491.webp new file mode 100644 index 0000000..41302d9 Binary files /dev/null and b/mysql/525989f60b9186f934c3e89344306491.webp differ diff --git a/mysql/531a1118b0868693543dca60303d2cc7.webp b/mysql/531a1118b0868693543dca60303d2cc7.webp new file mode 100644 index 0000000..25d3581 Binary files /dev/null and b/mysql/531a1118b0868693543dca60303d2cc7.webp differ diff --git a/mysql/53ed6171303b5c96fa9541a89d6cfa2b.webp b/mysql/53ed6171303b5c96fa9541a89d6cfa2b.webp new file mode 100644 index 0000000..0490fbf Binary files /dev/null and b/mysql/53ed6171303b5c96fa9541a89d6cfa2b.webp differ diff --git a/mysql/5e438d943afbdd684a911979474b85c7.webp b/mysql/5e438d943afbdd684a911979474b85c7.webp new file mode 100644 index 0000000..37279a2 Binary files /dev/null and b/mysql/5e438d943afbdd684a911979474b85c7.webp differ diff --git a/mysql/63e055617720853f5b64c99576227c09.png b/mysql/63e055617720853f5b64c99576227c09.png new file mode 100644 index 0000000..9ebb377 Binary files /dev/null and b/mysql/63e055617720853f5b64c99576227c09.png differ diff --git a/mysql/6a8f25390ebdd0c5d2b4cedb4a08c8be.webp b/mysql/6a8f25390ebdd0c5d2b4cedb4a08c8be.webp new file mode 100644 index 0000000..ebda862 Binary files /dev/null and b/mysql/6a8f25390ebdd0c5d2b4cedb4a08c8be.webp differ diff --git a/mysql/6bfd6da4f7156aa8ba5514329010a89f.webp b/mysql/6bfd6da4f7156aa8ba5514329010a89f.webp new file mode 100644 index 0000000..a70d875 Binary files /dev/null and b/mysql/6bfd6da4f7156aa8ba5514329010a89f.webp differ diff --git a/mysql/6fa48586516098b80d7c541966a96fc7.webp b/mysql/6fa48586516098b80d7c541966a96fc7.webp new file mode 100644 index 0000000..22d4cdc Binary files /dev/null and b/mysql/6fa48586516098b80d7c541966a96fc7.webp differ diff --git a/mysql/7bbe53dd677c12e69cf80431e7d397e8.webp b/mysql/7bbe53dd677c12e69cf80431e7d397e8.webp new file mode 100644 index 0000000..c798ac2 Binary files /dev/null and b/mysql/7bbe53dd677c12e69cf80431e7d397e8.webp differ diff --git a/mysql/7e322f24e1e81cbf9b3457f67f909dce.webp b/mysql/7e322f24e1e81cbf9b3457f67f909dce.webp new file mode 100644 index 0000000..5cb41a2 Binary files /dev/null and b/mysql/7e322f24e1e81cbf9b3457f67f909dce.webp differ diff --git a/mysql/7f9df142b3594daeaaca495abb7133f5.png b/mysql/7f9df142b3594daeaaca495abb7133f5.png new file mode 100644 index 0000000..b1bb46e Binary files /dev/null and b/mysql/7f9df142b3594daeaaca495abb7133f5.png differ diff --git a/mysql/825ce9ec4df0d2740a3e32cb25713aeb.webp b/mysql/825ce9ec4df0d2740a3e32cb25713aeb.webp new file mode 100644 index 0000000..5016b35 Binary files /dev/null and b/mysql/825ce9ec4df0d2740a3e32cb25713aeb.webp differ diff --git a/mysql/853661544b0d9a7322fbbd31a0a667de.webp b/mysql/853661544b0d9a7322fbbd31a0a667de.webp new file mode 100644 index 0000000..7961295 Binary files /dev/null and b/mysql/853661544b0d9a7322fbbd31a0a667de.webp differ diff --git a/mysql/85e7a2242c619988be2ddd1a2e76571c.webp b/mysql/85e7a2242c619988be2ddd1a2e76571c.webp new file mode 100644 index 0000000..6c5839b Binary files /dev/null and b/mysql/85e7a2242c619988be2ddd1a2e76571c.webp differ diff --git a/mysql/87d7b55393208b73b2f8a718e7957df0.webp b/mysql/87d7b55393208b73b2f8a718e7957df0.webp new file mode 100644 index 0000000..82b6a05 Binary files /dev/null and b/mysql/87d7b55393208b73b2f8a718e7957df0.webp differ diff --git a/mysql/8da776e8b513bc0565609822560450fb.webp b/mysql/8da776e8b513bc0565609822560450fb.webp new file mode 100644 index 0000000..f45d75b Binary files /dev/null and b/mysql/8da776e8b513bc0565609822560450fb.webp differ diff --git a/mysql/8fa3a24c82dd4b772cccaa9f44c2001c.webp b/mysql/8fa3a24c82dd4b772cccaa9f44c2001c.webp new file mode 100644 index 0000000..4cc85e7 Binary files /dev/null and b/mysql/8fa3a24c82dd4b772cccaa9f44c2001c.webp differ diff --git a/mysql/952ef19e09e2c0c1eadb885a9aff12b4.webp b/mysql/952ef19e09e2c0c1eadb885a9aff12b4.webp new file mode 100644 index 0000000..7db7c5d Binary files /dev/null and b/mysql/952ef19e09e2c0c1eadb885a9aff12b4.webp differ diff --git a/mysql/9R8ChusahrJvLGmUvHWBgA b/mysql/9R8ChusahrJvLGmUvHWBgA new file mode 100644 index 0000000..253232b --- /dev/null +++ b/mysql/9R8ChusahrJvLGmUvHWBgA @@ -0,0 +1,2437 @@ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +大家好,我是小林。
为什么会发生这种的事故?
又该如何避免这种事故的发生?
select ... from
语句,其他语句都会被锁住不能执行,业务会因此停滞,接下来等着你的,就是老板的挨骂。sql_safe_updates
参数设置为 1,开启安全更新模式。官方的解释:
If set to 1, MySQL aborts UPDATE or DELETE statements that do not use a key in the WHERE clause or a LIMIT clause. (Specifically, UPDATE statements must have a WHERE clause that uses a key or a LIMIT clause, or both. DELETE statements must have both.) This makes it possible to catch UPDATE or DELETE statements where keys are not used properly and that would probably change or delete a large number of rows. The default value is 0.
使用 where,并且 where 条件中必须有索引列;
使用 limit;
同时使用 where 和 limit,此时 where 条件中可以没有索引列;
使用 where,并且 where 条件中必须有索引列;
同时使用 where 和 limit,此时 where 条件中可以没有索引列;
force index([index_name])
可以告诉优化器使用哪个索引,以此避免有几率锁全表带来的隐患。force index([index_name])
可以告诉优化器使用哪个索引。当查询的记录是存在的,在用「唯一索引进行等值查询」时,next-key lock 会退化成「记录锁」。
当查询的记录是不存在的,在用「唯一索引进行等值查询」时,next-key lock 会退化成「间隙锁」。
先看看记录是存在的。
加锁的基本单位是 next-key lock,因此会话1的加锁范围是(8, 16];
但是由于是用唯一索引进行等值查询,且查询的记录存在,所以 next-key lock 退化成记录锁,因此最终加锁的范围是 id = 16 这一行。
接下来,看看记录不存在的情况
加锁的基本单位是 next-key lock,因此主键索引 id 的加锁范围是(8, 16];
但是由于查询记录不存在,next-key lock 退化成间隙锁,因此最终加锁的范围是 (8,16)。
select * from t_test where id=8 for update;
select * from t_test where id>=8 and id<9 for update;
最开始要找的第一行是 id = 8,因此 next-key lock(4,8],但是由于 id 是唯一索引,且该记录是存在的,因此会退化成记录锁,也就是只会对 id = 8 这一行加锁;
由于是范围查找,就会继续往后找存在的记录,也就是会找到 id = 16 这一行停下来,然后加 next-key lock (8, 16],但由于 id = 16 不满足 id < 9,所以会退化成间隙锁,加锁范围变为 (8, 16)。
当查询的记录存在时,除了会加 next-key lock 外,还额外加间隙锁,也就是会加两把锁。
当查询的记录不存在时,只会加 next-key lock,然后会退化为间隙锁,也就是只会加一把锁。
我们先来看看查询的值存在的情况。
先会对普通索引 b 加上 next-key lock,范围是(4,8];
然后因为是非唯一索引,且查询的记录是存在的,所以还会加上间隙锁,规则是向下遍历到第一个不符合条件的值才能停止,因此间隙锁的范围是(8,16)。
接下来,我们看看查询的值不存在的情况
先会对普通索引 b 加上 next-key lock,范围是(8,16];
但是由于查询的记录是不存在的,所以不会再额外加个间隙锁,但是 next-key lock 会退化为间隙锁,最终加锁范围是 (8,16)。
最开始要找的第一行是 b = 8,因此 next-key lock(4,8],但是由于 b 不是唯一索引,并不会退化成记录锁。
但是由于是范围查找,就会继续往后找存在的记录,也就是会找到 b = 16 这一行停下来,然后加 next-key lock (8, 16],因为是普通索引查询,所以并不会退化成间隙锁。
当查询的记录是存在的,next-key lock 会退化成「记录锁」。
当查询的记录是不存在的,next-key lock 会退化成「间隙锁」。
当查询的记录存在时,除了会加 next-key lock 外,还额外加间隙锁,也就是会加两把锁。
当查询的记录不存在时,只会加 next-key lock,然后会退化为间隙锁,也就是只会加一把锁。
唯一索引在满足一些条件的时候,next-key lock 退化为间隙锁和记录锁。
非唯一索引范围查询,next-key lock 不会退化为间隙锁和记录锁。
这是我的钱包,共有 100 万元。
原子性(Atomicity):一个事务中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节,而且事务在执行过程中发生错误,会被回滚到事务开始前的状态,就像这个事务从来没有执行过一样;
一致性(Consistency):数据库的完整性不会因为事务的执行而受到破坏,比如表中有一个字段为姓名,它有唯一约束,也就是表中姓名不能重复,如果一个事务对姓名字段进行了修改,但是在事务提交后,表中的姓名变得非唯一性了,这就破坏了事务的一致性要求,这时数据库就要撤销该事务,返回初始化的状态。
隔离性(Isolation):数据库允许多个并发事务同时对其数据进行读写和修改的能力,隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。
持久性(Durability):事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失。
原子性和持久性是通过 redo log (重做日志)来保证的;
一致性是通过 undo log(回滚日志) 来保证的;
隔离性是通过 MVCC(多版本并发控制) 或锁机制来保证的;
脏读:读到其他事务未提交的数据;
不可重复读:前后读取的数据不一致;
幻读:前后读取的记录数量不一致。
读未提交(read uncommitted),指一个事务还没提交时,它做的变更就能被其他事务看到;
读提交(read committed),指一个事务提交之后,它做的变更才能被其他事务看到;
可重复读(repeatable read),指一个事务执行过程中看到的数据,一直跟这个事务启动时看到的数据是一致的,MySQL InnoDB 引擎的默认隔离级别;
串行化(serializable );会对记录加上读写锁,在多个事务对这条记录进行读写操作时,如果发生了读写冲突的时候,后访问的事务必须等前一个事务执行完成,才能继续执行;
在「读未提交」隔离级别下,可能发生脏读、不可重复读和幻读现象;
在「读提交」隔离级别下,可能发生不可重复读和幻读现象,但是不可能发生脏读现象;
在「可重复读」隔离级别下,可能发生幻读现象,但是不可能脏读和不可重复读现象;
在「串行化」隔离级别下,脏读、不可重复读和幻读现象都不可能会发生。
在「读未提交」隔离级别下,事务 B 修改余额后,虽然没有提交事务,但是此时的余额已经可以被事务 A 看见了,于是事务 A 中余额 V1 查询的值是 200 万,余额 V2、V3 自然也是 200 万了;
在「读提交」隔离级别下,事务 B 修改余额后,因为没有提交事务,所以事务 A 中余额 V1 的值还是 100 万,等事务 B 提交完后,最新的余额数据才能被事务 A 看见,因此额 V2、V3 都是 200 万;
在「可重复读」隔离级别下,事务 A 只能看见启动事务时的数据,所以余额 V1、余额 V2 的值都是 100 万,当事务 A 提交事务后,就能看见最新的余额数据了,所以余额 V3 的值是 200 万;
在「串行化」隔离级别下,事务 B 在执行将余额 100 万修改为 200 万时,由于此前事务 A 执行了读操作,这样就发生了读写冲突,于是就会被锁住,直到事务 A 提交后,事务 B 才可以继续执行,所以从 A 的角度看,余额 V1、V2 的值是 100 万,余额 V3 的值是 200万。
对于「读未提交」隔离级别的事务来说,因为可以读到未提交事务修改的数据,所以直接读取最新的数据就好了;
对于「串行化」隔离级别的事务来说,通过加读写锁的方式来避免并行访问;
对于「读提交」和「可重复读」隔离级别的事务来说,它们是通过 **Read View **来实现的,它们的区别在于创建 Read View 的时机不同,大家可以把 Read View 理解成一个数据快照,就像相机拍照那样,定格某一时刻的风景。「读提交」隔离级别是在每个读取数据前都生成一个 Read View,而「可重复读」隔离级别是启动事务时生成一个 Read View,然后整个事务期间都在用这个 Read View。
Read View 中四个字段作用;
聚族索引记录中两个跟事务有关的隐藏列;
m_ids :指的是创建 Read View 时当前数据库中活跃且未提交的事务的事务 id 列表,注意是一个列表。
min_trx_id :指的是创建 Read View 时当前数据库中活跃且未提交的事务中最小事务的事务 id,也就是 m_ids 的最小值。
max_trx_id :这个并不是 m_ids 的最大值,而是创建 Read View 时当前数据库中应该给下一个事务的 id 值;
creator_trx_id :指的是创建该 Read View 的事务的事务 id。
trx_id,当一个事务对某条聚族索引记录进行改动时,就会把该事务的事务 id 记录在 trx_id 隐藏列里;
roll_pointer,每次对某条聚族索引记录进行改动时,都会把旧版本的记录写入到 undo 日志中,然后这个隐藏列是个指针,指向每一个旧版本记录,于是就可以通过它找到修改前的记录。
在事务 A 的 Read View 中,它的事务 id 是 51,由于与事务 B 同时启动,所以此时活跃的事务的事务 id 列表是 51 和 52,活跃的事务 id 中最小的事务 id 是事务 A 本身,下一个事务 id 应该是 53。
在事务 B 的 Read View 中,它的事务 id 是 52,由于与事务 A 同时启动,所以此时活跃的事务的事务 id 列表是 51 和 52,活跃的事务 id 中最小的事务 id 是事务 A,下一个事务 id 应该是 53。
如果记录的 trx_id 比该事务的 Read View 中的 creator_trx_id 要小,且不在 m_ids 列表里,这意味着这条记录的事务早就在该事务前提交过了,所以该记录对该事务可见;
如果记录的 trx_id 比该事务的 Read View 中的 creator_trx_id 要大,且在 m_ids 列表里,这意味着该事务读到的是和自己同时启动的另外一个事务修改的数据,这时就不应该读取这条记录,而是沿着 undo log 链条往下找旧版本的记录,直到找到 trx_id 等于或者小于该事务 id 的第一条记录。
「读提交」隔离级别是在每个 select 都会生成一个新的 Read View,也意味着,事务期间的多次读取同一条数据,前后两次读的数据可能会出现不一致,因为可能这期间另外一个事务修改了该记录,并提交了事务。
「可重复读」隔离级别是启动事务时生成一个 Read View,然后整个事务期间都在用这个 Read View,这样就保证了在事务期间读到的数据都是事务启动前的记录。
+ + 前往“发现”-“看一看”浏览“朋友在看” +
++ +
+ +