{"id":9004,"date":"2026-04-27T17:25:39","date_gmt":"2026-04-28T00:25:39","guid":{"rendered":"https:\/\/www.ultimatewb.com\/blog\/?p=9004"},"modified":"2026-04-27T17:33:18","modified_gmt":"2026-04-28T00:33:18","slug":"the-developers-guide-to-identifying-and-optimizing-slow-mysql-queries","status":"publish","type":"post","link":"https:\/\/www.ultimatewb.com\/blog\/9004\/the-developers-guide-to-identifying-and-optimizing-slow-mysql-queries\/","title":{"rendered":"The Developer\u2019s Guide to Identifying and Optimizing Slow MySQL Queries"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\">    <picture>\n                <source type=\"image\/webp\" srcset=\"https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries-150x82.webp 150w, https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries-500x273.webp 500w, https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries-800x437.webp 800w, https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries.webp 1200w\" sizes=\"(max-width: 600px) 100vw, (max-width: 1200px) 75vw, 1200px\">\n                <img src=\"https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries.jpg\"\n             srcset=\"https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries.jpg 1200w, https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries-500x273.jpg 500w, https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries-768x419.jpg 768w, https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries-150x82.jpg 150w, https:\/\/www.ultimatewb.com\/blog\/wp-content\/uploads\/web-developer-optimizing-mysql-queries-800x437.jpg 800w\"             sizes=\"(max-width: 600px) 100vw, (max-width: 1200px) 75vw, 1200px\"\n             width=\"1200\"\n             height=\"655\"\n             alt=\"web-developer-optimizing-mysql-queries\"\n             loading=\"lazy\"             decoding=\"async\"\n             class=\"wp-image-9008\" >\n    <\/picture>\n    <\/figure>\n\n\n\n<p>This tutorial will guide you through identifying, extracting, and analyzing your most problematic database queries using the command line on a WHM\/cPanel server.<\/p>\n\n\n\n<p>If you\u2019re using <strong><a href=\"https:\/\/www.ultimatewb.com\">UltimateWB<\/a><\/strong> for your backend, chances are you won\u2019t need any of this. We optimize UltimateWB to run fast whether your website is small or large. But in case you need it &#8211; for example, if you are adding complex custom code or building a project from scratch &#8211; this guide has you covered.<\/p>\n\n\n\n<p>Related: <a href=\"https:\/\/www.ultimatewb.com\/blog\/5316\/reduce-pagespeed-impact-of-google-adsense\/\">How to Lazy Load Google Adsense Ads for a Fast Website<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.ultimatewb.com\/blog\/6683\/why-avoiding-third-party-plugins-makes-your-website-faster-safer-and-easier-to-manage\/\">Why Avoiding Third-Party Plugins Makes Your Website Faster, Safer, and Easier to Manage<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 1: Prerequisites<\/strong><\/h2>\n\n\n\n<p>To follow this guide, you must have <strong>root access<\/strong> to your server via SSH.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Open your terminal<\/strong> and log in: <code>ssh root@your-server-ip<\/code>.<\/li>\n\n\n\n<li><strong>Confirm your log path:<\/strong> In this example, the path is <code>\/var\/log\/mysql-slow.log<\/code>.<\/li>\n\n\n\n<li><strong>Ensure logging is active:<\/strong> If that file is empty, MySQL isn&#8217;t recording slow queries yet. You can enable it temporarily without restarting MySQL by running:Bash<code>mysql -e \"SET GLOBAL slow_query_log = 'ON';\" mysql -e \"SET GLOBAL long_query_time = 2;\" <\/code><em>(Note: This sets the threshold to 2 seconds. Any query taking longer than this will be logged.)<\/em><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 2: Extracting the Top 10 (Quick Method)<\/strong><\/h2>\n\n\n\n<p>The <code>mysqldumpslow<\/code> tool is pre-installed on almost all WHM servers. To get the <strong>exact queries<\/strong> (including the actual IDs and data), we use the <strong><code>-a<\/code><\/strong> (across) flag.<\/p>\n\n\n\n<p><strong>Command for Top 10 by Average Speed:<\/strong><\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqldumpslow -a -s at -t 10 \/var\/log\/mysql-slow.log\n<\/code><\/pre>\n\n\n\n<p><strong>Command for Top 10 by \u201cTotal Impact\u201d:<\/strong> This identifies queries that might be slightly faster but run so often they slow down the entire server.<\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqldumpslow -a -s t -t 10 \/var\/log\/mysql-slow.log\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 3: Advanced Analysis (Professional Method)<\/strong><\/h2>\n\n\n\n<p>For a deep dive that provides specific optimization metrics, administrators often use the <strong>Percona Toolkit<\/strong>. This is the industry standard for managing high-traffic sites and massive databases.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Install the Toolkit:<\/strong>Bash<code>yum install percona-toolkit -y<\/code><\/li>\n\n\n\n<li><strong>Generate the Digest:<\/strong>Bash<code>pt-query-digest \/var\/log\/mysql-slow.log &gt; \/root\/slow_report.txt<\/code><\/li>\n\n\n\n<li><strong>Read the Report:<\/strong>Bash<code>less -S \/root\/slow_report.txt<\/code><\/li>\n<\/ol>\n\n\n\n<p><strong>What to look for in the report:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Query ID:<\/strong> A unique fingerprint for that specific type of query.<\/li>\n\n\n\n<li><strong>Exec time:<\/strong> Look for the <strong>\u201c95%\u201d<\/strong> column to see how slow the query is for the vast majority of users.<\/li>\n\n\n\n<li><strong>V\/M (Variance-to-Mean):<\/strong> If this is high, the query is \u201cunstable\u201d (sometimes fast, sometimes very slow).<\/li>\n\n\n\n<li><strong>The Sample:<\/strong> Scroll down to find the literal SQL statement you can copy-paste into your database manager to test.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Part 4: How to Optimize the Results<\/strong><\/h2>\n\n\n\n<p>Once you have the exact query, the next step is to ask the database <strong>why<\/strong> it is slow by running an <code>EXPLAIN<\/code> plan.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Copy the exact query<\/strong> (e.g., <code>SELECT * FROM users WHERE email = 'test@example.com';<\/code>).<\/li>\n\n\n\n<li><strong>Log into MySQL:<\/strong> Type <code>mysql<\/code>.<\/li>\n\n\n\n<li><strong>Run an EXPLAIN:<\/strong>SQL<code>EXPLAIN SELECT * FROM users WHERE email = 'test@example.com';<\/code><\/li>\n<\/ol>\n\n\n\n<p><strong>The Optimization Checklist:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>If <code>type<\/code> is <code>ALL<\/code>:<\/strong> This is a \u201cFull Table Scan.\u201d You are missing an <strong>Index<\/strong> on the column in your <code>WHERE<\/code> clause.<\/li>\n\n\n\n<li><strong>If <code>rows<\/code> is huge:<\/strong> The query is searching through thousands of rows just to find a few. Adding an index will reduce this number significantly.<\/li>\n\n\n\n<li><strong>If <code>Using temporary<\/code> or <code>Using filesort<\/code>:<\/strong> Your <code>GROUP BY<\/code> or <code>ORDER BY<\/code> statements are inefficient and should be optimized.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Summary Table of Commands<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Goal<\/th><th>Command<\/th><\/tr><\/thead><tbody><tr><td><strong>Quick Top 10 (Simple)<\/strong><\/td><td><code>mysqldumpslow -a -s at -t 10 \/var\/log\/mysql-slow.log<\/code><\/td><\/tr><tr><td><strong>Deep Analysis (Pro)<\/strong><\/td><td><code>pt-query-digest \/var\/log\/mysql-slow.log<\/code><\/td><\/tr><tr><td><strong>Real-time Monitoring<\/strong><\/td><td><code>tail -f \/var\/log\/mysql-slow.log<\/code><\/td><\/tr><tr><td><strong>Test Performance<\/strong><\/td><td><code>EXPLAIN [your_query_here];<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Related: <a href=\"https:\/\/www.ultimatewb.com\/blog\/5563\/why-should-you-care-about-your-website-performance-scores-on-pagespeed-insights-even-if-your-website-loads-fast\/\">Why Should You Care About Your Website Performance Scores on PageSpeed Insights, Even If Your Website Loads Fast?<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.ultimatewb.com\/blog\/6105\/does-faster-website-speed-increase-googlebot-crawl-frequency\/\">Does Faster Website Speed Increase Googlebot Crawl Frequency?<\/a><\/p>\n\n\n\n<p>Want a website builder that makes the web developing process a lot easier and faster? Learn more about\u00a0<a href=\"https:\/\/www.ultimatewb.com\/\">UltimateWB<\/a>! We also offer\u00a0<a href=\"https:\/\/www.ultimatewb.com\/web-design-packages\">web design packages<\/a>\u00a0if you would like your website designed and built for you.<\/p>\n\n\n\n<p><em>Got a techy\/website question? Whether it\u2019s about UltimateWB or another website builder, web hosting, or other aspects of websites, just send in your question in the&nbsp;<a href=\"https:\/\/www.ultimatewb.com\/ask-david\">\u201cAsk David!\u201d form<\/a>. We will email you when the answer is posted on the UltimateWB \u201cAsk David!\u201d section.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019re using UltimateWB for your backend, chances are you won\u2019t need any of this. We optimize &hellip; <a href=\"https:\/\/www.ultimatewb.com\/blog\/9004\/the-developers-guide-to-identifying-and-optimizing-slow-mysql-queries\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[374,100],"tags":[326,6713,468,6703,323,6706,6710,6704,6708,6711,6705,6709,5404,6714,3201,6712,6702,4029,6707],"class_list":["post-9004","post","type-post","status-publish","format-standard","hentry","category-troubleshooting","category-web-hosting-2","tag-cpanel","tag-database-optimization","tag-fast-website","tag-guide","tag-mysql","tag-mysql-database","tag-mysqldumpslow","tag-optimize-database","tag-percona-toolkit","tag-server-performance","tag-slow-mysql-queries","tag-slow-query-log","tag-software-development","tag-sql-optimization","tag-technical-seo","tag-terminal","tag-tutorial","tag-web-performance","tag-whm"],"_links":{"self":[{"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/posts\/9004"}],"collection":[{"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/comments?post=9004"}],"version-history":[{"count":5,"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/posts\/9004\/revisions"}],"predecessor-version":[{"id":9011,"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/posts\/9004\/revisions\/9011"}],"wp:attachment":[{"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/media?parent=9004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/categories?post=9004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ultimatewb.com\/blog\/wp-json\/wp\/v2\/tags?post=9004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}