You are here

function cleaner_optimize_table in Cleaner 6

1 call to cleaner_optimize_table()
cleaner_cleaner_run in ./cleaner.module
Implementation of hook_cleaner_run().

File

./cleaner.module, line 272
Allows the admin to set a schedule for clearing caches and other stuff.

Code

function cleaner_optimize_table($opt = 0) {
  global $db_type;

  // Make sure the db type hasn't changed.
  if (drupal_strtolower(drupal_substr($db_type, 0, 5)) == 'mysql') {
    $result = db_query('SHOW TABLE STATUS');
    $list = array();
    while ($table = db_fetch_object($result)) {
      if ($table->Data_free) {
        $list[] = $table->Name;
      }
    }
    $query = 'OPTIMIZE ' . ($opt == 2 ? 'LOCAL ' : '') . 'TABLE ' . '{' . implode('}, {', $list) . '}';
    timer_start('cleaner_opt');
    db_query($query);
    $time = timer_read('cleaner_opt') / 1000;
    watchdog('Cleaner', 'Optimized tables: !opts. This required !time seconds.', array(
      '!opts' => implode(', ', $list),
      '!time' => number_format($time, 3),
    ), WATCHDOG_INFO);
  }
  else {
    watchdog('Cleaner', 'Database type (!type) not allowed to be optimized.', array(
      '!type' => $db_type,
    ), WATCHDOG_ERROR);
  }
  return;
}