MySQL行锁与间隙锁:Next-Key锁并发异常治理
概览
- InnoDB 在
REPEATABLE READ 下通过间隙锁与 Next-Key 锁防止幻读;锁范围与索引影响并发。
- 不当语句可能扩大锁范围导致阻塞与死锁。
技术参数(已验证)
- 行锁:基于索引记录锁;更新/删除时加锁命中记录。
- 间隙锁:锁定索引间隙防止插入;与唯一索引/范围条件相关。
- Next-Key 锁:记录锁 + 间隙锁;在范围扫描时保护区间。
- 死锁与阻塞:记录锁冲突与等待;通过
SHOW ENGINE INNODB STATUS 诊断。
- 建议:精确索引与等值条件避免不必要范围锁;必要时降级隔离或改用悲观/乐观方案。
实战清单
- 为关键查询建立合适索引;避免全表与模糊范围扫描。
- 记录死锁事件与锁等待;优化事务长度与批量大小。
- 在争用场景审查语句与隔离级别;建立回退与重试策略。
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-09-28 05:52:57 HTTP/2.0 GET : www.ybb.press/database/2125.html
- 运行时间 : 0.557491s [ 吞吐率:1.79req/s ] 内存消耗:5,316.47kb 文件加载:115
- 查询信息 : 19 queries 2 writes
- 缓存信息 : 36 reads,3 writes
- 配置加载 : 80
- 会话信息 : SESSION_ID=1k2d7vguips0t27ekof0rjecv7
- [ BEHAVIOR ] Run Closure @app_init [ RunTime:0.000092s ]
- [ CACHE ] INIT File
- [ BEHAVIOR ] Run \addons\cms\Cms @app_init [ RunTime:0.009555s ]
- [ BEHAVIOR ] Run \addons\fastchat\Fastchat @app_init [ RunTime:0.000170s ]
- [ BEHAVIOR ] Run \addons\log\Log @app_init [ RunTime:0.000375s ]
- [ BEHAVIOR ] Run \addons\shop\Shop @app_init [ RunTime:0.004444s ]
- [ BEHAVIOR ] Run \addons\yunping\Yunping @app_init [ RunTime:0.000502s ]
- [ BEHAVIOR ] Run Closure @app_init [ RunTime:0.000650s ]
- [ BEHAVIOR ] Run app\common\behavior\Common @app_init [ RunTime:0.001501s ]
- [ LANG ] /www/wwwroot/yebinbing/thinkphp/lang/zh-cn.php
- [ BEHAVIOR ] Run app\common\behavior\Common @app_dispatch [ RunTime:0.000113s ]
- [ ROUTE ] array (
'type' => 'method',
'method' =>
array (
0 => '\\think\\addons\\Route',
1 => 'execute',
),
'var' =>
array (
'addon' => 'cms',
'controller' => 'archives',
'action' => 'index',
),
)
- [ HEADER ] array (
'x-forwarded-proto' => 'https',
'cf-visitor' => '{"scheme":"https"}',
'cf-ipcountry' => 'US',
'cf-connecting-ip' => '216.73.216.103',
'cdn-loop' => 'cloudflare; loops=1',
'user-agent' => 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; [email protected])',
'accept-encoding' => 'gzip, br',
'accept' => '*/*',
'cf-ray' => 'a41dbd1029156483-CMH',
'x-forwarded-for' => '216.73.216.103',
'host' => 'www.ybb.press',
'content-length' => '',
'content-type' => '',
)
- [ PARAM ] array (
'catename' => 'database',
'id' => '2125',
)
- [ RUN ] think\addons\Route->execute[ /www/wwwroot/yebinbing/vendor/fastadminnet/fastadmin-addons/src/addons/Route.php ]
- [ LANG ] /www/wwwroot/yebinbing/public/../application/common/lang/zh-cn/addon.php
- [ BEHAVIOR ] Run app\common\behavior\Common @addon_begin [ RunTime:0.004604s ]
- [ LANG ] /www/wwwroot/yebinbing/addons/cms/lang/zh-cn.php
- [ DB ] INIT mysql
- [ VIEW ] /www/wwwroot/yebinbing/addons/cms/view/default/show_article.html [ array (
0 => 'config',
1 => 'user',
2 => 'site',
3 => '__CHANNEL__',
4 => 'isWechat',
5 => 'canonical_url',
6 => '__ARCHIVES__',
7 => '__MODEL__',
) ]
- [ SESSION ] INIT array (
'id' => '',
'var_session_id' => '',
'prefix' => 'think',
'type' => '',
'auto_start' => true,
)
- [ BEHAVIOR ] Run \addons\cms\Cms @view_filter [ RunTime:0.000370s ]
- [ BEHAVIOR ] Run \addons\shop\Shop @view_filter [ RunTime:0.000136s ]

0.560799s
发表评论 取消回复