Course contentsShow
← System Design
Lesson 287 of 1,9198. Database Performance OptimizationFree lesson

Monitoring Slow Queries

Using slow query logs and APM tools to identify and prioritize optimization opportunities in production.

Monitoring Slow Queries

What you'll learn: How to discover which database queries are hurting your production performance and decide what to fix first.


The Performance Detective Work

You've learned techniques like indexing, pagination, and query rewriting—but how do you know which queries in your live system actually need help? Monitoring slow queries is like installing speed cameras on your database highways: you capture the slowest traffic and identify where bottlenecks happen in real usage, not just in theory.

Two Essential Tools

Slow Query Logs are built into most databases (MySQL, PostgreSQL, etc.). You configure a threshold—say, queries taking longer than 500ms—and the database writes these offenders to a log file with details like execution time, rows examined, and the actual SQL statement.

Think of it as your database keeping a journal: "At 2:47 PM, someone ran this monster query that took 3 seconds and scanned 2 million rows."

Application Performance Monitoring (APM) tools like New Relic, Datadog, or AppDynamics go further. They automatically track query performance across your entire application, showing you frequency (how often a query runs), percentile latencies (p95, p99), and which code paths trigger slow queries.

Prioritizing What Matters

Not all slow queries deserve immediate attention. A report that runs once daily and takes 2 seconds? Probably fine. A checkout query that runs 10,000 times per hour and takes 800ms? That's your top priority—it's affecting many users and consuming massive database resources.

Use your monitoring data to calculate total impact: frequency × average_duration. Optimize the highest-impact queries first.

Key Takeaway: Slow query logs and APM tools show you real production bottlenecks—prioritize fixes by combining frequency and execution time to maximize performance gains.