Categories
Uncategorized

Optimize all the tables in your database using cronjobs

This php script repairs and optimizes all the tables in the selected database. Make sure you provide the right values in the connection string.

Script

optimize.php

 $tablename)
   {
       if(mysqli_query($link,"REPAIR TABLE `$tablename`")) 
           { 
           echo '\nRepair Success: '.$tablename;
            }
       else 
         die(mysqli_error($link));
       if(mysqli_query($link,"OPTIMIZE TABLE `$tablename`")) 
          { 
           echo '\nOptmize Success: '.$tablename;
          }
       else
          die(mysqli_error($link));
   }
}

mysqli_close($link);

Cronjob

crontab -e
0 * * * * php /var/www/html/optimize.php
Ctrl+O
Enter
Ctrl+X
sudo service cron restart