Skip to content
WordPress Tips

How to Optimize Your WordPress Database for Speed (Without Losing Data)

Learn how to safely optimize your WordPress database for speed. Clean revisions, transients, spam, and more with WP-Optimize. Includes Redis object caching setup.

Thakur Aarti
5 min read
Computer monitor displaying database code representing WordPress database optimization, SQL cleanup and performance tuning

Is your WordPress site getting slower over time even though you haven’t changed anything? Your database is probably the culprit. Over months and years, WordPress accumulates thousands of rows of unnecessary data — post revisions, spam comments, transients, orphaned metadata — that make every database query slower. The good news: cleaning and optimizing your database is safe, fast, and free. This guide shows you exactly how to do it.

🗄️ Quick Summary: WordPress database bloat comes from post revisions, auto-drafts, trashed posts, spam comments, expired transients, and orphaned metadata. Regular cleanup can reduce database size by 30–70% and noticeably improve query times.

What Causes WordPress Database Bloat?

Every time you edit a post, WordPress saves a revision. Every time a plugin stores temporary data, it creates a transient. Spam comments accumulate. Plugins get deleted but leave their data tables behind. Over time, these add up to thousands of rows of data that WordPress has to sort through on every page load.

Here’s a breakdown of the most common sources of database bloat:

Bloat TypeWhere It Comes FromImpact
Post revisionsEvery save/update of a post or pageHigh — can be thousands of rows
Auto-draftsWordPress auto-save featureMedium
Trashed posts/pagesDeleted content not emptied from trashMedium
Spam & trashed commentsComment spam not deletedMedium–High on busy sites
Expired transientsPlugin temporary data that wasn’t cleaned upHigh — can be tens of thousands of rows
Orphaned metadataPost/user meta from deleted pluginsMedium
Unused plugin tablesPlugins that don’t clean up on deletionLow–Medium

How to Check Your Database Size

Before optimizing, benchmark your current database size so you can measure improvement. Log into your hosting control panel and open phpMyAdmin. Select your WordPress database from the left sidebar. The bottom of the page shows the total database size. Alternatively, install WP-Optimize and check its dashboard — it shows a detailed breakdown of bloat by type.

Method 1: Use WP-Optimize Plugin (Recommended for Beginners)

WP-Optimize is a free WordPress plugin that makes database optimization point-and-click simple. It’s safe, widely used (1M+ installs), and includes scheduling so you can set and forget it.

Step 1: Install WP-Optimize

Go to Plugins → Add New, search for “WP-Optimize,” install and activate it. Find it in your WordPress admin menu under WP-Optimize.

Step 2: Review What to Clean

WP-Optimize shows you a list of optimizations with the number of rows/items that would be removed and how much space they take up. Common items include:

  • Post revisions — Select “Keep last X revisions” to preserve recent history while removing old ones
  • Auto drafts — Safe to clean
  • Trash (posts, pages, comments) — Empty the trash
  • Spam comments — Delete all spam
  • Expired transients — Always safe to remove
  • Orphaned post meta — Generally safe, but check the count first
⚠️ Important: Always take a full database backup before running any optimization. While these operations are safe for most sites, edge cases exist. Use your host’s backup tool or UpdraftPlus to create a backup first.

Step 3: Run Optimizations and Schedule

Click Run All Selected Optimizations to clean the database. Then go to the Settings tab in WP-Optimize and set up a weekly scheduled clean to keep your database lean automatically.

Method 2: Limit Post Revisions in wp-config.php

Post revisions are the biggest source of WordPress database bloat on active sites. By default, WordPress saves unlimited revisions. You can cap this by adding a single line to your wp-config.php file:

// Keep only the last 5 revisions per post
define( 'WP_POST_REVISIONS', 5 );

To disable revisions entirely (not recommended — you lose the ability to roll back changes):

define( 'WP_POST_REVISIONS', false );

Add this line before the /* That's all, stop editing! */ line in wp-config.php. This prevents new revisions from accumulating, but you still need to clean up the existing revisions using WP-Optimize.

Method 3: Clean Up Transients via phpMyAdmin

Expired transients are stored in the wp_options table with the option name starting with _transient_ or _site_transient_. On sites with many plugins, this table can contain tens of thousands of rows. You can clean them directly in phpMyAdmin:

DELETE FROM wp_options 
WHERE option_name LIKE '%_transient_%';
⚠️ Caution: Running SQL queries directly in phpMyAdmin carries risk. Only do this if you’re comfortable with databases and have a recent backup. Using WP-Optimize to delete expired transients is safer and recommended for most users.

Method 4: Run MySQL OPTIMIZE TABLE

After deleting rows from your database, the physical disk space isn’t immediately reclaimed — the database has “gaps” where deleted rows used to be. Running OPTIMIZE TABLE reclaims this space and defragments the tables, which can further improve query speed.

WP-Optimize does this automatically as part of its optimization process. If you prefer to do it manually in phpMyAdmin, select all tables in your WordPress database, choose Optimize table from the “With selected” dropdown, and click Go.

Advanced: Add Object Caching with Redis

Even with a clean database, WordPress runs hundreds of database queries per page load. Object caching with Redis or Memcached stores the results of these queries in memory so they don’t need to re-execute on every request. This can reduce database load by 50–90% on high-traffic sites.

Redis is available on most managed WordPress hosts (Kinsta, WP Engine, Cloudways) and can be enabled with a plugin like Redis Object Cache by Till Krüss. Check if your host supports it before installing.

WordPress Database Optimization Checklist

TaskFrequencyTool
Clean post revisionsMonthlyWP-Optimize
Delete expired transientsWeeklyWP-Optimize (scheduled)
Empty trash (posts, comments)WeeklyWP-Optimize / WordPress admin
Delete spam commentsWeeklyAkismet / Comments admin
Run OPTIMIZE TABLEMonthlyWP-Optimize / phpMyAdmin
Limit post revisions in wp-config.phpOne timeManual
Enable object caching (Redis)One timeRedis Object Cache plugin

Frequently Asked Questions

Is it safe to delete all post revisions?

Yes — but it’s better practice to keep the last 3–5 revisions rather than deleting all of them. This gives you the ability to roll back to a recent version of a post if needed. Use WP-Optimize’s “Keep last X revisions” option rather than deleting all revisions.

How much of a speed improvement can I expect?

Results vary significantly depending on how bloated your database is. Sites that haven’t been cleaned in years can see database sizes drop by 50–70%, with noticeably faster admin dashboard load times and improved query performance. Combined with object caching, database optimization can be one of the most impactful speed improvements for dynamic WordPress sites.

Will optimizing the database affect my site’s content?

No — when done correctly, database optimization only removes unnecessary data like old revisions, expired transients, and spam comments. Your published posts, pages, product data, and user accounts are not affected.

Final Thoughts

Database optimization is one of those “set it and forget it” performance improvements. Install WP-Optimize, run an initial cleanup, limit future revisions in wp-config.php, set up a weekly scheduled cleanup, and consider adding Redis object caching if your host supports it. Your WordPress admin will feel snappier, and your front-end query times will improve — especially on content-heavy sites.

For a comprehensive WordPress speed audit that covers hosting, database, caching, images, and Core Web Vitals, check out our WordPress Speed & Performance Optimization service starting at $499.

Leave a Reply

Your email address will not be published. Required fields are marked *