A slow SQL statement is easier to diagnose when it is connected to the request, user journey, and code path that issued it. Application tracing provides that context.
Capture query timing
Record query duration, normalized statement identity, rows returned where available, and a trace or request identifier. Avoid logging raw sensitive values or full user-submitted content.
Detect common patterns
Look for N+1 queries, repeated lookups, unbounded result sets, missing indexes, lock waits, and queries executed inside loops. Compare query count and cumulative database time across similar requests.
Combine traces with database tools
Use slow-query logs and execution plans to understand the database side. Application traces show where the query originates; database plans show how the engine executes it.
Protect production
Sampling and thresholds can reduce overhead. Keep retention limited and ensure query logs cannot expose credentials or personal information.
Checklist
- Correlate queries with request or trace IDs.
- Track query count and cumulative SQL time.
- Normalize statements and avoid sensitive values.
- Investigate plans and application call sites together.
- Sample and retain telemetry responsibly.