You are here

function db_maintenance_optimize_tables in DB Maintenance 6

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. 5 db_maintenance.module \db_maintenance_optimize_tables()
  4. 6.2 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 122
Optimizes database tables during cron runs.

Code

function db_maintenance_optimize_tables() {
  global $db_url;
  global $db_prefix;
  if (is_array($db_prefix)) {
    $db_prefix = $db_prefix['default'];
  }
  $is_prefixed = strlen($db_prefix) > 0;

  // 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;
      while (list(, $table_name) = each($config_tables)) {

        // Set the database to query.
        $previous = db_set_active($db);
        $table_check_name = $is_prefixed ? str_replace($db_prefix, '', $table_name) : $table_name;
        if (db_table_exists($table_check_name)) {
          if (_db_maintenance_determine_software() == 'mysql') {
            db_query('OPTIMIZE TABLE %s', $table_name);
          }
          elseif (_db_maintenance_determine_software() == 'pgsql') {
            db_query('VACUUM ANALYZE %s', $table_name);
          }
        }
        else {
          watchdog('db_maintenance', '@table table in @db database was configured to be optimized but does not exist.', array(
            '@db' => $db_name,
            '@table' => $table_name,
          ), WATCHDOG_NOTICE);
        }

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