Skip to content

Slow Query Monitoring

antarr edited this page Apr 10, 2026 · 2 revisions

Slow Query Monitoring

MySQLGenius captures slow SELECT queries in real time using ActiveSupport notifications and stores them in Redis.

Setup

1. Configure Redis

MysqlGenius.configure do |config|
  config.redis_url = ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0"
  config.slow_query_threshold_ms = 250  # default: 250ms
end

2. That's It

MySQLGenius subscribes to sql.active_record notifications automatically when redis_url is configured. Any SELECT query exceeding the threshold is captured.

What Gets Captured

For each slow query:

  • SQL text (first 2,000 characters)
  • Duration in milliseconds
  • Timestamp

What Gets Ignored

  • Non-SELECT queries (INSERT, UPDATE, DELETE, etc.)
  • EXPLAIN queries
  • SCHEMA queries (migrations, schema lookups)

Viewing Slow Queries

The Slow Queries tab shows the most recent 200 captured queries, sorted by most recent first. Each entry shows:

  • Duration badge (color-coded: blue < 1s, yellow 1-2s, red > 2s)
  • Timestamp
  • SQL with syntax highlighting
  • Explain button to see the execution plan
  • Use button to copy the SQL into the Query Explorer

Dashboard Integration

The Dashboard tab shows the top 5 slowest queries from the captured list, linking directly to the Slow Queries tab for details.

Redis Key

Slow queries are stored in a Redis list under the key mysql_genius:slow_queries. The list is capped at 200 entries (oldest are trimmed automatically).

Disabling

Set redis_url to nil to disable slow query monitoring entirely. The Slow Queries tab will show an empty state.

Performance Impact

The notification subscriber is lightweight -- it only fires for queries that exceed the threshold and performs a single Redis LPUSH + LTRIM. There is no measurable impact on application performance.


< Dark Theme | Home >

Clone this wiki locally