.COM DomainHuge Price DropBuy at 1150 BDT Only
00
d
00
h
00
m
00
s
FlexoHost Logo
FlexoHost Logo
Back to guides
Wordpress3 min readMar 31, 2026

How to Optimize Your WordPress Database

Why Database Optimization Matters Your WordPress database grows over time with unnecessary data that slows down every page load. A bloated database can add 1-3 seconds to your load time. Common Database Bloat Sources Sou…

FlexoHost Team

Technical guides & hosting tips

On this page

Why Database Optimization Matters

Your WordPress database grows over time with unnecessary data that slows down every page load. A bloated database can add 1-3 seconds to your load time.

Common Database Bloat Sources

Source Growth Rate Impact on Speed
Post revisions High Medium
Auto-drafts Medium Low
Spam comments High Medium
Transient options Very High High
Orphaned post meta Medium High
Expired sessions High Medium
Unused tables Low Medium

Real-World Impact

Database Size Query Time Page Load Impact
<50MB <0.1s Minimal
50-200MB 0.1-0.5s Noticeable
200-500MB 0.5-1.5s Significant
>500MB >1.5s Critical

Understanding WordPress Tables

Core Tables

Table Purpose Bloat Risk
wp_posts Posts, pages, revisions High
wp_postmeta Post metadata Very High
wp_options Settings, transients High
wp_comments All comments Medium
wp_commentmeta Comment metadata Medium
wp_terms Categories, tags Low
wp_usermeta User metadata Low

WooCommerce Tables

Table Purpose Bloat Risk
wp_woocommerce_sessions Cart sessions Very High
wp_wc_orders Order data Medium
wp_wc_order_stats Analytics High

Method 1: WP-Optimize Plugin

The easiest way to clean your database:

Installation

wp plugin install wp-optimize --activate

Recommended Cleanup Settings

Option Action Frequency
Post revisions Delete all Weekly
Auto-drafts Delete all Weekly
Trashed posts Delete all Weekly
Spam comments Delete all Daily
Transients Delete expired Daily
Pingbacks/Trackbacks Delete all Monthly

Scheduling Automatic Cleanups

  1. Go to WP-Optimize → Settings
  2. Enable scheduled cleanups
  3. Set frequency (weekly recommended)
  4. Select cleanup options
  5. Save changes

Method 2: Manual SQL Queries

For advanced users who prefer direct database access:

Delete Post Revisions

DELETE FROM wp_posts WHERE post_type = 'revision';

-- Also clean orphaned meta
DELETE FROM wp_postmeta 
WHERE post_id NOT IN (SELECT ID FROM wp_posts);

Delete Expired Transients

DELETE FROM wp_options 
WHERE option_name LIKE '%_transient_%' 
AND option_name NOT LIKE '%_transient_timeout_%';

DELETE FROM wp_options 
WHERE option_name LIKE '%_transient_timeout_%' 
AND option_value < UNIX_TIMESTAMP();

Clean WooCommerce Sessions

DELETE FROM wp_woocommerce_sessions 
WHERE session_expiry < UNIX_TIMESTAMP();

Delete Spam and Trash Comments

DELETE FROM wp_comments WHERE comment_approved = 'spam';
DELETE FROM wp_comments WHERE comment_approved = 'trash';

-- Clean orphaned comment meta
DELETE FROM wp_commentmeta 
WHERE comment_id NOT IN (SELECT comment_ID FROM wp_comments);

Optimize All Tables

-- Run in phpMyAdmin
OPTIMIZE TABLE wp_posts, wp_postmeta, wp_options, 
wp_comments, wp_commentmeta, wp_terms, wp_termmeta;

Method 3: WP-CLI Commands

For developers and advanced users:

Database Optimization

# Optimize all tables
wp db optimize

# Repair tables
wp db repair

# Check database size
wp db size --tables

Cleanup Commands

# Delete all transients
wp transient delete --all

# Delete expired transients only
wp transient delete --expired

# Delete all post revisions
wp post delete $(wp post list --post_type='revision' --format=ids)

# Delete spam comments
wp comment delete $(wp comment list --status=spam --format=ids)

Bulk Operations

# Delete revisions older than 30 days
wp post delete $(wp post list --post_type='revision' --date_query='[{"before":"30 days ago"}]' --format=ids)

# Export before cleanup (safety)
wp db export backup-before-cleanup.sql

Method 4: phpMyAdmin

Step-by-Step Optimization

  1. Log into cPanel
  2. Open phpMyAdmin
  3. Select your WordPress database
  4. Click “Check All” to select all tables
  5. From dropdown, select “Optimize table”
  6. Wait for completion

Identifying Large Tables

SELECT 
  table_name AS 'Table',
  ROUND(data_length / 1024 / 1024, 2) AS 'Data (MB)',
  ROUND(index_length / 1024 / 1024, 2) AS 'Index (MB)',
  ROUND((data_length + index_length) / 1024 / 1024, 2) AS 'Total (MB)'
FROM information_schema.TABLES
WHERE table_schema = 'your_database_name'
ORDER BY (data_length + index_length) DESC;

Prevention: wp-config.php Settings

Add these settings to prevent future bloat:

// Limit post revisions (default: unlimited)
define('WP_POST_REVISIONS', 3);

// Empty trash faster (default: 30 days)
define('EMPTY_TRASH_DAYS', 7);

// Increase autosave interval (default: 60 seconds)
define('AUTOSAVE_INTERVAL', 300);

// Disable post revisions entirely (not recommended)
// define('WP_POST_REVISIONS', false);

Optimization Schedule

Task Frequency Method Impact
Delete spam comments Daily Plugin/Cron Medium
Clear expired transients Daily Plugin/Cron High
Delete trashed items Weekly Plugin Medium
Remove post revisions Weekly Plugin/SQL High
Optimize tables Monthly phpMyAdmin High
Full database audit Quarterly Manual High

Before and After Comparison

Metric Before Optimization After Optimization
Database Size 450MB 85MB
Tables 120 95
Query Time 1.2s 0.15s
Page Load 4.5s 1.8s
TTFB 800ms 200ms

Troubleshooting

Issue Cause Solution
Optimization fails Table corruption Run REPAIR TABLE first
Slow queries persist Missing indexes Add proper indexes
Database grows back quickly Plugin issue Audit plugin database usage
Lock timeout errors Large tables Optimize during low traffic

Best Practices

Practice Benefit
Backup before optimization Safety net
Optimize during low traffic Prevent locks
Limit revisions in wp-config Prevent bloat
Use object caching (Redis) Reduce DB queries
Regular cleanup schedule Maintain performance
Monitor database size Catch issues early

Conclusion

Regular database optimization is essential for WordPress performance. A clean, optimized database can reduce page load times by 50% or more. Set up automated weekly cleanups and monitor your database size to maintain peak performance.

Pro Tip: Enable Redis object caching to reduce database queries by up to 80%. Most managed WordPress hosts offer Redis as an add-on or included feature.

Need Help? We Are Here To Help You

Please feel free to consult with one of our experts, who will thoroughly evaluate your specific requirements and provide a tailored proposal that perfectly aligns with your company's unique needs and realities.