Skip to content

Reading the MySQL slow query log on a cPanel host without drowning in noise

The slow log file is unreadable raw. Aggregating with pt-query-digest reveals which account, which query, and which page is responsible — not the once-a-day analytics export.

You enabled the MySQL slow query log on a cPanel host, waited a day, and now you have a 280MB text file that nobody is going to scroll. Opening it in less returns a wall of timestamps, query times, and SQL fragments in no useful order. The first three queries you see are a back-office report and a couple of analytics imports — none of which the customer is complaining about. The actual culprit is somewhere in the noise, and reading the file linearly will not find it.

What’s happening

A raw slow log lists statements in execution order. A query that runs 40 times per page load at 200ms each contributes 8 seconds per page to wall-clock time, but each entry is a single 0.2s line buried between 12-second analytics jobs. Eyeballing the file ranks by single-execution time, which is exactly the wrong ranking. The fix is to aggregate by query fingerprint and rank by total time consumed — pt-query-digest from the Percona Toolkit does this in one command, and it ships with the percona-toolkit package on most cPanel hosts.

How to verify

which pt-query-digest || yum install percona-toolkit
ls -lh /var/log/mysql/slow.log
mysql -e "SHOW VARIABLES LIKE 'long_query_time'"
mysql -e "SHOW STATUS LIKE 'Slow_queries'"

Confirm the tool is installed, the log is what you expect, and the threshold is sensible. If Slow_queries is climbing by hundreds per minute and the file is small, the log path is wrong — MySQL is writing somewhere else. Check SHOW VARIABLES LIKE 'slow_query_log_file'.

The fix

  1. Aggregate first, read second:
    pt-query-digest /var/log/mysql/slow.log > /tmp/slow-digest.txt
    less /tmp/slow-digest.txt
    The top of the file is a histogram and a list of “queries ranked by total time consumed.” Each entry has a fingerprint (SELECT col FROM tbl WHERE id = ?), an example query, a count, and total execution time.
  2. Look at the top three by Response time percentage, not by individual query time. The query at position #1 might run 8000 times per minute at 80ms — well below long_query_time, but eating 11 minutes of wall clock per hour. The 12-second analytics export at position #15 runs once a day and is not the problem.
  3. Identify the source by user: each digest entry shows Users — acmecorp_wp means a single account’s database. On a cPanel host with 200 accounts, this immediately narrows the investigation to one customer’s app. Look at Hosts too: if all calls come from localhost, the source is on-host PHP; if they come from a remote address, it’s an external connection (which is rarer on cPanel).
  4. For each top query, run EXPLAIN against the production schema. The digest includes example queries — copy one, paste into a mysql session, prepend EXPLAIN. Look at the type column: ALL means full table scan; index means index scan; ref or eq_ref means an index lookup. Anything ALL against a large table is your candidate for an index.
  5. Cross-reference with pt-query-digest --review to track which queries you’ve already triaged. Run weekly, mark resolved queries, and the next run only highlights new offenders.
  6. For deeper investigation, pt-query-digest --explain h=localhost,u=root,p=... runs EXPLAIN automatically per fingerprint. Faster than copy-pasting one at a time.

When the top three offenders need real index work — a covering index, a redesign, or a query rewrite — Stack Harbor’s environment management team performs the schema review and ships the index migration off-peak with rollback ready.