This tutorial will guide you through identifying, extracting, and analyzing your most problematic database queries using the command line on a WHM/cPanel server.
If you’re using UltimateWB for your backend, chances are you won’t need any of this. We optimize UltimateWB to run fast whether your website is small or large. But in case you need it – for example, if you are adding complex custom code or building a project from scratch – this guide has you covered.
Related: How to Lazy Load Google Adsense Ads for a Fast Website
Why Avoiding Third-Party Plugins Makes Your Website Faster, Safer, and Easier to Manage
Part 1: Prerequisites
To follow this guide, you must have root access to your server via SSH.
- Open your terminal and log in:
ssh root@your-server-ip. - Confirm your log path: In this example, the path is
/var/log/mysql-slow.log. - Ensure logging is active: If that file is empty, MySQL isn’t recording slow queries yet. You can enable it temporarily without restarting MySQL by running:Bash
mysql -e "SET GLOBAL slow_query_log = 'ON';" mysql -e "SET GLOBAL long_query_time = 2;"(Note: This sets the threshold to 2 seconds. Any query taking longer than this will be logged.)
Part 2: Extracting the Top 10 (Quick Method)
The mysqldumpslow tool is pre-installed on almost all WHM servers. To get the exact queries (including the actual IDs and data), we use the -a (across) flag.
Command for Top 10 by Average Speed:
Bash
mysqldumpslow -a -s at -t 10 /var/log/mysql-slow.log
Command for Top 10 by “Total Impact”: This identifies queries that might be slightly faster but run so often they slow down the entire server.
Bash
mysqldumpslow -a -s t -t 10 /var/log/mysql-slow.log
Part 3: Advanced Analysis (Professional Method)
For a deep dive that provides specific optimization metrics, administrators often use the Percona Toolkit. This is the industry standard for managing high-traffic sites and massive databases.
- Install the Toolkit:Bash
yum install percona-toolkit -y - Generate the Digest:Bash
pt-query-digest /var/log/mysql-slow.log > /root/slow_report.txt - Read the Report:Bash
less -S /root/slow_report.txt
What to look for in the report:
- Query ID: A unique fingerprint for that specific type of query.
- Exec time: Look for the “95%” column to see how slow the query is for the vast majority of users.
- V/M (Variance-to-Mean): If this is high, the query is “unstable” (sometimes fast, sometimes very slow).
- The Sample: Scroll down to find the literal SQL statement you can copy-paste into your database manager to test.
Part 4: How to Optimize the Results
Once you have the exact query, the next step is to ask the database why it is slow by running an EXPLAIN plan.
- Copy the exact query (e.g.,
SELECT * FROM users WHERE email = 'test@example.com';). - Log into MySQL: Type
mysql. - Run an EXPLAIN:SQL
EXPLAIN SELECT * FROM users WHERE email = 'test@example.com';
The Optimization Checklist:
- If
typeisALL: This is a “Full Table Scan.” You are missing an Index on the column in yourWHEREclause. - If
rowsis huge: The query is searching through thousands of rows just to find a few. Adding an index will reduce this number significantly. - If
Using temporaryorUsing filesort: YourGROUP BYorORDER BYstatements are inefficient and should be optimized.
Summary Table of Commands
| Goal | Command |
|---|---|
| Quick Top 10 (Simple) | mysqldumpslow -a -s at -t 10 /var/log/mysql-slow.log |
| Deep Analysis (Pro) | pt-query-digest /var/log/mysql-slow.log |
| Real-time Monitoring | tail -f /var/log/mysql-slow.log |
| Test Performance | EXPLAIN [your_query_here]; |
Does Faster Website Speed Increase Googlebot Crawl Frequency?
Want a website builder that makes the web developing process a lot easier and faster? Learn more about UltimateWB! We also offer web design packages if you would like your website designed and built for you.
Got a techy/website question? Whether it’s about UltimateWB or another website builder, web hosting, or other aspects of websites, just send in your question in the “Ask David!” form. We will email you when the answer is posted on the UltimateWB “Ask David!” section.
