Lesson 8 of 9 · 25m
Observability, Slow Queries, and Query Digests
Build trustworthy visibility with Performance Schema, slow query logs, digests, latency percentiles, and operational alerts tied to user impact.
If you cannot answer 'what changed?' during a database incident, you do not have observability; you have a pile of metrics. Good MySQL operations rely on query digests, wait evidence, resource saturation, and error context. A slow-query log alone is better than nothing, but it is easy to drown in raw SQL while missing the one new deployment that multiplied rows examined by a thousand.
Query digests are the unit of analysis
MySQL Performance Schema normalizes statements into digests, turning thousands of literal variants into one workload shape:
SELECT DIGEST_TEXT, COUNT_STAR, SUM_TIMER_WAIT/1e12 AS total_seconds,
SUM_ROWS_EXAMINED, SUM_ROWS_SENT
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;A query with low average latency can still dominate total database time because it runs constantly. Optimize total cost and user-critical latency, not whichever query looks worst in a single sample.
Use the slow log deliberately
slow_query_log = 1
long_query_time = 0.5
log_queries_not_using_indexes = 0
slow_query_log_file = /var/log/mysql/mysql-slow.logLowering long_query_time increases visibility and noise. Logging every query not using an index can explode volume on systems where small lookup tables are scanned legitimately. Tune collection to the question you are asking, then rotate and retain logs intentionally.
Tools such as pt-query-digest aggregate patterns:
pt-query-digest /var/log/mysql/mysql-slow.log --since '2026-11-30 00:00:00' \
--until '2026-11-30 06:00:00' > digest-report.txtRead beyond the first query. Incident causality often sits in a combination: one query scanning more rows, one migration holding locks, and one backup raising I/O latency.
Metrics need saturation, errors, and latency shape
At minimum, monitor:
CPU, run queue, memory pressure, and swap
disk latency, throughput, utilization, and InnoDB dirty-page behavior
connections, active threads, replication lag, and apply errors
query throughput, latency percentiles, deadlocks, and lock waits
slow query rate by digestPercentiles matter because averages hide pain. A p50 of 3 ms and p99 of 2 seconds means a meaningful slice of users or dependent jobs is in trouble.
Alert on symptoms, then correlate causes
User-facing symptoms include elevated error rate, p95/p99 latency, saturated disk latency, replication apply failure, and connection starvation. Dangerous precursors include long transactions, growing undo history, dirty-page buildup, abnormal row scans, and backup duration growth. Alert thresholds should route to action, not merely generate dread.
When latency rises, compare deploy events, migrations, backup windows, traffic shifts, failover events, cron jobs, and storage maintenance. MySQL often wears the symptom while the cause sits in application concurrency, queue retries, object storage timeouts, or noisy neighbors.
Field notes
- Performance Schema has overhead, but disabling it entirely leaves you blind; configure consumers selectively.
- Digest text hides literals, which helps privacy and aggregation but can hide skewed parameter evidence.
EXPLAINin production is usually safe for reads, butEXPLAIN ANALYZEexecutes the statement.- Long query timeouts in applications can mask database queueing while retry storms amplify load.
- Dashboards should link a metric to the investigation query or runbook step that follows it.
Was this lesson helpful?