When a site is slow and the server isn't busy drawing pages, the database is usually the reason. Most of the gain comes from a handful of changes, and the order matters: find the slow queries, give them an index, give MySQL enough memory and stop storing what you don't need.
Everything here applies to MySQL and, with small differences, to MariaDB. Make a backup before you start, and change one thing at a time.
Before you start: measure
Without a number you can't tell whether a change helped. Note three things:
- How long a typical page takes to generate, without cache.
- Which version you run:
mysql --version, or this query:
SELECT VERSION();- How much data you have, table by table:
SELECT table_name,
ROUND((data_length + index_length) / 1024 / 1024) AS mb,
table_rows
FROM information_schema.tables
WHERE table_schema = DATABASE()
ORDER BY mb DESC
LIMIT 10;About versions: MySQL 8.0 reached its end of life in April 2026. If you are still on it, plan the move to a supported long-term version, 8.4 or 9.7. New versions bring better query planning, so upgrading is a performance step too. Test it on a copy first.
Step 1: find the slow queries
MySQL can write down every query that takes too long. Turn it on without restarting:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
SHOW VARIABLES LIKE 'slow_query_log_file';Leave it for a day with normal traffic, then summarize the file with the tool that comes with MySQL:
mysqldumpslow -s t -t 10 /path/to/slow.logYou get the ten queries that consumed the most time. Look at two numbers: how long each takes and how many times it runs. A query of 50 milliseconds that runs 400 times a minute hurts more than one of three seconds that runs once a day.
In WordPress, Query Monitor shows which plugin or theme sends each query. Install it on a test copy and remove it when you finish.
Step 2: give those queries an index
Without an index, MySQL reads the whole table to find a few rows. Ask how it plans to run a query by writing EXPLAIN in front of it:
EXPLAIN SELECT * FROM orders
WHERE customer_id = 42 AND status = 'paid'
ORDER BY created_at DESC;| What you see | What it means |
|---|---|
type: ALL | It reads the whole table |
key: NULL | It uses no index |
rows very high | It examines far more than it returns |
Using filesort | It sorts the result by hand |
Using temporary | It creates a temporary table |
For the query above, a single index on the three columns solves it:
ALTER TABLE orders
ADD INDEX customer_status_date (customer_id, status, created_at);Rules that help:
- Put first the columns compared with
=, then the one used to sort or to compare ranges. - An index on
(a, b)also serves searches bya, but not bybalone. - A function around a column, such as
YEAR(created_at) = 2026, disables the index. Write a range of dates instead. - A search like
'%word'can't use a normal index. For text, use a full-text index. - Every index makes writing a little slower. Delete the ones nobody uses.
In MySQL 8 and later, EXPLAIN ANALYZE runs the query and reports the real times. Use it on queries that only read.
Step 3: give MySQL enough memory
InnoDB, the default storage engine, keeps data and indexes in an area of memory called the buffer pool. If the data you use most fits there, disk is hardly touched. Its default is only 128 MB.
SELECT @@innodb_buffer_pool_size / 1024 / 1024 AS mb;
SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_read%';Compare Innodb_buffer_pool_reads (had to go to disk) with Innodb_buffer_pool_read_requests (all requests). If the first grows quickly, the pool is too small.
| Server | Reasonable starting point |
|---|---|
| Only the database | 60 to 75% of the memory |
| Web and database together | 25 to 40% of the memory |
| Small server (1 to 2 GB) | 128 to 512 MB |
Set it in the configuration file, under [mysqld]:
[mysqld]
innodb_buffer_pool_size = 2G
max_connections = 100
tmp_table_size = 64M
max_heap_table_size = 64MBe careful with max_connections. Each connection uses memory of its own, so a very high number can exhaust the server instead of helping. And ignore old advice about the query cache: MySQL removed it in version 8.0.
If you don't want to calculate, the free MySQLTuner script reads your server and makes suggestions. Run it after at least a day of use, and treat its advice as a list to review, not as orders.
Step 4: store less
Large tables slow down everything, including backups. On a WordPress site, check these first:
- Revisions. Keep five per post by adding
define('WP_POST_REVISIONS', 5);towp-config.php. - Autoloaded options. WordPress loads them on every page. Since version 6.6, Site Health warns you when they add up to more than 800 KB. This query shows the biggest:
SELECT option_name, LENGTH(option_value) AS bytes
FROM wp_options
WHERE autoload IN ('yes', 'on', 'auto', 'auto-on')
ORDER BY bytes DESC
LIMIT 15;- Expired transients, spam and trash.
- Logs and statistics that plugins keep forever. Set a limit in each plugin.
- Finished tasks in the
actionscheduler_tables, if you run WooCommerce.
Delete in small blocks and with a recent backup. After removing a lot, OPTIMIZE TABLE returns the space to the disk; run it at a quiet hour, since it rebuilds the table.
What usually isn't worth it
- Changing twenty settings copied from a forum.
- Adding a server before reading the slow query log.
- Optimizing every table every night.
- Caching a page whose query takes five seconds: the first visitor still waits, and you still have the problem.
Cache is the next layer, once queries are reasonable. An object cache such as Redis keeps results in memory and saves a large share of the reads in WordPress.
When the server is the limit
If queries are fine and there isn't any memory left, you need more resources. On a server of your own, such as a Managed VM, you can apply every setting in this guide. If you would rather not look after a database, our WooCommerce plans include Redis and memory sized for stores. For the rest of the store, see the WooCommerce speed checklist.
