2010-06-16 49 views

回答

5

我寫了一個班



class DbPerformance 
{ 
    var $TablesHaveOverHead = array(); 
    function DbPerformance() 
    { 
     if (date("H") == '00') 
     { 
      $this->GetOverheadTables(); 
      $this->OptimizeDatabase(); 
     } 
    } 
    function GetOverheadTables() 
    { 
     $result = mysql_query("SHOW TABLE STATUS "); 
     while ($row = mysql_fetch_assoc($result)) 
     { 
      if ($row["Data_free"] > 0) 
      { 
       $this->TablesHaveOverHead[] = $row['Name']; 
      } 
     } 
    } 
    function OptimizeDatabase() 
    { 
     if (!empty($this->TablesHaveOverHead)) 
     { 
      $tables = implode(",", $this->TablesHaveOverHead); 
      mysql_query("OPTIMIZE TABLE $tables;"); 
     } 
    } 
} 
$optimise = new DbPerformance(); 
1

您可以將優化過程放入每24小時運行一次的cron作業。這個cron作業可能是PHP(或其他你喜歡的語言)。