深度解析:ExplorerPatcher如何让Windows 11找回熟悉的操作体验
2026/8/10 21:55:35
为 PHP 项目添加慢查询监控告警,是保障数据库性能与系统稳定性的核心防线。
my.cnf):slow_query_log = 1 slow_query_log_file = /var/log/mysql/slow.log long_query_time = 1 # 超过 1 秒记录 log_queries_not_using_indexes = 1 # 记录未用索引的查询SHOWVARIABLESLIKE'slow_query%';// app/Providers/AppServiceProvider.phppublicfunctionboot(){if(app()->environment('production')){DB::listen(function($query){// 记录超过 500ms 的查询if($query->time>500){Log::warning('Slow query detected',['sql'=>$query->sql,'bindings'=>$query->bindings,'time'=>$query->time,'url'=>request()->fullUrl()??'CLI','user_id'=>auth()->id()??'guest']);}});}}✅优势:
- 关联业务上下文(URL、用户 ID)
- 无需 DBA 权限
CREATETABLE`slow_queries`(`id`BIGINTAUTO_INCREMENTPRIMARYKEY,`sql`TEXTNOTNULL,`time_ms`INTNOTNULL,`url`VARCHAR(500),`user_id`BIGINT,`created_at`TIMESTAMPDEFAULTCURRENT_TIMESTAMP);// 在 DB::listen 中DB::connection('monitoring')->table('slow_queries')->insert(['sql'=>$query->sql,'time_ms'=>$query->time,'url'=>request()->fullUrl()??'CLI','user_id'=>auth()->id()??null,]);⚠️注意:
使用独立数据库连接(monitoring),避免影响主业务
filter{if[message]=~"Slow query detected"{mutate{add_tag=>["slow_query"]}}}tags:slow_query且count > 5/5min// app/Services/SlowQueryAlert.phpclassSlowQueryAlert{publicstaticfunctiontrigger($query){// 避免告警风暴if(Cache::has('slow_query_alert_sent')){return;}// 发送企业微信/钉钉Http::post(config('alert.webhook'),['msgtype'=>'text','text'=>['content'=>"🚨 慢查询告警\nSQL:{$query['sql']}\n耗时:{$query['time']}ms\nURL:{$query['url']}"]]);// 5 分钟内不再告警Cache::put('slow_query_alert_sent',true,300);}}// 在 DB::listen 中调用if($query->time>1000){// 超过 1 秒才告警SlowQueryAlert::trigger($query);}mysqld_exporter采集slow_queries指标rate(mysql_slow_queries[5m]) > 0.1// 在记录慢查询时$explain=DB::select("EXPLAIN{$query->sql}",$query->bindings);Log::info('Slow query EXPLAIN',['plan'=>$explain]);[{"type":"ALL","possible_keys":null,"key":null,"rows":100000,"Extra":"Using where"}]"type": "ALL"表示全表扫描 →急需添加索引pt-index-usage(Percona Toolkit):pt-index-usage /var/log/mysql/slow.log --host=localhostALTER TABLE `orders` ADD INDEX `idx_user_id` (`user_id`);| 陷阱 | 解决方案 |
|---|---|
| 日志爆炸 | 设置long_query_time=1(非 0.1 秒) |
| 生产环境性能影响 | 应用层监听仅记录 >500ms 查询 |
| 敏感信息泄露 | 过滤 SQL 中的密码/手机号:str_replace($bindings, '***', $sql) |
| 告警疲劳 | 实现告警抑制(5 分钟内相同 SQL 不重复告警) |
“慢查询监控不是为了惩罚代码,
而是为了照亮性能的盲区——
每一次告警,
都是系统在向你低语:
‘这里,还有优化的空间。’”
- 当你忽略慢查询,
你在积累技术债;- 当你响应慢查询,
你在加固系统的护城河。真正的工程能力,
体现在对每一毫秒延迟的敬畏中。
从今天起,为你的 PHP 项目添加慢查询监控:
因为最好的性能优化,
始于对问题的精准捕获,
而非事后的亡羊补牢。