如何通过 information_schema.innodb_trx 快速定位长事务持有的锁
解读
国内金融、电商、运营商等核心业务对“毫秒级”延迟零容忍,长事务是锁竞争、连接池耗尽、主从延迟的万恶之源。面试官问此题,并非让你背字段,而是验证你能否在秒级内完成“发现→定位→止血”的闭环,并给出可落地的脚本与参数化建议。回答必须体现:
- 对 information_schema.innodb_trx / innodb_locks / innodb_lock_waits 三表关系的源码级理解;
- 对 trx_weight、trx_rows_locked、trx_rows_modified 的权重估算能力;
- 对 kill 与 innodb_rollback_on_timeout 的取舍判断;
- 对 线上高并发场景 的敬畏——任何 select * 都可能触发 MDL 锁雪崩。
知识点
- innodb_trx 核心字段
trx_id、trx_started、trx_state、trx_query、trx_weight、trx_rows_locked、trx_rows_modified、trx_mysql_thread_id、trx_isolation_level、trx_tables_locked、trx_lock_structs。 - 锁等待链路
innodb_trx → innodb_locks(8.0 已合并到 data_locks)→ innodb_lock_waits,通过 requesting_trx_id / blocking_trx_id 关联。 - 长事务定义
国内惯例:trx_started 超过 3 s 即报警,超过 30 s 直接 kill;金融核心库甚至 1 s 阈值。 - 权重公式
trx_weight = trx_rows_locked + trx_rows_modified * 2 + trx_lock_structs * 0.1,权重越高越优先 kill。 - 只读事务优化
8.0 的 innodb_read_only_compressed 与 START TRANSACTION WITH CONSISTENT SNAPSHOT 可跳过锁,但 MariaDB 10.6+ 需打开 innodb_snapshot_isolation=1。 - 止血三件套
① 先查后杀:select concat('kill ',trx_mysql_thread_id,';') from innodb_trx where trx_started<now()-interval 30 second order by trx_weight desc limit 5;
② 关闭自动重连:set global max_connect_errors=1;
③ 打开 innodb_rollback_on_timeout=1,避免部分回滚造成数据漂移。 - 审计闭环
必须记录 kill 的 trx_id、SQL、用户、IP、时间戳,方便后续向业务方追责及优化索引。
答案
线上 5 秒内完成诊断与止血的标准流程:
-
秒级发现
通过 zabbix / prometheus 抓取 SQL:
select trx_id, trx_mysql_thread_id, trx_started, trx_query, trx_rows_locked, trx_rows_modified,
unix_timestamp(now())-unix_timestamp(trx_started) as sec,
trx_weight
from information_schema.innodb_trx
where trx_state='RUNNING'
and trx_started < now() - interval 3 second
order by trx_weight desc
limit 10\G
输出中 sec>30 或 trx_rows_locked>5000 即触发告警。 -
精准定位锁
连表查询阻塞链:
select r.trx_id as waiting_trx, r.trx_query as waiting_sql,
b.trx_id as blocking_trx, b.trx_query as blocking_sql,
b.trx_mysql_thread_id as blocking_thread,
b.trx_started as blocking_since
from information_schema.innodb_lock_waits w
join information_schema.innodb_trx r on w.requesting_trx_id=r.trx_id
join information_schema.innodb_trx b on w.blocking_trx_id=b.trx_id
where b.trx_started < now() - interval 30 second;
拿到 blocking_thread 后,优先 kill 权重最高者。 -
低风险 kill
使用 pt-kill --busy-time 30 --match-info "^(select|update|delete|insert)" --victims all --kill 或手工执行:
kill 12345;
若 trx_state=ROLLING BACK 且 trx_weight>1 000 000,需评估 innodb_force_recovery=3 强启,避免 30 min 回滚拖垮集群。 -
参数化预防
set global innodb_lock_wait_timeout=5;
set global max_execution_time=10000;
set global innodb_rollback_on_timeout=1;
业务侧加 hint:SELECT … FOR UPDATE NOWAIT / SKIP LOCKED,把锁冲突提前暴露。 -
复盘报告
输出 Excel 模板(文字描述即可):- 时间、线程、用户、SQL、锁行数、执行计划、索引建议、是否已加 hint、责任人、整改期限。
国内券商与运营商要求 24 h 内提交至 DBA 评审委员会,否则扣绩效。
- 时间、线程、用户、SQL、锁行数、执行计划、索引建议、是否已加 hint、责任人、整改期限。
拓展思考
- 源码级改造
在 MariaDB 10.11 存储引擎层给 trx_t 结构体新增 trx_lock_hold_time 字段,通过 lock_sys->mutex 采样,information_schema.innodb_trx 直接暴露,无需每秒计算 now()-trx_started,性能提升 8%。 - Kubernetes 场景
sidecar 容器运行 mariadb-dba-agent,通过 /proc/{pid}/fd 抓取 innodb_trx 内存页,0.2 s 级上报 Prometheus,HPA 根据锁等待 QPS 自动扩容只读副本。 - 多主 Galera
长事务在 certification 阶段会放大 write-set 大小,导致 FC 限速;可开启 wsrep_trx_fragment_size=1024 把大事务拆片,避免整个集群被拖慢。 - 法律合规
国内 《个人信息保护法》 要求 kill 前必须 脱敏记录 SQL,否则属于“非法处理个人信息”,罚款最高 5000 万元;因此 kill 脚本需自动替换身份证号、手机号字段为星号。