MySQL 幻读原理与可重复读隔离级别
🎯 一句话定位
幻读是什么问题?
同一事务内,两次相同的范围查询,第二次读到了其他事务新增的行
就像:你点名时发现多了个人,但名单上没他名字
生活化比喻 :
就像你在教室里数人数,第一次数是 30 人,第二次数变成了 31 人------但奇怪的是,你两次看的都是同一个教室,而且中间没人进出。那个"多出来的人"就是"幻影"。
核心特点:幻读只在"范围查询"时出现,单行查询不会出现(单行查询用行锁就能解决)。
💎 核心观点(5 分钟速览版)
核心要点速览
核心观点(5 个关键点)
1. 幻读的本质:范围查询结果不一致
❌ 非幻读:同一行数据两次读取值不同(这是不可重复读)
✅ 幻读:同一范围查询,第二次读到了新增的行
例子:
事务 A:SELECT * FROM orders WHERE status = 'pending'
→ 返回 10 行
事务 B:INSERT INTO orders (id, status) VALUES (11, 'pending')
COMMIT
事务 A:SELECT * FROM orders WHERE status = 'pending'
→ 返回 11 行(多了 id=11 的行)
这就是幻读:id=11 的行就是"幻影"
2. 可重复读(RR)的快照读可以避免普通幻读
✅ 快照读(普通 SELECT):通过 MVCC 机制,读的是事务开始时的快照版本
- 事务 A 开始时的数据版本:10 行
- 即使事务 B 插入了新行,事务 A 看到的还是 10 行
- 这就是"可重复读"的含义:同一事务内多次读取结果一致
❌ 但快照读无法解决"当前读"场景的幻读
3. 当前读场景依赖 next-key lock 抑制幻读
当前读场景:
- SELECT ... FOR UPDATE(加锁查询)
- SELECT ... LOCK IN SHARE MODE(加锁查询)
- UPDATE(修改数据)
- DELETE(删除数据)
- INSERT(插入数据,唯一性校验)
当前读会读取最新数据,并通过加锁防止其他事务修改
Next-key lock = 行锁 + 间隙锁
- 行锁:锁住已有的行
- 间隙锁:锁住索引之间的"间隙",防止其他事务插入新行
4. 三种仍会出现幻读的情况
情况 1:没命中索引,锁升级为表锁
- 如果 WHERE 条件没有索引,InnoDB 会锁全表
- 性能极差,但不会幻读(因为全表都被锁了)
情况 2:只有行锁没有间隙锁
- 某些特殊场景(如 RR 降级为 RU)
- 间隙锁失效,其他事务可以插入新行
情况 3:快照读和当前读混用
- 事务 A 先快照读(看到 10 行)
- 事务 B 插入新行并提交
- 事务 A 再当前读(看到 11 行)
- 这就是"混用"导致的幻读
5. 标准 SQL 的 RR 本身不解决幻读
标准 SQL 定义:
- 可重复读(Repeatable Read):保证同一事务内多次读取同一行数据一致
- 不保证范围查询结果一致(即不解决幻读)
InnoDB 的扩展:
- 通过 next-key lock 在"当前读"场景抑制幻读
- 通过 MVCC 在"快照读"场景避免幻读
- 但仍有边界情况(如混用场景)
生产环境建议:
- 使用唯一约束防止重复插入
- 合理使用加锁(FOR UPDATE)
- 避免长事务,减少并发冲突
🚀 一句话精华(如果只能记住一句)
幻读 = 同一事务内两次范围查询结果不一致;InnoDB 通过 MVCC(快照读)+ next-key lock(当前读)抑制幻读,但仍有边界情况(混用、无索引等)。
一、幻读的本质:什么是"幻影行"?
幻读 vs 不可重复读
生活化比喻:
不可重复读:
你打开一本书,第一页写的是"张三",翻到最后一页再回来,第一页变成了"李四"。
问题:同一行数据,两次读取值不同。
幻读:
你打开一本书,第一次数有 100 页,第二次数变成了 101 页。
问题:同一范围查询,两次读取行数不同。
关键区别:
- 不可重复读:关注"单行数据"的变化(UPDATE)
- 幻读:关注"范围查询"的变化(INSERT/DELETE)
幻读的典型场景
示例代码(SQL):
sql
-- 事务 A
START TRANSACTION;
SELECT * FROM orders WHERE status = 'pending';
-- 返回 10 行
-- 事务 B(在事务 A 执行期间)
START TRANSACTION;
INSERT INTO orders (id, status) VALUES (11, 'pending');
COMMIT;
-- 事务 A(继续执行)
SELECT * FROM orders WHERE status = 'pending';
-- 如果返回 11 行,就是幻读
生活化比喻:
想象你在教室数人数:
第一次:1, 2, 3, ..., 30(共 30 人)
第二次:1, 2, 3, ..., 30, 31(共 31 人)
问题:你两次数的都是同一个教室,为什么人数变了?
答案:有个人"幻影"一样出现了(事务 B 插入的新行)
幻读的技术定义
幻读条件:
1. 同一事务内
2. 两次相同的范围查询(WHERE 条件相同)
3. 第二次查询结果包含第一次没有的行
4. 这些行是其他事务在两次查询之间插入的
注意:
- 如果第二次查询结果少了行(其他事务删除),也是幻读
- 幻读只发生在"范围查询",单行查询不会出现
二、可重复读(RR)如何解决幻读?
InnoDB 的两种读方式
生活化比喻:
快照读(Snapshot Read):
就像拍照:你拍了一张照片,照片里的内容是当时的状态。
即使后来场景变了,照片里的内容不会变。
普通 SELECT 就是快照读。
当前读(Current Read):
就像实时监控:你看到的是当前的实时状态。
SELECT ... FOR UPDATE 就是当前读,会加锁防止其他事务修改。
#mermaid-svg-wyewiDBMG1yhajaw{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wyewiDBMG1yhajaw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wyewiDBMG1yhajaw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wyewiDBMG1yhajaw .error-icon{fill:#552222;}#mermaid-svg-wyewiDBMG1yhajaw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wyewiDBMG1yhajaw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wyewiDBMG1yhajaw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wyewiDBMG1yhajaw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wyewiDBMG1yhajaw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wyewiDBMG1yhajaw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wyewiDBMG1yhajaw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wyewiDBMG1yhajaw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wyewiDBMG1yhajaw .marker.cross{stroke:#333333;}#mermaid-svg-wyewiDBMG1yhajaw svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wyewiDBMG1yhajaw p{margin:0;}#mermaid-svg-wyewiDBMG1yhajaw .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-wyewiDBMG1yhajaw .cluster-label text{fill:#333;}#mermaid-svg-wyewiDBMG1yhajaw .cluster-label span{color:#333;}#mermaid-svg-wyewiDBMG1yhajaw .cluster-label span p{background-color:transparent;}#mermaid-svg-wyewiDBMG1yhajaw .label text,#mermaid-svg-wyewiDBMG1yhajaw span{fill:#333;color:#333;}#mermaid-svg-wyewiDBMG1yhajaw .node rect,#mermaid-svg-wyewiDBMG1yhajaw .node circle,#mermaid-svg-wyewiDBMG1yhajaw .node ellipse,#mermaid-svg-wyewiDBMG1yhajaw .node polygon,#mermaid-svg-wyewiDBMG1yhajaw .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wyewiDBMG1yhajaw .rough-node .label text,#mermaid-svg-wyewiDBMG1yhajaw .node .label text,#mermaid-svg-wyewiDBMG1yhajaw .image-shape .label,#mermaid-svg-wyewiDBMG1yhajaw .icon-shape .label{text-anchor:middle;}#mermaid-svg-wyewiDBMG1yhajaw .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wyewiDBMG1yhajaw .rough-node .label,#mermaid-svg-wyewiDBMG1yhajaw .node .label,#mermaid-svg-wyewiDBMG1yhajaw .image-shape .label,#mermaid-svg-wyewiDBMG1yhajaw .icon-shape .label{text-align:center;}#mermaid-svg-wyewiDBMG1yhajaw .node.clickable{cursor:pointer;}#mermaid-svg-wyewiDBMG1yhajaw .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wyewiDBMG1yhajaw .arrowheadPath{fill:#333333;}#mermaid-svg-wyewiDBMG1yhajaw .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wyewiDBMG1yhajaw .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wyewiDBMG1yhajaw .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wyewiDBMG1yhajaw .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wyewiDBMG1yhajaw .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wyewiDBMG1yhajaw .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wyewiDBMG1yhajaw .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wyewiDBMG1yhajaw .cluster text{fill:#333;}#mermaid-svg-wyewiDBMG1yhajaw .cluster span{color:#333;}#mermaid-svg-wyewiDBMG1yhajaw div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wyewiDBMG1yhajaw .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wyewiDBMG1yhajaw rect.text{fill:none;stroke-width:0;}#mermaid-svg-wyewiDBMG1yhajaw .icon-shape,#mermaid-svg-wyewiDBMG1yhajaw .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wyewiDBMG1yhajaw .icon-shape p,#mermaid-svg-wyewiDBMG1yhajaw .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wyewiDBMG1yhajaw .icon-shape .label rect,#mermaid-svg-wyewiDBMG1yhajaw .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wyewiDBMG1yhajaw .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wyewiDBMG1yhajaw .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wyewiDBMG1yhajaw :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 当前读(Current Read)
SELECT ... FOR UPDATE
读取最新数据,加锁防止并发修改
SELECT ... LOCK IN SHARE MODE
UPDATE / DELETE / INSERT
通过锁机制实现
快照读(Snapshot Read)
普通 SELECT
读取事务开始时的快照版本
通过 MVCC 实现
不加锁,性能好
快照读如何通过 MVCC 避免幻读
MVCC(Multi-Version Concurrency Control)原理:
核心思想:
- 每行数据有多个版本(历史版本)
- 每个事务看到的是"事务开始时"的数据版本
- 通过 undo log 维护历史版本
例子:
时间线:
T0:事务 A 开始,看到数据版本 V0(10 行)
T1:事务 B 插入新行,生成版本 V1(11 行)
T2:事务 B 提交
T3:事务 A 再次查询,看到的还是 V0(10 行)
结果:事务 A 两次查询结果一致,没有幻读
生活化比喻:
MVCC 就像图书馆的"历史档案室":
- 每本书都有多个版本(第 1 版、第 2 版、第 3 版...)
- 你借书时,图书管理员会记录你借的是哪个版本
- 即使后来出了新版,你看到的还是你借的那个版本
好处:
- 你不用和其他人抢书(不用加锁)
- 你看到的永远是同一版本(可重复读)
快照读避免幻读的示例
sql
-- 事务 A(快照读场景)
START TRANSACTION;
SELECT * FROM orders WHERE status = 'pending';
-- 返回 10 行(版本 V0)
-- 事务 B(在事务 A 执行期间)
START TRANSACTION;
INSERT INTO orders (id, status) VALUES (11, 'pending');
COMMIT;
-- 生成新版本 V1(11 行)
-- 事务 A(继续执行,快照读)
SELECT * FROM orders WHERE status = 'pending';
-- 仍然返回 10 行(还是版本 V0)
-- 没有幻读!
关键点:
- ✅ 普通 SELECT 是快照读,通过 MVCC 避免幻读
- ✅ 事务 A 看到的是"事务开始时"的快照,不受其他事务影响
- ❌ 但快照读无法解决"当前读"场景的幻读
三、当前读场景:Next-Key Lock 抑制幻读
当前读为什么需要加锁?
生活化比喻:
快照读 = 拍照(不加锁,性能好)
当前读 = 实时监控(加锁,防止并发修改)
为什么当前读要加锁?
因为当前读要读取"最新数据",而且要"修改数据"。
如果不加锁,其他事务可能同时修改同一行数据,导致数据不一致。
例子:
事务 A:SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
事务 B:UPDATE accounts SET balance = balance - 100 WHERE id = 1;
如果不加锁:
- 事务 A 读到 balance = 1000
- 事务 B 把 balance 改为 900
- 事务 A 基于 1000 做计算,结果错误
加锁后:
- 事务 A 加锁,读到 balance = 1000
- 事务 B 被阻塞,等待事务 A 释放锁
- 事务 A 完成并提交
- 事务 B 才能继续
Next-Key Lock 的原理
Next-Key Lock = 行锁 + 间隙锁
行锁(Record Lock):
- 锁住已有的行
- 防止其他事务修改这一行
- 例如:锁住 id=1 的行
间隙锁(Gap Lock):
- 锁住索引之间的"间隙"
- 防止其他事务在间隙中插入新行
- 例如:锁住 id=1 和 id=3 之间的间隙,防止插入 id=2 的行
Next-Key Lock = 行锁 + 左侧间隙锁
- 锁住 id=1 的行
- 锁住 id=1 左侧的间隙(id=0 到 id=1 之间)
- 防止其他事务插入 id=0 或 id=1 的行
生活化比喻:
行锁 = 锁住房子
- 你锁住了"门牌号 1 号"的房子
- 其他不能进入这个房子
间隙锁 = 锁住房子之间的空地
- 你锁住了"门牌号 1 号和 3 号之间的空地"
- 其他人不能在这块空地上建新房子
Next-Key Lock = 行锁 + 间隙锁
- 你锁住了"门牌号 1 号的房子"和"1 号左侧的空地"
- 其他人不能进入 1 号房子,也不能在 1 号左侧建新房子
好处:防止幻读(其他人不能插入新行)
#mermaid-svg-NhQdVxxFg5rsLAg9{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-NhQdVxxFg5rsLAg9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-NhQdVxxFg5rsLAg9 .error-icon{fill:#552222;}#mermaid-svg-NhQdVxxFg5rsLAg9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NhQdVxxFg5rsLAg9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NhQdVxxFg5rsLAg9 .marker.cross{stroke:#333333;}#mermaid-svg-NhQdVxxFg5rsLAg9 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NhQdVxxFg5rsLAg9 p{margin:0;}#mermaid-svg-NhQdVxxFg5rsLAg9 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-NhQdVxxFg5rsLAg9 .cluster-label text{fill:#333;}#mermaid-svg-NhQdVxxFg5rsLAg9 .cluster-label span{color:#333;}#mermaid-svg-NhQdVxxFg5rsLAg9 .cluster-label span p{background-color:transparent;}#mermaid-svg-NhQdVxxFg5rsLAg9 .label text,#mermaid-svg-NhQdVxxFg5rsLAg9 span{fill:#333;color:#333;}#mermaid-svg-NhQdVxxFg5rsLAg9 .node rect,#mermaid-svg-NhQdVxxFg5rsLAg9 .node circle,#mermaid-svg-NhQdVxxFg5rsLAg9 .node ellipse,#mermaid-svg-NhQdVxxFg5rsLAg9 .node polygon,#mermaid-svg-NhQdVxxFg5rsLAg9 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-NhQdVxxFg5rsLAg9 .rough-node .label text,#mermaid-svg-NhQdVxxFg5rsLAg9 .node .label text,#mermaid-svg-NhQdVxxFg5rsLAg9 .image-shape .label,#mermaid-svg-NhQdVxxFg5rsLAg9 .icon-shape .label{text-anchor:middle;}#mermaid-svg-NhQdVxxFg5rsLAg9 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-NhQdVxxFg5rsLAg9 .rough-node .label,#mermaid-svg-NhQdVxxFg5rsLAg9 .node .label,#mermaid-svg-NhQdVxxFg5rsLAg9 .image-shape .label,#mermaid-svg-NhQdVxxFg5rsLAg9 .icon-shape .label{text-align:center;}#mermaid-svg-NhQdVxxFg5rsLAg9 .node.clickable{cursor:pointer;}#mermaid-svg-NhQdVxxFg5rsLAg9 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-NhQdVxxFg5rsLAg9 .arrowheadPath{fill:#333333;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-NhQdVxxFg5rsLAg9 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NhQdVxxFg5rsLAg9 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-NhQdVxxFg5rsLAg9 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NhQdVxxFg5rsLAg9 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-NhQdVxxFg5rsLAg9 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-NhQdVxxFg5rsLAg9 .cluster text{fill:#333;}#mermaid-svg-NhQdVxxFg5rsLAg9 .cluster span{color:#333;}#mermaid-svg-NhQdVxxFg5rsLAg9 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-NhQdVxxFg5rsLAg9 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-NhQdVxxFg5rsLAg9 rect.text{fill:none;stroke-width:0;}#mermaid-svg-NhQdVxxFg5rsLAg9 .icon-shape,#mermaid-svg-NhQdVxxFg5rsLAg9 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NhQdVxxFg5rsLAg9 .icon-shape p,#mermaid-svg-NhQdVxxFg5rsLAg9 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-NhQdVxxFg5rsLAg9 .icon-shape .label rect,#mermaid-svg-NhQdVxxFg5rsLAg9 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NhQdVxxFg5rsLAg9 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-NhQdVxxFg5rsLAg9 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-NhQdVxxFg5rsLAg9 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Next-Key Lock
行锁:锁住 id=3
间隙锁:锁住 id=1 到 id=3 的间隙
防止修改 id=3,防止插入 id=2
间隙锁
锁住 id=1 到 id=3 的间隙
防止其他事务插入 id=2
行锁
锁住 id=3 这一行
防止其他事务修改 id=3
数据行(索引)
间隙
间隙
间隙
id=1
id=3
id=5
id=7
Next-Key Lock 抑制幻读的示例
sql
-- 事务 A(当前读场景)
START TRANSACTION;
SELECT * FROM orders WHERE status = 'pending' FOR UPDATE;
-- 返回 10 行,同时加上 Next-Key Lock
-- 锁住这 10 行的索引,以及索引之间的间隙
-- 事务 B(尝试插入新行)
START TRANSACTION;
INSERT INTO orders (id, status) VALUES (11, 'pending');
-- 被阻塞!因为事务 A 的间隙锁锁住了插入位置
-- 等待事务 A 释放锁...
-- 事务 A(继续执行)
SELECT * FROM orders WHERE status = 'pending' FOR UPDATE;
-- 仍然返回 10 行
-- 没有幻读!
-- 事务 A 提交
COMMIT;
-- 释放所有锁
-- 事务 B 继续执行
INSERT INTO orders (id, status) VALUES (11, 'pending');
-- 插入成功
COMMIT;
关键点:
- ✅ SELECT ... FOR UPDATE 是当前读,会加 Next-Key Lock
- ✅ Next-Key Lock 锁住已有的行和索引间隙,防止其他事务插入新行
- ✅ 因此,事务 A 两次查询结果一致,没有幻读
📊 附:MySQL 锁分类(三个维度)
MySQL 的锁没有固定的"几种"答案,要按维度分。面试时按下面三个维度讲就完整了。
维度一:按粒度(锁多大范围)
| 锁 | 锁什么 | 典型场景 | 生活化比喻 |
|---|---|---|---|
| 全局锁 | 整个库 | FLUSH TABLES WITH READ LOCK,全库逻辑备份 |
整栋楼封闭 |
| 表级锁 | 整张表 | LOCK TABLES;DDL 时的 MDL 元数据锁 |
锁住整个小区 |
| 行级锁 | 索引记录 | InnoDB 主力,UPDATE/DELETE/FOR UPDATE |
只锁某栋房子 |
其中 MDL 元数据锁容易被忽略:
执行 SELECT 时会加 MDL 读锁,ALTER TABLE 要加 MDL 写锁。
所以「长事务不提交 → DDL 被堵住 → 后面所有查询排队」是经典生产事故。
维度二:按读写属性(能不能共存)
| 锁 | 含义 | 兼容性 |
|---|---|---|
| 共享锁 S | 读锁,LOCK IN SHARE MODE |
S 与 S 兼容,S 与 X 互斥 |
| 排他锁 X | 写锁,FOR UPDATE、UPDATE/DELETE |
X 与任何锁互斥 |
| 意向锁 IS/IX | 表级"预告",表示"我打算锁某几行" | 意向锁之间互不互斥,只和表级 S/X 冲突 |
意向锁的作用一句话:不用逐行检查,就能判断"这张表有没有行被锁住",让表锁和行锁高效共存。它由引擎自动加,不需要手动干预。
维度三:InnoDB 行锁的三种算法(本篇重点)
| 算法 | 锁住什么 | 作用 |
|---|---|---|
| Record Lock 记录锁 | 索引上的某一行 | 防止别人改这行 |
| Gap Lock 间隙锁 | 索引记录之间的空隙,不含行本身 | 防止别人在空隙里插入新行 |
| Next-Key Lock | 左开右闭区间 = 间隙 + 行 | 既防改又防插,RR 下当前读的默认算法 |
索引值: 1 3 5 7
Record: [3] ← 只锁 id=3 这行
Gap: (1,3) ← 能挡住插入 id=2
NextKey: (1,3] ← 间隙 + id=3 这行
关键点:
- RR 隔离级别下,
SELECT ... FOR UPDATE默认用 Next-Key Lock,这就是"当前读抑制幻读"的原理 - RC 下没有 Gap Lock(只有记录锁),所以 RC 会幻读
- 没命中索引时退化为全表锁定,性能差但不幻读
补充两个常问的锁
| 锁 | 作用 |
|---|---|
| 插入意向锁 | 插入前在间隙上加,多个事务往同一间隙的不同位置插入时互不阻塞,提高并发插入性能 |
| 自增锁 AUTO-INC | 表级,保证自增 ID 连续分配;innodb_autoinc_lock_mode=2 时退化为轻量互斥量,批量插入性能更好 |
速记:
粒度:全局 / 表 / 行
属性:S / X + 意向锁
行锁算法:Record / Gap / Next-Key
幻读这件事,本质就是 Next-Key Lock(当前读)+ MVCC(快照读)两条路在兜。
📊 附:MDL 元数据锁(防止表结构被改)
MDL 解决什么问题?
一句话:防止「正在读写记录的时候,表结构被人改了」。
生活化比喻:
就像你正在教室里点名(SELECT),
突然有人进来把桌椅全搬走、把房间改成仓库(ALTER TABLE),
你的点名就进行不下去了。
MDL 就是门口那块牌子:
- 点名牌(MDL 读锁):房间里可以有很多人同时点名,但不许改造房间
- 改造牌(MDL 写锁):改造期间,任何人不许进来点名
核心特点 :MySQL 5.5 引入,Server 层自动加锁 ,不需要手动操作;属于表级锁,保护的是表结构元数据(表名、列定义、索引定义),不是行记录。
MDL 的兼容规则
| 你在做什么 | 自动加什么 MDL | 效果 |
|---|---|---|
SELECT / INSERT / UPDATE / DELETE(DML) |
MDL 读锁 | 多个 DML 之间互不阻塞,但挡住 DDL |
ALTER TABLE / DROP TABLE(DDL) |
MDL 写锁 | 挡住所有 DML 和其他 DDL |
一句话:读读兼容,读写互斥,写写互斥。和 S/X 锁是同一套思路,只是作用对象是"表结构"而不是"行数据"。
⚠️ 最关键:MDL 随事务释放,不是随语句释放
这是生产事故的根因:
text
事务 A:BEGIN; SELECT * FROM orders WHERE id = 1; -- 加了 MDL 读锁
(一直没提交,MDL 读锁一直持有)
事务 B:ALTER TABLE orders ADD COLUMN remark ...; -- 需要 MDL 写锁 → 被阻塞
事务 C:SELECT * FROM orders LIMIT 1; -- 需要 MDL 读锁
-- 排在 B 后面,也被阻塞!
注意第三步:哪怕只是一个普通查询,也会因为排在 DDL 后面而被拖死。结果是连接池迅速打满,整表甚至整库不可用。这就是「长事务 + DDL」经典翻车现场。
所以上线 DDL 前,一定要先确认没有长事务,必要时设置 lock_wait_timeout 并选择低峰期操作。
MDL 锁不锁数据表里的数据?
| 持有的 MDL | 对数据表的影响 |
|---|---|
| MDL 读锁 | 数据的增删改查完全正常,只有改结构的 DDL 被挡住 |
| MDL 写锁 | 所有读写全部堵死,连一条 SELECT 都进不来 |
准确说法:MDL 读锁不碰数据,MDL 写锁让整张表(含数据)暂时不可访问。
另外,锁不会写进数据表:锁是内存中的结构,由 Server 层(MDL)和 InnoDB(行锁)分别维护,业务表里不会留下任何"被锁过"的痕迹,也没有锁字段。
排查方法
sql
-- MDL 锁:谁持有、谁在等
SELECT * FROM performance_schema.metadata_locks;
-- 更直观:MDL 阻塞链路
SELECT * FROM sys.schema_table_lock_waits;
-- InnoDB 行锁(MySQL 8.0)
SELECT * FROM performance_schema.data_locks;
SELECT * FROM performance_schema.data_lock_waits;
各种锁的分工
| 锁 | 保护对象 | 防的是什么 |
|---|---|---|
| MDL | 表结构 | 读写记录时表被 ALTER / DROP |
表锁 LOCK TABLES |
整张表的数据 | 显式独占整表 |
| 行锁 / Next-Key Lock | 行记录 | 记录被并发改、范围里被插入(幻读) |
MDL 防"表结构变化与记录读写"的冲突,行锁防"记录之间"的冲突,两者不是一回事,经常一起出现但不互相替代。
四、三种仍会出现幻读的情况
情况 1:没命中索引,锁升级为表锁
生活化比喻:
有索引时:
- 就像按"门牌号"锁房子
- 只锁住相关的几栋房子和间隙
- 其他房子不受影响
没有索引时:
- 就像按"小区"锁房子
- 整个小区都被锁住(全表锁)
- 其他事务不能插入任何新行
性能对比:
- 有索引:锁住 10 行,其他事务可以插入其他行
- 没有索引:锁住 1000 行,其他事务不能插入任何行
示例:
sql
-- 假设 status 字段没有索引
-- 事务 A
START TRANSACTION;
SELECT * FROM orders WHERE status = 'pending' FOR UPDATE;
-- 没有索引,InnoDB 会锁全表(所有行)
-- 事务 B
START TRANSACTION;
INSERT INTO orders (id, status) VALUES (11, 'pending');
-- 被阻塞!因为事务 A 锁住了全表
-- 等待事务 A 释放锁...
问题:
- 性能极差(全表被锁)
- 但不会幻读(因为全表都被锁了)
解决方案:
- 为 WHERE 条件字段添加索引
- 让锁只作用于相关行,而不是全表
表格对比:
| 场景 | 是否有索引 | 锁范围 | 性能 | 是否幻读 |
|---|---|---|---|---|
| 有索引 | ✅ | 相关行 + 间隙 | ⭐⭐⭐⭐⭐ | ❌ 不幻读 |
| 无索引 | ❌ | 全表 | ⭐ | ❌ 不幻读(但性能差) |
情况 2:只有行锁没有间隙锁
生活化比喻:
正常情况(RR 隔离级别):
- Next-Key Lock = 行锁 + 间隙锁
- 锁住已有的行,也锁住间隙
- 其他事务不能插入新行
异常情况(RR 降级为 RU):
- 只有行锁,没有间隙锁
- 只锁住已有的行,不锁间隙
- 其他事务可以在间隙中插入新行
- 导致幻读
示例:
sql
-- 假设隔离级别为 Read Uncommitted(RU)
-- 事务 A
START TRANSACTION;
SELECT * FROM orders WHERE status = 'pending' FOR UPDATE;
-- 返回 10 行,只加上行锁(没有间隙锁)
-- 事务 B
START TRANSACTION;
INSERT INTO orders (id, status) VALUES (11, 'pending');
COMMIT;
-- 插入成功!因为没有间隙锁阻止插入
-- 事务 A
SELECT * FROM orders WHERE status = 'pending' FOR UPDATE;
-- 返回 11 行(多了 id=11 的行)
-- 幻读!
关键点:
- ❌ 只有行锁没有间隙锁时,其他事务可以插入新行
- ❌ 导致幻读
- ✅ InnoDB 默认使用 RR 隔离级别,有 Next-Key Lock,不会幻读
情况 3:快照读和当前读混用
生活化比喻:
快照读 = 拍照(看到事务开始时的版本)
当前读 = 实时监控(看到最新数据)
混用场景:
第一次:拍照(看到 10 行)
其他事务插入新行
第二次:实时监控(看到 11 行)
结果:两次看到的不一样,幻读!
示例:
sql
-- 事务 A
START TRANSACTION;
SELECT * FROM orders WHERE status = 'pending';
-- 快照读,返回 10 行(版本 V0)
-- 事务 B
START TRANSACTION;
INSERT INTO orders (id, status) VALUES (11, 'pending');
COMMIT;
-- 生成新版本 V1(11 行)
-- 事务 A(继续执行)
SELECT * FROM orders WHERE status = 'pending' FOR UPDATE;
-- 当前读,返回 11 行(最新版本 V1)
结果:
- 第一次快照读:10 行
- 第二次当前读:11 行
- 幻读!
关键点:
- ❌ 快照读和当前读混用会导致幻读
- ❌ 因为快照读看到旧版本,当前读看到最新版本
- ✅ 建议:同一事务内,要么都用快照读,要么都用当前读
五、标准 SQL 的 RR vs InnoDB 的 RR
标准 SQL 定义
生活化比喻:
标准 SQL 的 RR:
- 就像"同一本书的同一页,多次阅读内容一致"
- 保证:同一事务内,多次读取同一行数据一致
- 不保证:范围查询结果一致(即不解决幻读)
InnoDB 的 RR:
- 在标准 SQL 基础上扩展
- 通过 MVCC + Next-Key Lock 抑制幻读
- 但仍有边界情况(混用、无索引等)
表格对比:
| 特性 | 标准 SQL RR | InnoDB RR |
|---|---|---|
| 可重复读(单行) | ✅ 保证 | ✅ 保证 |
| 避免幻读(范围查询) | ❌ 不保证 | ✅ 通过 MVCC + Next-Key Lock 抑制 |
| 实现机制 | 未定义 | MVCC(快照读)+ Next-Key Lock(当前读) |
| 边界情况 | - | 混用、无索引时仍可能幻读 |
InnoDB 的扩展实现
#mermaid-svg-7L3dZCR2bsSdg9sO{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7L3dZCR2bsSdg9sO .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7L3dZCR2bsSdg9sO .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7L3dZCR2bsSdg9sO .error-icon{fill:#552222;}#mermaid-svg-7L3dZCR2bsSdg9sO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7L3dZCR2bsSdg9sO .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7L3dZCR2bsSdg9sO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7L3dZCR2bsSdg9sO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7L3dZCR2bsSdg9sO .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7L3dZCR2bsSdg9sO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7L3dZCR2bsSdg9sO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7L3dZCR2bsSdg9sO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7L3dZCR2bsSdg9sO .marker.cross{stroke:#333333;}#mermaid-svg-7L3dZCR2bsSdg9sO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7L3dZCR2bsSdg9sO p{margin:0;}#mermaid-svg-7L3dZCR2bsSdg9sO .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-7L3dZCR2bsSdg9sO .cluster-label text{fill:#333;}#mermaid-svg-7L3dZCR2bsSdg9sO .cluster-label span{color:#333;}#mermaid-svg-7L3dZCR2bsSdg9sO .cluster-label span p{background-color:transparent;}#mermaid-svg-7L3dZCR2bsSdg9sO .label text,#mermaid-svg-7L3dZCR2bsSdg9sO span{fill:#333;color:#333;}#mermaid-svg-7L3dZCR2bsSdg9sO .node rect,#mermaid-svg-7L3dZCR2bsSdg9sO .node circle,#mermaid-svg-7L3dZCR2bsSdg9sO .node ellipse,#mermaid-svg-7L3dZCR2bsSdg9sO .node polygon,#mermaid-svg-7L3dZCR2bsSdg9sO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7L3dZCR2bsSdg9sO .rough-node .label text,#mermaid-svg-7L3dZCR2bsSdg9sO .node .label text,#mermaid-svg-7L3dZCR2bsSdg9sO .image-shape .label,#mermaid-svg-7L3dZCR2bsSdg9sO .icon-shape .label{text-anchor:middle;}#mermaid-svg-7L3dZCR2bsSdg9sO .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7L3dZCR2bsSdg9sO .rough-node .label,#mermaid-svg-7L3dZCR2bsSdg9sO .node .label,#mermaid-svg-7L3dZCR2bsSdg9sO .image-shape .label,#mermaid-svg-7L3dZCR2bsSdg9sO .icon-shape .label{text-align:center;}#mermaid-svg-7L3dZCR2bsSdg9sO .node.clickable{cursor:pointer;}#mermaid-svg-7L3dZCR2bsSdg9sO .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7L3dZCR2bsSdg9sO .arrowheadPath{fill:#333333;}#mermaid-svg-7L3dZCR2bsSdg9sO .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7L3dZCR2bsSdg9sO .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7L3dZCR2bsSdg9sO .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7L3dZCR2bsSdg9sO .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7L3dZCR2bsSdg9sO .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7L3dZCR2bsSdg9sO .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7L3dZCR2bsSdg9sO .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7L3dZCR2bsSdg9sO .cluster text{fill:#333;}#mermaid-svg-7L3dZCR2bsSdg9sO .cluster span{color:#333;}#mermaid-svg-7L3dZCR2bsSdg9sO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7L3dZCR2bsSdg9sO .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7L3dZCR2bsSdg9sO rect.text{fill:none;stroke-width:0;}#mermaid-svg-7L3dZCR2bsSdg9sO .icon-shape,#mermaid-svg-7L3dZCR2bsSdg9sO .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7L3dZCR2bsSdg9sO .icon-shape p,#mermaid-svg-7L3dZCR2bsSdg9sO .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7L3dZCR2bsSdg9sO .icon-shape .label rect,#mermaid-svg-7L3dZCR2bsSdg9sO .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7L3dZCR2bsSdg9sO .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7L3dZCR2bsSdg9sO .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7L3dZCR2bsSdg9sO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} InnoDB RR(扩展)
可重复读同一行
快照读:MVCC
当前读:Next-Key Lock
抑制幻读
边界情况:混用、无索引
标准 SQL RR
可重复读同一行
不保证范围查询一致
不解决幻读
InnoDB 实现细节:
快照读(普通 SELECT):
- 通过 MVCC 读取事务开始时的快照版本
- 不受其他事务影响
- 避免幻读
当前读(SELECT ... FOR UPDATE, UPDATE, DELETE, INSERT):
- 读取最新数据
- 通过 Next-Key Lock 锁住已有的行和间隙
- 防止其他事务插入新行
- 抑制幻读
边界情况:
- 快照读和当前读混用:看到不同版本,幻读
- 没有索引:锁升级为全表锁,性能差但不幻读
- RR 降级为 RU:只有行锁没有间隙锁,幻读
六、生产环境最佳实践
避免幻读的 Checklist
生活化比喻:
避免幻读就像"防止教室里出现幽灵":
1. 锁好门窗(加锁)
2. 登记进出人员(唯一约束)
3. 不要频繁切换观察方式(避免混用)
4. 保持教室整洁(合理索引)
Checklist:
| 措施 | 说明 | 生活化比喻 |
|---|---|---|
| ✅ 使用唯一约束 | 防止重复插入 | 给每个人发唯一身份证号 |
| ✅ 合理使用加锁 | SELECT ... FOR UPDATE | 锁住房子,防止其他人进入 |
| ✅ 添加索引 | 避免全表锁 | 按门牌号锁房子,而不是按小区 |
| ✅ 避免长事务 | 减少并发冲突 | 快进快出,不占座 |
| ✅ 避免混用 | 同一事务内,要么都用快照读,要么都用当前读 | 要么都拍照,要么都实时监控 |
| ✅ 选择合适的隔离级别 | RR 或 RU,根据业务需求 | 选择观察方式 |
唯一约束的重要性
生活化比喻:
唯一约束 = 给每个人发唯一身份证号
作用:
- 防止重复插入(即使有幻读,唯一约束也能防止重复)
- 提高查询性能(索引加速)
- 保证数据一致性
例子:
给 orders 表的 order_no 字段加唯一约束:
ALTER TABLE orders ADD UNIQUE INDEX uk_order_no (order_no);
即使出现幻读,唯一约束也能防止重复插入相同订单号
示例:
sql
-- 创建唯一约束
ALTER TABLE orders ADD UNIQUE INDEX uk_order_no (order_no);
-- 事务 A
START TRANSACTION;
SELECT * FROM orders WHERE order_no = 'ORD001';
-- 返回空
-- 事务 B
START TRANSACTION;
SELECT * FROM orders WHERE order_no = 'ORD001';
-- 返回空
-- 事务 A
INSERT INTO orders (order_no, status) VALUES ('ORD001', 'pending');
COMMIT;
-- 事务 B
INSERT INTO orders (order_no, status) VALUES ('ORD001', 'pending');
-- 失败!唯一约束冲突
结果:即使出现幻读,唯一约束也能防止重复插入
Go 代码示例:合理使用加锁和索引
go
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
// 避免幻读的最佳实践
func AvoidPhantomRead(db *sql.DB) error {
// 1. 开启事务
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback() // 失败时回滚
// 2. 使用索引字段查询(避免全表锁)
// 假设 order_no 字段有索引
query := "SELECT * FROM orders WHERE order_no = ? FOR UPDATE"
rows, err := tx.Query(query, "ORD001")
if err != nil {
return err
}
defer rows.Close()
// 3. 处理查询结果
var count int
for rows.Next() {
count++
}
// 4. 根据查询结果决定操作
if count == 0 {
// 订单不存在,插入新订单
insertQuery := "INSERT INTO orders (order_no, status) VALUES (?, ?)"
_, err = tx.Exec(insertQuery, "ORD001", "pending")
if err != nil {
return err
}
} else {
// 订单存在,更新订单
updateQuery := "UPDATE orders SET status = ? WHERE order_no = ?"
_, err = tx.Exec(updateQuery, "completed", "ORD001")
if err != nil {
return err
}
}
// 5. 提交事务
return tx.Commit()
}
// 避免混用快照读和当前读
func AvoidMixedRead(db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
// ❌ 错误示例:混用快照读和当前读
// query1 := "SELECT * FROM orders WHERE status = ?" // 快照读
// query2 := "SELECT * FROM orders WHERE status = ? FOR UPDATE" // 当前读
// ✅ 正确示例:统一使用当前读
query := "SELECT * FROM orders WHERE status = ? FOR UPDATE"
rows, err := tx.Query(query, "pending")
if err != nil {
return err
}
defer rows.Close()
// 处理结果...
return tx.Commit()
}
func main() {
db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/testdb")
if err != nil {
log.Fatal(err)
}
defer db.Close()
err = AvoidPhantomRead(db)
if err != nil {
log.Fatal(err)
}
fmt.Println("避免幻读成功!")
}
七、技术深入
#mermaid-svg-05I0qw9Ho395yKCZ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-05I0qw9Ho395yKCZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-05I0qw9Ho395yKCZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-05I0qw9Ho395yKCZ .error-icon{fill:#552222;}#mermaid-svg-05I0qw9Ho395yKCZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-05I0qw9Ho395yKCZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-05I0qw9Ho395yKCZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-05I0qw9Ho395yKCZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-05I0qw9Ho395yKCZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-05I0qw9Ho395yKCZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-05I0qw9Ho395yKCZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-05I0qw9Ho395yKCZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-05I0qw9Ho395yKCZ .marker.cross{stroke:#333333;}#mermaid-svg-05I0qw9Ho395yKCZ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-05I0qw9Ho395yKCZ p{margin:0;}#mermaid-svg-05I0qw9Ho395yKCZ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-05I0qw9Ho395yKCZ .cluster-label text{fill:#333;}#mermaid-svg-05I0qw9Ho395yKCZ .cluster-label span{color:#333;}#mermaid-svg-05I0qw9Ho395yKCZ .cluster-label span p{background-color:transparent;}#mermaid-svg-05I0qw9Ho395yKCZ .label text,#mermaid-svg-05I0qw9Ho395yKCZ span{fill:#333;color:#333;}#mermaid-svg-05I0qw9Ho395yKCZ .node rect,#mermaid-svg-05I0qw9Ho395yKCZ .node circle,#mermaid-svg-05I0qw9Ho395yKCZ .node ellipse,#mermaid-svg-05I0qw9Ho395yKCZ .node polygon,#mermaid-svg-05I0qw9Ho395yKCZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-05I0qw9Ho395yKCZ .rough-node .label text,#mermaid-svg-05I0qw9Ho395yKCZ .node .label text,#mermaid-svg-05I0qw9Ho395yKCZ .image-shape .label,#mermaid-svg-05I0qw9Ho395yKCZ .icon-shape .label{text-anchor:middle;}#mermaid-svg-05I0qw9Ho395yKCZ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-05I0qw9Ho395yKCZ .rough-node .label,#mermaid-svg-05I0qw9Ho395yKCZ .node .label,#mermaid-svg-05I0qw9Ho395yKCZ .image-shape .label,#mermaid-svg-05I0qw9Ho395yKCZ .icon-shape .label{text-align:center;}#mermaid-svg-05I0qw9Ho395yKCZ .node.clickable{cursor:pointer;}#mermaid-svg-05I0qw9Ho395yKCZ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-05I0qw9Ho395yKCZ .arrowheadPath{fill:#333333;}#mermaid-svg-05I0qw9Ho395yKCZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-05I0qw9Ho395yKCZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-05I0qw9Ho395yKCZ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-05I0qw9Ho395yKCZ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-05I0qw9Ho395yKCZ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-05I0qw9Ho395yKCZ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-05I0qw9Ho395yKCZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-05I0qw9Ho395yKCZ .cluster text{fill:#333;}#mermaid-svg-05I0qw9Ho395yKCZ .cluster span{color:#333;}#mermaid-svg-05I0qw9Ho395yKCZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-05I0qw9Ho395yKCZ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-05I0qw9Ho395yKCZ rect.text{fill:none;stroke-width:0;}#mermaid-svg-05I0qw9Ho395yKCZ .icon-shape,#mermaid-svg-05I0qw9Ho395yKCZ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-05I0qw9Ho395yKCZ .icon-shape p,#mermaid-svg-05I0qw9Ho395yKCZ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-05I0qw9Ho395yKCZ .icon-shape .label rect,#mermaid-svg-05I0qw9Ho395yKCZ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-05I0qw9Ho395yKCZ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-05I0qw9Ho395yKCZ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-05I0qw9Ho395yKCZ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Q5: MVCC 和 Next-Key Lock 的区别?
MVCC
用于快照读(普通 SELECT)
Next-Key Lock
用于当前读(SELECT ... FOR UPDATE)
MVCC 优点
不加锁,性能好
Next-Key Lock 优点
可以防止幻读
Q4: 如何避免幻读?
使用唯一约束
防止重复插入
合理使用加锁
SELECT ... FOR UPDATE
添加索引
避免全表锁
避免长事务
减少并发冲突
避免混用
统一使用快照读或当前读
Q3: 什么时候会出现幻读?
情况 1
没命中索引,锁升级为全表锁
情况 2
只有行锁没有间隙锁(RR 降级为 RU)
情况 3
快照读和当前读混用
Q2: 为什么 InnoDB 默认用 RR 而不是 SERIALIZABLE?
RR 优点
通过 MVCC + Next-Key Lock 抑制幻读
SERIALIZABLE 缺点
所有查询都加锁,性能差
权衡
RR 在性能和一致性之间取得平衡
Q1: 幻读和不可重复读的区别?
不可重复读
同一行数据两次读取值不同
幻读
同一范围查询两次结果行数不同
关键区别
不可重复读是 UPDATE 导致,幻读是 INSERT/DELETE 导致
技术方案分析
Q1: 幻读和不可重复读的区别?
答:
- 不可重复读:同一行数据,两次读取值不同。原因是其他事务 UPDATE 了这一行。例如:第一次读 balance=1000,第二次读 balance=900。
- 幻读:同一范围查询,两次结果行数不同。原因是其他事务 INSERT 或 DELETE 了行。例如:第一次查询返回 10 行,第二次返回 11 行。
- 关键区别:不可重复读是 UPDATE 导致,幻读是 INSERT/DELETE 导致。
Q2: 为什么 InnoDB 默认用 RR 而不是 SERIALIZABLE?
答:
- RR 优点:通过 MVCC(快照读)+ Next-Key Lock(当前读)抑制幻读,性能较好。
- SERIALIZABLE 缺点:所有查询都加锁(包括普通 SELECT),性能极差。
- 权衡:RR 在性能和一致性之间取得平衡,既能抑制幻读,又不会像 SERIALIZABLE 那样性能差。
Q3: 什么时候会出现幻读?
答:三种情况:
- 没命中索引:锁升级为全表锁,性能差但不幻读(因为全表被锁)。
- 只有行锁没有间隙锁:RR 降级为 RU 时,其他事务可以插入新行,导致幻读。
- 快照读和当前读混用:快照读看到旧版本,当前读看到最新版本,导致幻读。
Q4: 如何避免幻读?
答:
- 使用唯一约束:防止重复插入,即使有幻读也能保证数据唯一。
- 合理使用加锁:SELECT ... FOR UPDATE 加 Next-Key Lock,防止其他事务插入。
- 添加索引:避免全表锁,提高性能。
- 避免长事务:减少并发冲突,快进快出。
- 避免混用:同一事务内,要么都用快照读,要么都用当前读。
Q5: MVCC 和 Next-Key Lock 的区别?
答:
- MVCC:用于快照读(普通 SELECT),通过读取事务开始时的快照版本,不加锁,性能好。
- Next-Key Lock:用于当前读(SELECT ... FOR UPDATE),通过锁住已有的行和间隙,防止其他事务插入,可以防止幻读。
- 优点对比:MVCC 性能好(不加锁),Next-Key Lock 可以防止幻读(加锁)。
八、方案总结
#mermaid-svg-O80xzoTKFe7BaP5q{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-O80xzoTKFe7BaP5q .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-O80xzoTKFe7BaP5q .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-O80xzoTKFe7BaP5q .error-icon{fill:#552222;}#mermaid-svg-O80xzoTKFe7BaP5q .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-O80xzoTKFe7BaP5q .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-O80xzoTKFe7BaP5q .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-O80xzoTKFe7BaP5q .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-O80xzoTKFe7BaP5q .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-O80xzoTKFe7BaP5q .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-O80xzoTKFe7BaP5q .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-O80xzoTKFe7BaP5q .marker{fill:#333333;stroke:#333333;}#mermaid-svg-O80xzoTKFe7BaP5q .marker.cross{stroke:#333333;}#mermaid-svg-O80xzoTKFe7BaP5q svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-O80xzoTKFe7BaP5q p{margin:0;}#mermaid-svg-O80xzoTKFe7BaP5q .edge{stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .section--1 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section--1 path,#mermaid-svg-O80xzoTKFe7BaP5q .section--1 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section--1 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section--1 text{fill:#ffffff;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth--1{stroke-width:17;}#mermaid-svg-O80xzoTKFe7BaP5q .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-0 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-0 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-0 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-0 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-0 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-0{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-0{stroke-width:14;}#mermaid-svg-O80xzoTKFe7BaP5q .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-1 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-1 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-1 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-1 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-1 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-1{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-1{stroke-width:11;}#mermaid-svg-O80xzoTKFe7BaP5q .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-2 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-2 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-2 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-2 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-2 text{fill:#ffffff;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-2{stroke-width:8;}#mermaid-svg-O80xzoTKFe7BaP5q .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-3 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-3 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-3 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-3 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-3 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-3{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-3{stroke-width:5;}#mermaid-svg-O80xzoTKFe7BaP5q .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-4 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-4 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-4 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-4 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-4 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-4{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-4{stroke-width:2;}#mermaid-svg-O80xzoTKFe7BaP5q .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-5 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-5 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-5 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-5 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-5 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-5{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-5{stroke-width:-1;}#mermaid-svg-O80xzoTKFe7BaP5q .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-6 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-6 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-6 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-6 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-6 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-6{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-6{stroke-width:-4;}#mermaid-svg-O80xzoTKFe7BaP5q .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-7 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-7 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-7 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-7 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-7 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-7{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-7{stroke-width:-7;}#mermaid-svg-O80xzoTKFe7BaP5q .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-8 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-8 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-8 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-8 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-8 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-8{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-8{stroke-width:-10;}#mermaid-svg-O80xzoTKFe7BaP5q .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-9 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-9 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-9 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-9 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-9 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-9{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-9{stroke-width:-13;}#mermaid-svg-O80xzoTKFe7BaP5q .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-10 rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-10 path,#mermaid-svg-O80xzoTKFe7BaP5q .section-10 circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-10 polygon,#mermaid-svg-O80xzoTKFe7BaP5q .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-10 text{fill:black;}#mermaid-svg-O80xzoTKFe7BaP5q .node-icon-10{font-size:40px;color:black;}#mermaid-svg-O80xzoTKFe7BaP5q .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .edge-depth-10{stroke-width:-16;}#mermaid-svg-O80xzoTKFe7BaP5q .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled,#mermaid-svg-O80xzoTKFe7BaP5q .disabled circle,#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:lightgray;}#mermaid-svg-O80xzoTKFe7BaP5q .disabled text{fill:#efefef;}#mermaid-svg-O80xzoTKFe7BaP5q .section-root rect,#mermaid-svg-O80xzoTKFe7BaP5q .section-root path,#mermaid-svg-O80xzoTKFe7BaP5q .section-root circle,#mermaid-svg-O80xzoTKFe7BaP5q .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-O80xzoTKFe7BaP5q .section-root text{fill:#ffffff;}#mermaid-svg-O80xzoTKFe7BaP5q .section-root span{color:#ffffff;}#mermaid-svg-O80xzoTKFe7BaP5q .section-2 span{color:#ffffff;}#mermaid-svg-O80xzoTKFe7BaP5q .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-O80xzoTKFe7BaP5q .edge{fill:none;}#mermaid-svg-O80xzoTKFe7BaP5q .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-O80xzoTKFe7BaP5q :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} MySQL 幻读
幻读本质
同一事务内两次范围查询结果不一致
读到了其他事务新增的行
只发生在范围查询
可重复读 RR
快照读:MVCC
读取事务开始时的快照版本
不加锁,性能好
避免普通查询场景的幻读
当前读:Next-Key Lock
行锁 + 间隙锁
锁住已有的行和间隙
抑制当前读场景的幻读
三种幻读情况
没命中索引,锁升级为全表锁
只有行锁没有间隙锁(RR 降级为 RU)
快照读和当前读混用
生产环境建议
使用唯一约束
合理使用加锁
添加索引
避免长事务
避免混用
方案要点:
幻读本质:同一事务内两次相同范围查询结果不一致,读到了其他事务新增的行。
可重复读(RR):
- 快照读(普通 SELECT):通过 MVCC 读取事务开始时的快照版本,避免普通查询场景的幻读。
- 当前读(SELECT ... FOR UPDATE):通过 Next-Key Lock(行锁 + 间隙锁)锁住已有的行和间隙,抑制当前读场景的幻读。
三种幻读情况:
- 没命中索引:锁升级为全表锁,性能差但不幻读。
- 只有行锁没有间隙锁:RR 降级为 RU,其他事务可以插入新行,幻读。
- 快照读和当前读混用:看到不同版本,幻读。
生产建议:使用唯一约束、合理加锁、添加索引、避免长事务、避免混用。
九、生产环境 Checklist(部署检查清单)
#mermaid-svg-RKl37P2uqqW4Ukc0{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-RKl37P2uqqW4Ukc0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-RKl37P2uqqW4Ukc0 .error-icon{fill:#552222;}#mermaid-svg-RKl37P2uqqW4Ukc0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RKl37P2uqqW4Ukc0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RKl37P2uqqW4Ukc0 .marker.cross{stroke:#333333;}#mermaid-svg-RKl37P2uqqW4Ukc0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RKl37P2uqqW4Ukc0 p{margin:0;}#mermaid-svg-RKl37P2uqqW4Ukc0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-RKl37P2uqqW4Ukc0 .cluster-label text{fill:#333;}#mermaid-svg-RKl37P2uqqW4Ukc0 .cluster-label span{color:#333;}#mermaid-svg-RKl37P2uqqW4Ukc0 .cluster-label span p{background-color:transparent;}#mermaid-svg-RKl37P2uqqW4Ukc0 .label text,#mermaid-svg-RKl37P2uqqW4Ukc0 span{fill:#333;color:#333;}#mermaid-svg-RKl37P2uqqW4Ukc0 .node rect,#mermaid-svg-RKl37P2uqqW4Ukc0 .node circle,#mermaid-svg-RKl37P2uqqW4Ukc0 .node ellipse,#mermaid-svg-RKl37P2uqqW4Ukc0 .node polygon,#mermaid-svg-RKl37P2uqqW4Ukc0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RKl37P2uqqW4Ukc0 .rough-node .label text,#mermaid-svg-RKl37P2uqqW4Ukc0 .node .label text,#mermaid-svg-RKl37P2uqqW4Ukc0 .image-shape .label,#mermaid-svg-RKl37P2uqqW4Ukc0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-RKl37P2uqqW4Ukc0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-RKl37P2uqqW4Ukc0 .rough-node .label,#mermaid-svg-RKl37P2uqqW4Ukc0 .node .label,#mermaid-svg-RKl37P2uqqW4Ukc0 .image-shape .label,#mermaid-svg-RKl37P2uqqW4Ukc0 .icon-shape .label{text-align:center;}#mermaid-svg-RKl37P2uqqW4Ukc0 .node.clickable{cursor:pointer;}#mermaid-svg-RKl37P2uqqW4Ukc0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-RKl37P2uqqW4Ukc0 .arrowheadPath{fill:#333333;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RKl37P2uqqW4Ukc0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RKl37P2uqqW4Ukc0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-RKl37P2uqqW4Ukc0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RKl37P2uqqW4Ukc0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-RKl37P2uqqW4Ukc0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RKl37P2uqqW4Ukc0 .cluster text{fill:#333;}#mermaid-svg-RKl37P2uqqW4Ukc0 .cluster span{color:#333;}#mermaid-svg-RKl37P2uqqW4Ukc0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RKl37P2uqqW4Ukc0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-RKl37P2uqqW4Ukc0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-RKl37P2uqqW4Ukc0 .icon-shape,#mermaid-svg-RKl37P2uqqW4Ukc0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RKl37P2uqqW4Ukc0 .icon-shape p,#mermaid-svg-RKl37P2uqqW4Ukc0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-RKl37P2uqqW4Ukc0 .icon-shape .label rect,#mermaid-svg-RKl37P2uqqW4Ukc0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RKl37P2uqqW4Ukc0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-RKl37P2uqqW4Ukc0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-RKl37P2uqqW4Ukc0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 读模式
✅ 避免混用快照读和当前读
✅ 统一使用一种读模式
事务管理
✅ 避免长事务
✅ 快进快出
索引优化
✅ 为 WHERE 条件字段加索引
✅ 避免全表锁
加锁策略
✅ 合理使用 SELECT ... FOR UPDATE
✅ 避免长事务持有锁
唯一约束
✅ 给关键字段加唯一约束
✅ 防止重复插入
部署检查清单:
| 类别 | 检查项 | 状态 | 生活化比喻 |
|---|---|---|---|
| 唯一约束 | 给关键字段加唯一约束 | ⬜ | 发唯一身份证号 |
| 防止重复插入 | ⬜ | 防止重复登记 | |
| 加锁策略 | 合理使用 SELECT ... FOR UPDATE | ⬜ | 锁住房子 |
| 避免长事务持有锁 | ⬜ | 快进快出 | |
| 索引优化 | 为 WHERE 条件字段加索引 | ⬜ | 按门牌号锁房子 |
| 避免全表锁 | ⬜ | 避免按小区锁房子 | |
| 事务管理 | 避免长事务 | ⬜ | 减少并发冲突 |
| 快进快出 | ⬜ | 不占座 | |
| 读模式 | 避免混用快照读和当前读 | ⬜ | 统一观察方式 |
| 统一使用一种读模式 | ⬜ | 要么都拍照,要么都实时监控 |
十、总结(一句话带走)
#mermaid-svg-u463uotJJ1rIp8jG{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-u463uotJJ1rIp8jG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-u463uotJJ1rIp8jG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-u463uotJJ1rIp8jG .error-icon{fill:#552222;}#mermaid-svg-u463uotJJ1rIp8jG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-u463uotJJ1rIp8jG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-u463uotJJ1rIp8jG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-u463uotJJ1rIp8jG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-u463uotJJ1rIp8jG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-u463uotJJ1rIp8jG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-u463uotJJ1rIp8jG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-u463uotJJ1rIp8jG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-u463uotJJ1rIp8jG .marker.cross{stroke:#333333;}#mermaid-svg-u463uotJJ1rIp8jG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-u463uotJJ1rIp8jG p{margin:0;}#mermaid-svg-u463uotJJ1rIp8jG .edge{stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .section--1 rect,#mermaid-svg-u463uotJJ1rIp8jG .section--1 path,#mermaid-svg-u463uotJJ1rIp8jG .section--1 circle,#mermaid-svg-u463uotJJ1rIp8jG .section--1 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section--1 text{fill:#ffffff;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth--1{stroke-width:17;}#mermaid-svg-u463uotJJ1rIp8jG .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-0 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-0 path,#mermaid-svg-u463uotJJ1rIp8jG .section-0 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-0 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-u463uotJJ1rIp8jG .section-0 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-0{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-0{stroke-width:14;}#mermaid-svg-u463uotJJ1rIp8jG .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-1 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-1 path,#mermaid-svg-u463uotJJ1rIp8jG .section-1 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-1 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-1 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-1{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-1{stroke-width:11;}#mermaid-svg-u463uotJJ1rIp8jG .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-2 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-2 path,#mermaid-svg-u463uotJJ1rIp8jG .section-2 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-2 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-2 text{fill:#ffffff;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-2{stroke-width:8;}#mermaid-svg-u463uotJJ1rIp8jG .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-3 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-3 path,#mermaid-svg-u463uotJJ1rIp8jG .section-3 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-3 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-3 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-3{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-3{stroke-width:5;}#mermaid-svg-u463uotJJ1rIp8jG .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-4 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-4 path,#mermaid-svg-u463uotJJ1rIp8jG .section-4 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-4 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-4 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-4{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-4{stroke-width:2;}#mermaid-svg-u463uotJJ1rIp8jG .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-5 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-5 path,#mermaid-svg-u463uotJJ1rIp8jG .section-5 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-5 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-5 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-5{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-5{stroke-width:-1;}#mermaid-svg-u463uotJJ1rIp8jG .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-6 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-6 path,#mermaid-svg-u463uotJJ1rIp8jG .section-6 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-6 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-6 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-6{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-6{stroke-width:-4;}#mermaid-svg-u463uotJJ1rIp8jG .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-7 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-7 path,#mermaid-svg-u463uotJJ1rIp8jG .section-7 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-7 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-7 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-7{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-7{stroke-width:-7;}#mermaid-svg-u463uotJJ1rIp8jG .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-8 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-8 path,#mermaid-svg-u463uotJJ1rIp8jG .section-8 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-8 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-8 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-8{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-8{stroke-width:-10;}#mermaid-svg-u463uotJJ1rIp8jG .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-9 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-9 path,#mermaid-svg-u463uotJJ1rIp8jG .section-9 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-9 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-9 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-9{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-9{stroke-width:-13;}#mermaid-svg-u463uotJJ1rIp8jG .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-10 rect,#mermaid-svg-u463uotJJ1rIp8jG .section-10 path,#mermaid-svg-u463uotJJ1rIp8jG .section-10 circle,#mermaid-svg-u463uotJJ1rIp8jG .section-10 polygon,#mermaid-svg-u463uotJJ1rIp8jG .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-10 text{fill:black;}#mermaid-svg-u463uotJJ1rIp8jG .node-icon-10{font-size:40px;color:black;}#mermaid-svg-u463uotJJ1rIp8jG .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .edge-depth-10{stroke-width:-16;}#mermaid-svg-u463uotJJ1rIp8jG .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-u463uotJJ1rIp8jG .disabled,#mermaid-svg-u463uotJJ1rIp8jG .disabled circle,#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:lightgray;}#mermaid-svg-u463uotJJ1rIp8jG .disabled text{fill:#efefef;}#mermaid-svg-u463uotJJ1rIp8jG .section-root rect,#mermaid-svg-u463uotJJ1rIp8jG .section-root path,#mermaid-svg-u463uotJJ1rIp8jG .section-root circle,#mermaid-svg-u463uotJJ1rIp8jG .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-u463uotJJ1rIp8jG .section-root text{fill:#ffffff;}#mermaid-svg-u463uotJJ1rIp8jG .section-root span{color:#ffffff;}#mermaid-svg-u463uotJJ1rIp8jG .section-2 span{color:#ffffff;}#mermaid-svg-u463uotJJ1rIp8jG .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-u463uotJJ1rIp8jG .edge{fill:none;}#mermaid-svg-u463uotJJ1rIp8jG .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-u463uotJJ1rIp8jG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} MySQL 幻读
核心要点
幻读本质
同一事务内两次范围查询结果不一致
读到了其他事务新增的行
可重复读 RR
快照读:MVCC(避免幻读)
当前读:Next-Key Lock(抑制幻读)
三种幻读情况
没命中索引
只有行锁没有间隙锁
快照读和当前读混用
生产建议
唯一约束
合理加锁
添加索引
避免长事务
避免混用
🎯 一句话总结
幻读 = 同一事务内两次范围查询结果不一致;InnoDB 通过 MVCC(快照读)+ Next-Key Lock(当前读)抑制幻读,但仍有边界情况(混用、无索引、降级)。
🧠 核心要点
- 幻读是什么? 同一事务内两次范围查询结果不一致,读到了"幻影行"。
- RR 如何解决幻读? 快照读通过 MVCC,当前读通过 Next-Key Lock。
- 三种幻读情况? 没命中索引、只有行锁没有间隙锁、快照读和当前读混用。
- 如何避免幻读? 唯一约束、合理加锁、添加索引、避免长事务、避免混用。
- MVCC vs Next-Key Lock? MVCC 用于快照读(不加锁),Next-Key Lock 用于当前读(加锁)。