Optimizing your MySQL database from your website
In this post I’ll explain how simple is it to optimize your MySQL database directly from your website / website administration panel ( if you have one ).
Usually people I know optimize their MySQL database from “phpmyadmin” if they have it.
Why optimize ? Well, in case you just deleted a considerable amount of data from your website ( mysql database ) an optimization is pretty good for the well being of your MySQL server.
Here’s the function I use:
function optimize() {
$alltables = mysql_query("SHOW TABLES");
while ($table = mysql_fetch_assoc($alltables)) {
foreach ($table as $db => $tablename) {
if(mysql_query("OPTIMIZE TABLE `".$tablename."`")) {
echo 'Table '.$tablename.' optimized.'
';
} else {
echo 'Something went wrong. Table '.$tablename.' was not optimized !';
}
}
}
The code is pretty simple. We ask the MySQL server to tell us every table in the database and for each table we run the optimize command.
Let me know if you know or have a simpler way to optimize your database.