You are here

function db_maintenance_optimize_tables in DB Maintenance 5

Same name and namespace in other branches
  1. 8.2 db_maintenance.module \db_maintenance_optimize_tables()
  2. 5.2 db_maintenance.module \db_maintenance_optimize_tables()
  3. 6.2 db_maintenance.module \db_maintenance_optimize_tables()
  4. 6 db_maintenance.module \db_maintenance_optimize_tables()
  5. 7 db_maintenance.module \db_maintenance_optimize_tables()

Perform the maintenance.

2 calls to db_maintenance_optimize_tables()
db_maintenance_cron in ./db_maintenance.module
Implementation of hook_cron().
db_maintenance_optimize_tables_page in ./db_maintenance.module
Callback page for manually optimizing tables.

File

./db_maintenance.module, line 121
Optimizes database tables during cron runs.

Code

function db_maintenance_optimize_tables() {
  global $db_url;

  // Set the databases array if not already set in $db_url.
  if (is_array($db_url)) {
    $databases = $db_url;
  }
  else {
    $databases['default'] = $db_url;
  }

  // Loop through each database optimizing any selected tables.
  foreach ($databases as $db => $connection) {
    $config_tables = variable_get('db_maintenance_table_list_' . $db, NULL);

    // Only proceed if tables are selected for this database.
    if (is_array($config_tables) && sizeof($config_tables) > 0) {
      $db_name = $db == 'default' ? 'Drupal' : $db;

      // Only proceed if it's a MySQL database.
      if (strpos($connection, 'mysql://') !== 0 && strpos($connection, 'mysqli://') !== 0) {
        watchdog('db_maintenance', t('Tables are configured to be optimized, but the !db database is not using MySQL', array(
          '!db' => $db_name,
        )));
      }
      else {
        $tables = implode(', ', $config_tables);

        // Set the database to query.
        $previous = db_set_active($db);
        db_query('OPTIMIZE TABLE %s', $tables);

        // Return to the previously set database.
        db_set_active($previous);
        if (variable_get('db_maintenance_log', 0)) {
          watchdog('db_maintenance', t('Optimized tables in !db database: ', array(
            '!db' => $db_name,
          )) . $tables);
        }
      }
    }
  }
  variable_set('db_maintenance_cron_last', time());
}