【问题标题】:Bottlenecks slowing some part of my script瓶颈减慢了我的脚本的某些部分
【发布时间】:2015-05-29 19:33:30
【问题描述】:

我有一个执行以下步骤的脚本

  1. 用户登录,添加 SMS 消息并指定收件人。该信息被添加到“排队消息表”中;
  2. 某些进程使用 API 发送消息,并将消息移动到“已发送消息表”;
  3. 出现传递报告并从“已发送消息表”中删除消息,并将引用已发送消息的日志条目添加到“已发送消息日志表”中。

当大量消息在“排队消息表”中排队时,步骤(2)和(3)耗时较长,

在将消息推送到 API 之前,会为每个接收者生成一个随机的唯一 id,以供以后检索报告时参考,该 id 用于表“已发送消息日志表”。

下面是示例脚本

<?php
class Message {

    /*
    * random unique id for mobile number
    */
    protected $mobile_ids = array();

    public function add_unique_id($id, $mobile) 
    {
        $this->mobile_ids[] = array($id, $mobile);
    }

    public function get_unique_id()
    {
        return $this->mobile_ids;
    }

    // The method that generated the xml for API
     public function makeXML($param,$multi_part=false) 
     {
            $xmlString =
            "<SMS>
            <authentification>
            <username>sss</username>
            <password>sss</password>
            </authentification>
            <message>
            <sender>sender</sender>";
            if($multi_part == "longSMS") $xmlString .= "<type>longSMS</type>";

            $xmlString .= "<text>{$param['text']}</text></message><recipients>";

            // Array of mobile numbers came from $param
            $phone_numbers = $param['numbers'];

            // Loop through the array and generate <gsm messageId='0001'>mobile</gsm>
            foreach($phone_numbers as $mobile) {

                // Generate id for mobile
                $msg_id = $this->make_random_int();

                /**
                 * This is the slow part of the script,
                 * IDs are added to the array for logging into the database
                 * When message is sent, i looped through the id and created a log for this message
                 **/
                $this->add_unique_id($msg_id, $mobile);


                $xmlString .= "<gsm messageId=\"{$msg_id}\">{$mobile}</gsm>";
            }
            $xmlString .= "</recipients></SMS>";
            return $xmlString;
        }

         /**
          * This is the method that created the log
         * Log the sms
         * You will need to call $msgid = $this->update_db('the sms')
         * return value of $msgid is last_insert_id
         */
        public function log_sms($msgid) {
            // Log the currently sent message
            $userData = array();
            $now = date('Y-m-d H:i:s');
            foreach ($this->mobile_ids as $data) {
                $userData[] = "('{$msgid}', '{$data[0]}', '{$data[1]}', 'QUEUED', '0000-00-00', '0000-00-00', '{$now}')";
             }

             $query = 'INSERT INTO sent_sms_log (txtId,msgID,mobile,status,sentdate_time,deliver_date_time,sysdate_time) VALUES' . implode(',', $userData);
             $this->ci->db->query($query);

             $this->mobile_ids = array(); // reset the array
        }       
     // Make random int
      protected function make_random_int() {
            $this->ci->load->helper('string');
            $int =  random_string('numeric', 12);
            return $int;
        }

         /**
         * Update database after sms sent
         * @return int
         */
        public function update_db($msg, $owner, $qid=0) {
            $data = array('qid'=> $qid, 'sms' => $msg, 'date_time' => date('Y-m-d H:i:s'), 'owner' => $owner);
            $this->ci->db->insert('f_sent_sms', $data);
            return $this->ci->db->insert_id();
        }
}

【问题讨论】:

    标签: php performance codeigniter codeigniter-2


    【解决方案1】:

    我猜它可能是您正在使用的 api。我不得不为 非常 慢的不同服务使用 api。也许尝试使用基准类分析代码的不同部分:

    http://codeigniter.com/user_guide/libraries/benchmark.html

    这将是查找代码中最慢部分的快捷方式。

    【讨论】:

    • 将消息传输到 API 没问题,循环遍历 id 并将其记录到数据库中是很慢的部分,我想知道是否可以找到更好的方法来生成和记录 id跨度>
    【解决方案2】:

    我猜这现在是在某种循环上运行?与其一次插入一个未知数量的记录,不如查看用户指南http://codeigniter.com/user_guide/database/active_record.html 中的 Active Record 的 insert_batch() 方法。您可以使用一个数据库调用来插入所有记录。与将数据插入数据库的循环不同,循环所要做的就是构建一个包含将要插入的所有数据的数组。循环完成后,为刚刚构建的数组运行 insert_batch('f_sent_sms', $my_data)。

    正如@Matthew 已经说过的那样(之前和之后)对它进行基准测试是个好主意。

    【讨论】:

    • 如果您阅读了上面的示例代码,请检查此方法 log_sms(),您在说什么?我从 ids 数组构建一个查询并运行一次查询以插入数据
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2013-07-27
    • 1970-01-01
    • 2018-03-28
    • 2011-06-17
    • 2023-01-16
    • 1970-01-01
    • 2021-03-20
    相关资源
    最近更新 更多