嗨,伙计们,我有这个查询,而且速度非常慢。如果您能理解代码,我怎么可能将这两个查询合并为一个,或者我如何才能使这个查询更快。
这是代码
$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();