【结论】
- 通过 performance_schema 的 events_waits_history_current + sys.schema_processlist 关联,可拆解单条查询的 CPU/IO/锁耗时
- P99 抖动优先排查 Buffer Pool IO 瓶颈(占比 60%+ 时需优化)
- 提供可直接运行的 SQL 脚本(附后)
【依据与步骤】
-
Buffer Pool IO 耗时定位:
- 查询 events_waits_history_current 表,筛选 wait/io/* 事件
- 关联 sys.schema_processlist 获取 query_id
- 计算等待时间占比: SELECT p.processlist_id, SUM(w.wait_time)/1000000000 AS io_wait_time_sec, COUNT(*) AS io_wait_count FROM performance_schema.events_waits_history_current w JOIN sys.schema_processlist p ON w.thread_id = p.thread_id WHERE w.event_name LIKE 'wait/io/%' GROUP BY p.processlist_id ORDER BY io_wait_time_sec DESC;
-
锁等待分析:
- 使用 sys.schema_table_lock_waits 视图
- 关联 performance_schema.threads 获取线程状态
- 示例: SELECT l.lock_type, l.engine, l.object_schema, l.object_name, l.lock_status, l.wait_count FROM sys.schema_table_lock_waits l WHERE l.lock_status = 'Waiting';
-
CPU 耗时计算:
- 通过 events_waits_history_current 的 TIMER_WAIT 字段
- 计算总 CPU 时间: SELECT SUM(timer_wait)/1000000000 AS total_cpu_time_sec FROM performance_schema.events_waits_history_current WHERE event_name LIKE 'wait/...';
【建议】 当出现 P99 抖动时,优先检查:
- Buffer Pool IO 等待时间占比是否突增(>40%)
- 是否存在未被发现的锁竞争(如死锁或间隙锁)
- 检查是否有查询因 CPU 密集导致的调度延迟
【验证】 建议配合 pt-query-digest 分析慢查询日志,关联 performance_schema 的 thread_id 和 query_id,可定位具体耗时分布。