Skip to content

Databases

EXPLAIN a query before you buy RAM

Use EXPLAIN to see if a slow query needs an index, not a larger server.

Updated Aug 29, 20263 min read16 reads
EXPLAIN a query before you buy RAM
Run EXPLAIN before you upgrade RAM for a slow query

EXPLAIN shows how MySQL plans to run a query before you spend money on more RAM. It tells you whether the server will use an index or scan the whole table. A full scan on a large table such as wp_postmeta stays a full scan even after you add memory. Fix the plan first, then decide whether the server size still needs to change.

How to read the EXPLAIN output

Open phpMyAdmin, paste your SELECT, UPDATE, or DELETE, and put EXPLAIN in front of it. The type column is the first thing to check. A value of ALL means a full table scan. Values like ref or eq_ref mean an index is in use. The key column names the index MySQL chose. The rows figure is only an estimate, but a guess of more than a million rows on a page that should touch a handful is a clear signal. Watch the Extra column for filesort or temporary table notes on busy queries. Newer MariaDB builds also support EXPLAIN ANALYZE, which runs the query and reports real timings. Use that on staging rather than on a live storefront.

Slow pages are often treated as a RAM problem when the real issue is a missing index. A smaller plan on a modest VPS usually beats a larger plan that still scans every row. Memory can make a bad scan finish a little sooner, but it does not turn the scan into an index lookup.

How to confirm the fix worked

Add the missing index or rewrite the query, then run EXPLAIN again. Confirm that type is no longer ALL and that key points at a sensible index. Load the page with Query Monitor and check that the same statement has left the slow list. If the query is quicker but still fires hundreds of times per request, you are looking at a PHP loop, which extra RAM will not solve. If the new error is too many connections after a memory change, that is a separate issue; see our guide on connection limits.

Work on a copy of production data when the table is large. Building an index on a multi-gigabyte table is a write-heavy job. Use staging or a short maintenance window instead of peak traffic.

When RAM still matters

After the plan looks clean and the query count is under control, memory can still help with buffer pool size and concurrent load. Start with EXPLAIN so you are not paying for hardware that only hides a missing index. Shared hosting here runs cPanel with no root access, so index work happens in phpMyAdmin or your application tools. On a VPS you have root and can tune MySQL further once the queries themselves are sound.

Share

Send this article

Need someone else to do this? Send them the link — the commands are in the article.

Tagged

Was this article helpful?

Be the first to rate this article.