0

嗨,伙计们,我有这个查询,而且速度非常慢。如果您能理解代码,我怎么可能将这两个查询合并为一个,或者我如何才能使这个查询更快。

这是代码

$strSQL  = "SELECT user_status_history.*, queues_config.extension FROM user_status_history ";
    $strSQL .= "LEFT JOIN queues_config ON user_status_history.skillset = REPLACE(queues_config.grppre, \":\", \"\") ";
    $strSQL .= "WHERE user_status_history.status_id = 9 "; # outbound call
    $strSQL .= "AND DATE_FORMAT(status_time, '%Y-%m-%d') = '$check_date' ";
    if ($i==1)
        $strSQL .= "AND TIME_FORMAT(status_time, '%H:%i') >= '$cboHrFrom:$cboMinFrom' ";
    else if (strtotime($check_date) == strtotime($end_date))
        $strSQL .= "AND TIME_FORMAT(status_time, '%H:%i') <= '$cboHrTo:$cboMinTo' ";
    $strSQL .= "AND queues_config.extension = '$cboSkillSet' ";
    if ($cboGroup!='' && $strAgentGroup!='') $strSQL .= "AND user_status_history.user_id IN (".$strAgentGroup.") ";
    $strSQL .= "ORDER BY user_status_history.user_id, user_status_history.status_time ";
    $rs2     = &$cn->Execute($strSQL);
    while (!$rs2->EOF)
    {
        $strSQL  = "SELECT status_time, time_to_sec(timediff(status_time, '".$rs2->fields['status_time']."')) AS timesecs FROM user_status_history ";
        $strSQL .= "LEFT JOIN queues_config ON user_status_history.skillset = REPLACE(queues_config.grppre, \":\", \"\") ";
        $strSQL .= "WHERE status_time > '".$rs2->fields['status_time']."' ";
        $strSQL .= "AND user_id = '".$rs2->fields['user_id']."' ";
        $strSQL .= "AND queues_config.extension = '".$rs2->fields['extension']."' ";
        $strSQL .= "ORDER BY status_time LIMIT 1;";
        $rs3     = &$cn->Execute($strSQL);
        if (!$rs3->EOF)
        {
            $sum_talk_sec = $sum_talk_sec + $rs3->fields['timesecs'];
        }
        $rs3->Close();

        $rs2->MoveNext();
    }
    $rs2->Close();
4

1 回答 1

0

1

检查有表索引

user_status_history.status_id

user_status_history.user_id

2

这些对于优化器来说是困难的。尝试摆脱这些

LEFT JOIN queues_config ON user_status_history.skillset = REPLACE(queues_config.grppre, \":\", \"\") ";
LEFT JOIN queues_config ON user_status_history.skillset = REPLACE(queues_config.grppre, \":\", \"\") ";

3

摆脱循环查询。从 query1 获取所有 ID 到一个数组并为它们创建一个查询:

"SELECT user_id, status_time, time_to_sec(timediff(status_time, '".$rs2->fields['status_time']."')) AS timesecs FROM user_status_history "; ... AND user_id IN (id1, id2, id3 ... )

4(来自 DCOder)

反过来转换这些。使用 PHP 创建 mysql 日期时间字段。

    $strSQL .= "AND DATE_FORMAT(status_time, '%Y-%m-%d') = '$check_date' ";

-- > 
    preg_match('/^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])$/', $check_date, $regs );
    $enddate = date('Y-m-d', mktime( $hour=6, $min=0, $sec=0, $mon=$regs[2], $day=$regs[3]+1, $year=$regs[1] ));
    $strSQL .= " AND status_time > '$check_date' AND status_time <= '$enddate' ; 


    $strSQL .= "AND TIME_FORMAT(status_time, '%H:%i') >= '$cboHrFrom:$cboMinFrom' ";

-- >  $strSQL .= " AND status_time >= '$check_date $cboHrFrom:$cboMinFrom:00' ";

    $strSQL .= "AND TIME_FORMAT(status_time, '%H:%i') <= '$cboHrTo:$cboMinTo' ";

-- >  $strSQL .= " AND status_time <= '$check_date $cboHrTo:$cboMinTo:59' ";
于 2012-09-21T05:46:56.423 回答