php - 缓慢的PHP进程

标签 php mysql performance function process

我有一个关于调用需要相当长一段时间的函数的问题,我希望有人能看到我是否做了什么犯罪行为。

当我从 sendletter.php 在下面的函数中运行函数 GetEmailCue 时,它​​需要大约一分钟左右的时间才能完成,我不明白为什么。

emailcue 数据库表仅包含 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);                
            }
        }   

这个函数从另外两个函数中提取数据

获取信件内容:

         /******************************************
         * 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;                
        }

获取邮件提示:

         /******************************************
         * 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 本地主机上运行它

最佳答案

发送电子邮件需要一些时间,但你说,只发送了两封邮件。这并不多。

此代码会更新任何电子邮件行吗?

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 函数测量单个代码部分的运行时间?

关于php - 缓慢的PHP进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8532551/

相关文章:

javascript - 如何在 JavaScript 中从二进制流打开并读取 PDF

php - 打破大规模 MySQL 更新

mysql - 使用 JOIN 函数

java - 向 Android 版 Google map 添加 5900 多个标记。如何高效做事

php - HTML5 和 getUserMedia - 在特定时间后录制音频并保存到 Web 服务器

PHP 没有通过本地套接字连接到 MYSQL

php - 从 try/catch block 中断

mysql - 获取特定进程 ID 的完整 mysql 查询的最佳方法?

java - 如何制作无分支代码?

android - 哪个 View 是 android 相机预览的最佳选择?