How to optimize all tables in a MySQL database using php

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to optimize all tables in a MySQL database using php

Post by Saman » Fri Jul 15, 2011 6:17 pm

If you have a database driven site and you want to optimize MySQL tables then this is perfect. It goes through all the tables in a MySQL database and does table optimization on each one using the MySQL Optimize Table syntax.

Code: Select all

<?php 

dbConnect() 

$alltables = mysql_query("SHOW TABLES"); 

while ($table = mysql_fetch_assoc($alltables)) 
{ 

   foreach ($table as $db => $tablename) 
   { 
       mysql_query("OPTIMIZE TABLE '".$tablename."'") 
           or die(mysql_error()); 
   } 
    
} 

?>
Post Reply

Return to “PHP & MySQL”