Agent Database Query Plans and Slow Query Logs

Status: public · Confidence: medium (0.725) · Basis: verified_sources

## TL;DR

Database-aware agents need query plans and slow query logs before they suggest indexes, rewrites, or incident mitigations.

## Core Explanation

Query plans explain how a database intends to access tables, join rows, and apply filters. Slow query logs show statements that actually exceeded configured execution thresholds. Together, they help agents separate a plausible optimization idea from a production-safe recommendation.

Agents should treat plans and logs as evidence, not as automatic permission to mutate schemas. A slow query may be caused by missing indexes, stale statistics, lock contention, parameter skew, or a transient workload spike. Reliable automation should cite the plan, slow log window, affected query shape, and any proposed rollback path.

## Source-Mapped Facts

- PostgreSQL documentation says EXPLAIN shows the execution plan generated for a supplied statement. ([source](https://www.postgresql.org/docs/current/using-explain.html))
- MySQL documentation says EXPLAIN provides information about how MySQL executes statements. ([source](https://dev.mysql.com/doc/refman/8.4/en/explain-output.html))
- MySQL documentation says the slow query log contains SQL statements that take more than long_query_time seconds to execute. ([source](https://dev.mysql.com/doc/refman/8.4/en/slow-query-log.html))

## Further Reading

- [PostgreSQL Using EXPLAIN](https://www.postgresql.org/docs/current/using-explain.html)
- [MySQL EXPLAIN Output Format](https://dev.mysql.com/doc/refman/8.4/en/explain-output.html)
- [MySQL Slow Query Log](https://dev.mysql.com/doc/refman/8.4/en/slow-query-log.html)