2011-12-16 70 views
1

我有一個問題,關於一個函數的調用,需要相當長的一段時間,我希望有些人可以看看我是否做了一些犯罪。緩慢的PHP過程

當我在sendletter.php下面的函數中運行函數GetEmailCue時,大約需要一分鐘左右才能完成,我找不到原因。

emailcue db表只包含2封電子郵件。

  /****************************************** 
     * Sends a letter from the email cue 
     * 
     * @param int  | The letter id 
     *******************************************/ 
     function SendTheLetter($letter_id) { 

      /*** Find the email cue based on the newsletter uid ***/ 
      $cue = new NewsletterHandler; 
      $cue->GetLetterContent($letter_id); 
      $cue->GetEmailCue($letter_id); 

      $headers = 'MIME-Version: 1.0' . "\r\n"; 
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

      foreach($cue->email_row as $key => $value) { 
       mail($value, "Newsletter - ". date("d-m-Y"), $this->htmlTemplate, $headers); 
       $sql_update = "UPDATE newsletter_emailcue SET time_recieved = NOW() WHERE email = '". $value ."'"; 
       SQLHandling::SQLquery($sql_update);     
      } 
     } 

此功能與其他兩個功能

拉數據

GetLetterContent:

  /****************************************** 
     * Find all fields that belongs to a  
     * newsletter. 
     * 
     * @param int  | The letter id 
     *******************************************/ 
     function GetLetterContent($letter_id) { 

      $sql = "SELECT A.template, A.status, B.field_name, B.field_content FROM newsletter_items A, newsletter_fields B WHERE B.field_letter_uid = '". $letter_id ."'";   

      $result = SQLHandling::SQLquery($sql); 

       while($row = mysql_fetch_array($result)) { 

        $this->content_markers["". $row["field_name"] .""] = $row["field_content"]; 
        $template = $row["template"]; 
       } 

       /*** Compile the letter with all the markers ***/ 
       $this->CompileLetter($letter_id, $template, $this->content_markers); 
       return;     
     } 

GetEmailCue:

  /****************************************** 
     * Get an entire email cue based on a 
     * newsletter id. 
     * This functions is called from the send 
     * function. 
     * 
     * @param int  | The letter id 
     *******************************************/ 
     function GetEmailCue($letter_id) { 
      $sql = "SELECT * FROM newsletter_emailcue WHERE mail_to_recieve = '". $letter_id ."' AND time_recieved = '0000-00-00 00:00:00' LIMIT 1"; 
      $result = SQLHandling::SQLquery($sql); 

      if(mysql_num_rows($result) < 1) { 
       $sql_update = "UPDATE newsletter_items SET status = 2 WHERE letter_id = '". $letter_id ."'"; 
       SQLHandling::SQLquery($sql_update); 
      } else {    
       while($row = mysql_fetch_array($result)) { 
        $this->email_row[] = $row["email"]; 
       } 
      } 

      return $this->email_row; 
     } 

似乎GetEmailCue可能是這個問題,因爲如果我在SendTheLetter中註釋掉它,那麼函數會馬上執行,如果我直接從文件運行GetEmailCue,這個函數也會立即執行,但是當我在SendTheLetter中運行它時,它永遠都會用到。

SendTheLetter功能再次從FindLetterToSend

  /****************************************** 
     * Finds all letters that have send status 1 
     * which is pending to send. 
     *******************************************/ 
     function FindLetterToSend() { 
      $sql = "SELECT * FROM newsletter_items WHERE status = '1'";   
      $result = SQLHandling::SQLquery($sql); 

      if(mysql_num_rows($result) == 0) { 
       Main::txtOutput("Der er ingen nyhedsbreve i udsendelseskøen!", "TXT_ERR");    

       /*** Let's clear the emailcue table - no need for the cue any more ***/ 
       SQLHandling::SQLquery("TRUNCATE TABLE newsletter_emailcue");    
      } else { 
       while($row = mysql_fetch_array($result)) { 
        $this->SendTheLetter($row["letter_id"]); 
       } 
      } 
      return; 
     } 

叫我運行它在我的Ubuntu的本地主機

回答

1

發送電子郵件需要一些時間,但你說,只有兩個郵件已發送。這並不多。

此代碼可以在任何電子郵件行上進行更新?

foreach($cue->email_row as $key => $value) { 
    mail($value, "Newsletter - ". date("d-m-Y"), $this->htmlTemplate, $headers); 
    $sql_update = "UPDATE newsletter_emailcue SET time_recieved = NOW() WHERE email = '". $value ."'"; 
    SQLHandling::SQLquery($sql_update);     
} 

您是否嘗試使用microtime函數來測量單個代碼部分的運行時間?

+0

當發送電子郵件時,該電子郵件將獲得時間戳,以避免他/她不止一次接收新聞通訊。該foreach更新電子郵件只是發送... 我可以嘗試測量運行時間。 – 2011-12-16 10:34:55