{
    "slug": "slow_query_log",
    "term": "Slow Query Log",
    "category": "performance",
    "difficulty": "beginner",
    "short": "A database log of queries exceeding a time threshold — the first place to look when diagnosing PHP application performance problems.",
    "long": "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.",
    "aliases": [
        "MySQL slow query log",
        "slow log",
        "long_query_time"
    ],
    "tags": [
        "performance",
        "database",
        "debugging",
        "devops"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "query_plan",
        "query_optimisation",
        "profiling",
        "n_plus_one"
    ],
    "prerequisites": [
        "database_indexing",
        "query_optimisation",
        "db_explain_advanced"
    ],
    "refs": [
        "https://dev.mysql.com/doc/refman/8.0/en/slow-query-log.html"
    ],
    "bad_code": "# my.cnf — slow query log disabled:\n[mysqld]\n# slow_query_log = 1          ; Not set\n# slow_query_time = 0.5       ; Not set — use 0.5s not 10s\n# log_queries_not_using_indexes = 1 ; Not set\n\n# Correct:\nslow_query_log = 1\nslow_query_log_file = /var/log/mysql/slow.log\nlong_query_time = 0.5\nlog_queries_not_using_indexes = 1",
    "good_code": "; MySQL slow query log — log queries exceeding threshold\n[mysqld]\nslow_query_log = 1\nslow_query_log_file = /var/log/mysql/slow.log\nlong_query_time = 1          ; seconds — log queries over 1s\nlog_queries_not_using_indexes = 1  ; also log full-table scans\n\n; Analyse with pt-query-digest (Percona Toolkit)\n$ pt-query-digest /var/log/mysql/slow.log\n; Shows: call count, total time, average, worst offenders\n\n; PostgreSQL equivalent:\n; log_min_duration_statement = 1000   ; milliseconds\n; log_destination = 'csvlog'",
    "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",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/slow_query_log",
        "html_url": "https://codeclaritylab.com/glossary/slow_query_log",
        "json_url": "https://codeclaritylab.com/glossary/slow_query_log.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Slow Query Log](https://codeclaritylab.com/glossary/slow_query_log) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/slow_query_log"
            }
        }
    }
}