function db_maintenance_optimize_tables in DB Maintenance 7
Same name and namespace in other branches
- 8.2 db_maintenance.module \db_maintenance_optimize_tables()
- 5.2 db_maintenance.module \db_maintenance_optimize_tables()
- 5 db_maintenance.module \db_maintenance_optimize_tables()
- 6.2 db_maintenance.module \db_maintenance_optimize_tables()
- 6 db_maintenance.module \db_maintenance_optimize_tables()
Perform the maintenance.
2 calls to db_maintenance_optimize_tables()
- db_maintenance_cron in ./
db_maintenance.module - Implements hook_cron().
- db_maintenance_optimize_tables_page in ./
db_maintenance.module - Callback page for manually optimizing tables.
File
- ./
db_maintenance.module, line 148 - Optimizes database tables during cron runs.
Code
function db_maintenance_optimize_tables() {
global $databases;
foreach ($databases as $db => $connection) {
$db_name = $connection['default']['database'];
$all_tables = variable_get('db_maintenance_all_tables', 0);
if ($all_tables) {
$config_tables = _db_maintenance_list_tables($db);
}
else {
$config_tables = variable_get('db_maintenance_table_list_' . $db_name, NULL);
}
// Only proceed if tables are selected for this database.
if (is_array($config_tables) && sizeof($config_tables) > 0) {
foreach ($config_tables as $key => $table_name) {
// Set the database to query.
$previous = db_set_active($db);
if (db_table_exists($table_name)) {
// PDO doesn't replace table names with ? or :
// db_table_exists and db_escape_table is redundant, but just doing both
if (db_driver() == 'mysql') {
try {
db_query("OPTIMIZE TABLE {$table_name}")
->execute();
} catch (Exception $e) {
watchdog_exception('type', $e);
}
}
elseif (db_driver() == 'pgsql') {
try {
db_query("VACUUM ANALYZE {$table_name}")
->execute();
} catch (Exception $e) {
watchdog_exception('type', $e);
}
}
}
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);
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', REQUEST_TIME);
}