The Slow Query Sentinel: A Zero-Dollar Server That Explains Your Database's Worst SQL
说明与用途
Sentinel 自动识别 PostgreSQL 最慢查询,发送给免费 LLM,获取简明解释和修复建议,加速性能排查。
问题
On-call engineers often spend long time diagnosing slow queries after a sudden performance drop, typically by manually inspecting EXPLAIN output and identifying root causes such as index regressions.
结果
The sentinel reduces the time from noticing a slow query to understanding its root cause from hours to minutes, providing clear, high-impact fixes such as index suggestions.
工作流
- 1. Prepare a free server database: enable pg_stat_statements and create a query_advice table.
- 2. Write a collector that queries pg_stat_statements for top queries, fetches EXPLAIN (FORMAT JSON) for each, and stages the data.
- 3. Implement a model caller that sends query text and plan JSON to a free LLM endpoint, requesting a structured JSON answer with advice and impact.
- 4. Parse the model response and insert advice into query_advice table.
- 5. Query the table to rank recommendations by impact, presenting the highest-value fixes first.
证据
The sentinel I describe below pulls the slowest queries from pg_stat_statements, formats them with their execution plans, and asks a free model to explain the root cause in plain language.
A typical response looks like this: the model points out a sequential scan on a large table, suggests a composite index, and marks the impact as high.
- 记录日期: 2026-09-06T23:37:16.140Z