Skip to content

Instantly share code, notes, and snippets.

@kroatoanweb
Last active July 22, 2016 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kroatoanweb/8f18670dd79199e15093c39a86eae185 to your computer and use it in GitHub Desktop.
Save kroatoanweb/8f18670dd79199e15093c39a86eae185 to your computer and use it in GitHub Desktop.
<?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