Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Configuración de la aplicación
* Idealmente en un fichero aparte
*/
$dsn = 'mysql:dbname=nuestrabasededatos;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
//Obtenemos la lista de tablas a optimizar
$sth = $dbh->prepare("SHOW TABLES");
$sth->execute();
$tables = $sth->fetchAll(PDO::FETCH_COLUMN);
//Recorremos la lista optimizando cada una de las tablas
if (count($tables) > 0 && is_array($tables)) {
foreach ($tables as $table) {
$sth = $dbh->prepare("OPTIMIZE TABLE `{$table}`");
$sth->execute();
}
}
} catch (PDOException $e) {
die('Fallo de conexión: ' . $e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.