← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Slow Query Log

performance PHP 5.0+ Beginner

Also Known As

MySQL slow query log slow log long_query_time

TL;DR

A database log of queries exceeding a time threshold — the first place to look when diagnosing PHP application performance problems.

Explanation

MySQL's slow query log records queries taking longer than long_query_time (set to 1–2 seconds in production, 0 in development to catch all queries). Enable with slow_query_log=ON and log_queries_not_using_indexes=ON. Tools like mysqldumpslow, pt-query-digest (Percona Toolkit), or MySQL Workbench aggregate and rank slow queries by frequency and total time. In PHP applications, ORMs can hide expensive queries — the slow query log surfaces them regardless of abstraction layer. Pair with EXPLAIN to understand why a slow query is slow and what index would fix it.

Common Misconception

The slow query log only captures queries over the long_query_time threshold. With log_queries_not_using_indexes=ON, the slow log also captures fast queries that perform full table scans — often more valuable than catching slow queries that already have alerts.

Why It Matters

The slow query log captures every query exceeding a configurable threshold — it is the fastest way to find which queries are degrading production performance without any application code changes.

Common Mistakes

  • Not enabling the slow query log in production — slow queries are invisible without it.
  • Setting long_query_time too high (10s) — queries taking 500ms still cause user-visible latency.
  • Not enabling log_queries_not_using_indexes — catches missing index issues the time threshold misses.
  • Not rotating or monitoring the slow query log file — it grows indefinitely and can fill the disk.

Code Examples

✗ Vulnerable
# my.cnf — slow query log disabled:
[mysqld]
# slow_query_log = 1          ; Not set
# slow_query_time = 0.5       ; Not set — use 0.5s not 10s
# log_queries_not_using_indexes = 1 ; Not set

# Correct:
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 0.5
log_queries_not_using_indexes = 1
✓ Fixed
; MySQL slow query log — log queries exceeding threshold
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1          ; seconds — log queries over 1s
log_queries_not_using_indexes = 1  ; also log full-table scans

; Analyse with pt-query-digest (Percona Toolkit)
$ pt-query-digest /var/log/mysql/slow.log
; Shows: call count, total time, average, worst offenders

; PostgreSQL equivalent:
; log_min_duration_statement = 1000   ; milliseconds
; log_destination = 'csvlog'

Added 15 Mar 2026
Edited 22 Mar 2026
Views 32
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 1 ping T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 0 pings F 3 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T
No pings yet today
Perplexity 9 Amazonbot 8 Unknown AI 3 Ahrefs 2 Google 2 ChatGPT 1 SEMrush 1
crawler 24 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Enable slow_query_log with long_query_time=0.1, run EXPLAIN on flagged queries, add covering index on WHERE + ORDER BY columns
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
MySQL slow_query_log entries; EXPLAIN showing type:ALL full table scan; Laravel Debugbar query times >100ms
Auto-detectable: ✓ Yes mysql-slow-query-log laravel-debugbar clockwork pt-query-digest
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Low ✗ Manual fix Fix: Medium Context: File

✓ schema.org compliant