InnoDB buffer pool for WordPress: size it, then check that it works
The InnoDB buffer pool is MySQL’s memory cache for table and index pages. When the working set of a WordPress database (the posts, the meta, the options, the WooCommerce lookup tables that are actually read) fits into it, queries are served from RAM. When it does not, every miss is a disk read, and the same query is fast one minute and slow the next. The default of 128 MB is meant for a laptop, not a shop with a 3 GB database.
Problem: A WooCommerce database of 2.4 GB runs on a server with 8 GB RAM and the default buffer pool of 128 MB. Product archive queries vary between 20 ms and 900 ms, and iostat shows constant reads on the database disk.
Solution: Measure the data and index size, set innodb_buffer_pool_size to hold the working set while leaving memory for PHP-FPM and the OS, change it online if needed, and confirm with the hit ratio and the read counters that the pool works. Check the redo log size at the same time.
How big is the database?
SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024) AS total_mb FROM information_schema.TABLES WHERE table_schema = DATABASE() AND engine = 'InnoDB'; SELECT table_name, ROUND((data_length + index_length) / 1024 / 1024) AS mb FROM information_schema.TABLES WHERE table_schema = DATABASE() ORDER BY data_length + index_length DESC LIMIT 10;
The whole database rarely needs to be in memory: old orders, logs and revisions are seldom read. But if the server has the memory, sizing the pool for the full data plus growth is the simplest rule.
Set the size
On a dedicated database server, 60–75% of RAM is common. On a single server that also runs Nginx, PHP-FPM and Redis, subtract what they need first:
# 8 GB RAM total # PHP-FPM: 20 workers x 80 MB = 1.6 GB # Redis, Nginx, OS, page cache = 2 GB # left for MySQL = ~4 GB, buffer pool 3 GB # /etc/mysql/mysql.conf.d/zz-tuning.cnf [mysqld] innodb_buffer_pool_size = 3G innodb_buffer_pool_instances = 4 innodb_redo_log_capacity = 1G # MySQL 8.0.30+; older: innodb_log_file_size
A buffer pool that pushes the server into swap is worse than a small one: memory pages of MySQL on disk turn every cache hit into a disk read. How much the PHP workers use: tune the PHP-FPM pool.
Since MySQL 5.7.5 the size can be changed without a restart:
SET GLOBAL innodb_buffer_pool_size = 3 * 1024 * 1024 * 1024; SHOW STATUS LIKE 'Innodb_buffer_pool_resize_status';
Put the same value into the configuration file, or it is lost at the next restart. MariaDB also supports online resizing (10.2+).
Is it working?
SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_read%'; -- Innodb_buffer_pool_read_requests logical reads (from memory or disk) -- Innodb_buffer_pool_reads reads that had to go to disk
The miss rate is reads / read_requests. After the pool has warmed up (hours of normal traffic), it should be well below 1%. Take two readings ten minutes apart and compare the differences, not the totals since the server start.
SELECT ROUND(100 * (1 - (SELECT variable_value FROM performance_schema.global_status WHERE variable_name = 'Innodb_buffer_pool_pages_free')
/ (SELECT variable_value FROM performance_schema.global_status WHERE variable_name = 'Innodb_buffer_pool_pages_total'))) AS pct_used;
A pool that is 100% used with a low miss rate is fine. A pool that is 100% used with many disk reads is too small.
Warm restarts
innodb_buffer_pool_dump_at_shutdown = ON innodb_buffer_pool_load_at_startup = ON
Both are on by default since MySQL 5.7: MySQL saves the list of cached pages at shutdown and reloads them at startup, so the site is not slow for an hour after every restart.
NOTE: The redo log has to be big enough for the write load; on MySQL before 8.0.30, a too small innodb_log_file_size also caused errors on large rows: MySQL error 1118 and the redo log size. Whether the disk is really the bottleneck: htop, vmstat and iostat.