To regularly optimize my WordPress database tables, I created a small plugin that utilizes the WordPress Cron feature. This comes in handy to perform database optimization for WordPress on a regular basis, without forgetting about it. Just activate and enjoy. And here is the plugin code …

Saotn MySQL Database Optimizer

Keeping your WordPress MySQL database optimized is a must for a fast performing website. Every post, draft, revision and comment adds data to your database, and removing revisions, drafts and comments creates empty space between data. This makes your database huger and fragmented.

Speaking of post revisions, did you know you can easily delete old WordPress post revisions?

Defragmenting the data puts all data back in order, removing empty spaces and results in a smaller database. The defragmented database performs faster because MySQL will be able locate information much faster.

You can use this as a must-use plugin by placing the file in the wp-content/mu-plugins directory, or as a regular plugin in wp-content/plugins (create a folder saotn-optimizer yourself), which you then must activate through the WordPress Dashboard.

You are free to use, change and customize the plugin code. I’ve based it off Pressjitsu’s transient cleaner plugin. All the plugin does is executing an OPTIMIZE TABLE statement for every table in your WordPress MySQL database on a daily or weekly basis (yours to choose).

 * Plugin Name: Saotn Database Table Optimizer
 * Plugin URI:
 * Donate Link:
 * Description: Optimizes WordPress MySQL database behind the scenes by executing an OPTIMIZE TABLE statement on all MySQL tables, 'daily' or 'hourly'.
 * Version: 1.0.2
 * Author: Jan Reilink
 * Author URI:
 * License: GPLv2

class Saotn_Db_Optimize_Tables_Cron {
  public static function load() {
    add_action( 'init', array( __CLASS__, 'schedule_events' ) );
    add_filter('plugin_row_meta', 'prefix_plugin_row_meta', 10, 2);

   * Schedule cron events, runs during init.
  public static function schedule_events() {
    if ( ! wp_next_scheduled( 'saotn_db_optimize_tables_cron' ) )
      // wp_schedule_event( time(), 'daily', 'saotn_db_optimize_tables_cron' );
      wp_schedule_event( time(), 'hourly', 'saotn_db_optimize_tables_cron' );
      add_action( 'saotn_db_optimize_tables_cron', array( __CLASS__, 'optimize_tables' ) );

  public static function optimize_tables() {
    global $wpdb;
    $bDebug = TRUE;
    $tables = $wpdb->get_col( "SHOW TABLES" );
    foreach ( $tables as $table ) {
      if ( $wpdb->query( "OPTIMIZE TABLE $table" ) !== FALSE ) {
        if ( $bDebug ) {
          error_log( "Saotn_Db_Optimizer ran successfully on $table" );


add_filter('plugin_row_meta', 'prefix2_plugin_row_meta', 10, 2);
function prefix2_plugin_row_meta($links, $file) {
    if ( !preg_match('/wp-database-tables-optimizer.php$/', $file ) ) {
      return $links;

    $links[] = sprintf(
      __( 'Donate' )
    return $links;

Use a plugin like Advanced Cron Manager to verify this database optimization WordPress Cron.

Saotn Optimizer on GitHub as Gist, and local download

You can find the Gist here on GitHub:, or as a text file on Saotn: wp-database-tables-optimizer.txt (rename .txt to .php).

Optimize WordPress database alternatives

Two alternatives for my very basic plugin are WP Sweep and WP Optimize.

Want to say thanks?

If I’ve helped you out and you want to thank me, why not buy me a coffee?

If I’ve helped you out and you want to thank me, why not buy me a coffee?

Thank you for your support. ♥

Jan Reilink

My name is Jan. I am not a hacker, coder, developer, programmer or guru. I am merely a system administrator, doing my daily thing at Vevida in the Netherlands. With over 15 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, WordPress, websites & optimization. Want to support me and donate? Use this link:


Ediong · 31 July 2017 at 11:18

Hi, I need your help in mysql optimization.

See my issue here:


Jean · 11 March 2016 at 19:54

Great tutorial. I recently tried using MySQLtunner which is a Perl script for MySQL performance analysis and tuning. It is working perfectly for me.


    Jan Reilink · 12 March 2016 at 12:06

    Thanks for your reply Jean! I’m familiar with MySQLtuner, which indeed is a great script to tune MySQL. Somehow I just like going through MySQL’s configuration settings to see what’s new and can be optimized, and therefore I mostly do it myself manually. MySQLtuner can then be used to verify the settings.

Muhammad Arslan · 5 March 2016 at 12:13

Looks cool. I would give it a try and give you my feedback.

MySQL-database optimaliseren - · 30 December 2018 at 11:23

[…] Voor het optimaliseren van MySQL tabellen via WordPress heb ik een kleine plugin ontwikkeld. Deze plugin haakt in op de WordPress Cron functionaliteit en voert zo één keer per dag of weekelijks een OPTIMIZE TABLE opdracht uit. Je vindt de code van deze plugin op, in mijn post Optimize WordPress MySQL tables through Cron, behind the scenes. […]

Leave a Reply

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

20 queries, 0.163 seconds running PHP version 7.3.2