Keep your database fast

database, mysql, wordpress, revisions, transients, autoload, optimize · 0 found this helpful

Every WordPress page asks the database dozens of questions. A tidy database answers quickly. A cluttered one slows every uncached page and every trip to wp-admin.

Before you start

Back up the database. See Export a database and Download a backup copy. All the steps below delete or change data.

What slows a database

Cause Where it comes from
Post revisions Every save in the editor keeps a copy. A page edited 100 times has 100 revisions
Expired transients Temporary data plugins store and forget to remove
Spam and trashed comments Pile up if not cleared
Autoloaded options Settings WordPress loads on every page. Old plugins leave big ones behind
Leftover tables Deleted plugins sometimes leave tables
Missing indexes on large tables Mostly in shops and custom plugins

Step 1: Limit revisions

Add this to wp-config.php above the line that says "That's all, stop editing".

define('WP_POST_REVISIONS', 5);

Step 2: Clean up with a plugin

  1. Take a backup first. Open Account → Hosting → your site → WordPress and find your site. For a full backup, use Backups (see How backups work at Barbrick). Then use a plugin for the next step, since Barbrick's tools don't clean the database itself.
  2. If you use LiteSpeed Cache, go to LiteSpeed Cache → Database. It can clear revisions, auto-drafts, trashed posts, spam comments, and expired transients. Tick one type at a time. See LiteSpeed Cache explained.
  3. Empty trash and spam in WordPress: Comments → Spam → Empty Spam, and Posts → Trash → Empty Trash.

Step 3: Find big autoloaded options

Autoloaded data is read on every request. If it grows past about 800 KB, WordPress's Site Health screen warns you, and it can slow the whole site.

  1. Open Account → Hosting → your site → Databases → phpMyAdmin. See Use phpMyAdmin.
  2. Select your WordPress database. Click SQL.
  3. Run this. Replace wp_ if your tables use a different prefix.
SELECT ROUND(SUM(LENGTH(option_value)) / 1024) AS autoload_kb
FROM wp_options
WHERE autoload IN ('yes', 'on', 'auto-on', 'auto');
  1. To see the biggest offenders:
SELECT option_name, LENGTH(option_value) AS bytes
FROM wp_options
WHERE autoload IN ('yes', 'on', 'auto-on', 'auto')
ORDER BY bytes DESC
LIMIT 10;
  1. Look at the names. If a name belongs to a plugin you removed, it's safe to delete after a backup. If you're not sure, leave it.

Step 4: Remove tables from deleted plugins

In phpMyAdmin, look for tables with the prefix of plugins you no longer use. Delete only after a backup, and only if you're sure. WordPress core tables are wp_posts, wp_postmeta, wp_options, wp_users, wp_usermeta, wp_comments, wp_commentmeta, wp_terms, wp_term_taxonomy, wp_term_relationships, and wp_links.

Step 5: Use an object cache if available

An object cache keeps repeated database results in memory. It helps shops and membership sites most. Turn on Redis object caching in Account → Hosting → your site → WordPress → Manage → Speed and debug → Speed up this site, with the Object cache switch under Speed tools. See LiteSpeed Cache explained.

Step 6: Repair if it's damaged

If you see "database error" messages, or a table is marked as crashed, see Check and repair a database.

Habits

  • Keep the number of plugins small.
  • Remove plugins fully, not just deactivate.
  • Review the database every few months.
  • WooCommerce stores: archive old orders if your store has years of them.

If something goes wrong

Was this helpful?

Still stuck? Ask us and we'll walk you through it.