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
- Aggregate first, read second:
The top of the file is a histogram and a list of “queries ranked by total time consumed.” Each entry has a fingerprint (pt-query-digest /var/log/mysql/slow.log > /tmp/slow-digest.txt less /tmp/slow-digest.txtSELECT col FROM tbl WHERE id = ?), an example query, a count, and total execution time. - Look at the top three by
Response timepercentage, not by individual query time. The query at position #1 might run 8000 times per minute at 80ms — well belowlong_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. - Identify the source by user: each digest entry shows
Users—acmecorp_wpmeans a single account’s database. On a cPanel host with 200 accounts, this immediately narrows the investigation to one customer’s app. Look atHoststoo: if all calls come fromlocalhost, the source is on-host PHP; if they come from a remote address, it’s an external connection (which is rarer on cPanel). - For each top query, run
EXPLAINagainst the production schema. The digest includes example queries — copy one, paste into amysqlsession, prependEXPLAIN. Look at thetypecolumn:ALLmeans full table scan;indexmeans index scan;reforeq_refmeans an index lookup. AnythingALLagainst a large table is your candidate for an index. - Cross-reference with
pt-query-digest --reviewto track which queries you’ve already triaged. Run weekly, mark resolved queries, and the next run only highlights new offenders. - 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.