【问题标题】:How to optimize mysql database automatically in PHP?如何在 PHP 中自动优化 mysql 数据库?
【发布时间】:2011-03-04 08:29:03
【问题描述】:

我有一个数据库,需要每 24 小时自动优化一次

我该怎么办?

【问题讨论】:

    标签: php mysql database optimization


    【解决方案1】:

    您可以将优化过程放入每 24 小时运行一次的 cron 作业中。这个 cron 作业可以是 PHP(或您喜欢的任何其他语言)。

    【讨论】:

      【解决方案2】:

      我写了一门课

      
      
      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();
      

      【讨论】:

        猜你喜欢
        • 2011-01-23
        • 1970-01-01
        • 2012-07-20
        • 2010-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-06
        • 1970-01-01
        相关资源
        最近更新 更多